Run Postgres Query from Command line or File

Pass query in command line

SYNTAX

psql -U <user_name> -p <port> -d <database_name> -c “<query>

psql -U postgres -p 5432 -d test -c "select * from test"

 

PASS QUERY FILE IN COMMAND IN POSTGRESQL

psql -U <user_name> -p <port> -d <database_name> -f “<file_path>”

Step 1: Create text file a.txt and write query which you want to execute.

psql -U postgres -p 5432 -d test -f a.txt

Leave a Reply