Every now and then we have/want to run a .sql file or sql script from command line instead of typing each and every sql commands. So, I will write how to execute .sql file from command line in postgresql today. See this howto to see how to install postgresql in ubuntu.
Lets see how to do it:
We can execute the .sql file in two ways from command line(mention in the comment part if you know others).
In the following example commands, simulator is the database name, qs.sql is the .sql file/script with SQL commands, qs.sql file is located at Desktop and present working directory is /home/manohar i.e my home directory.
- From the ubuntu/linux command prompt:
psql -d simulator -f Desktop/qs.sql
psql -U <database_username> -d simulator -f Desktop/qs.sql
- From the psql command prompt:
Hope this helps you all.