How to execute .sql files in a postgres database

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

+8
postgresql psql
source share
2 answers

Use the psql command line tool:

 psql -f file_with_sql.sql 

This command executes all commands in turn (except when the file contains BEGIN ... END blocks. In this case, the commands in the blocks are executed in a transaction). To wrap all the commands in a transaction, use the --single-transaction switch:

 psql --single-transaction -f file_with_sql.sql 

Extra options:

 psql --help 
+15
source share

Just put it on the command line after psql :

 psql example.sql 

psql will take the file and run each line on the server.

If the server is not running on your computer, you need to specify the host name on the computer and the username to log into the server with:

 psql -h server.postgres.com -U username example.sql 

To send multiple files, just list them all:

 psql example1.sql example2.sql example3.sql 
+5
source share

All Articles