how to execute .sql files in postgres database

sasikala picture sasikala · Nov 26, 2015 · Viewed 58.6k times · Source

I am using postgres database with PostGIS and PGAdmin. I have many .sql files with different sizes like 300MB, 280MB etc to be inserted into database. What is the best way to do it i.e through java code or some psql commands. I am very new to java and postgres database also. Please give me some suggestion.

Answer

Tomasz Jakub Rup picture Tomasz Jakub Rup · Nov 26, 2015

Use psql command line tool:

psql -f file_with_sql.sql

This command executes all commands line-by-line (except when file contains BEGIN…END blocks. In this case commands in blocks executes in transaction). To wrap all commands in transaction use --single-transaction switch:

psql --single-transaction -f file_with_sql.sql

For more options:

psql --help