how to execute .sql files in postgres database

Use the psql command line tool:

psql -f file_with_sql.sql

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

psql --single-transaction -f file_with_sql.sql

For more options:

psql --help

Leave a Comment

tech