How do I execute/run a .sql file in PostgreSQL 9.2.6 + CentOs

Use psql:

psql -f thefile.sql targetdatabase

You may need to specify additional parameters, like username to connect as, host to connect to, etc.


If you already logged in to database then you can use meta commands like

db=# \i or \include filename

For more info see doc


I am adding this it might be useful who try to execute *.sql place where PostgreSQL server located in a difference place.

   psql -h localhost -d userstoreis -U admin -p 5432 -a -q -f /home/jobs/Desktop/resources/postgresql.sql

These are the usage of each parameter

-h PostgreSQL server IP address
-d database name
-U user name
-p port which PostgreSQL server is listening on
-f path to SQL script

then you are prompted to enter the password of the user.