I want to execute several sql scripts sequentially with psql as a single transaction to set up my database schema. What is the best way to do this? In the past I know I had a master script that I ran psql against that included the other files however I don't remember the syntax of this script.


Your script could look like this:

\i file1.sql
\i file2.sql

Or you could do something like this:

cat file1.sql file2.sql | psql -1 -f -
| improve this answer | |

I did it using find in unix / linux with the path to the file sql past with rep_sql parameter with "read" instruction :

find -P ${rep_sql} -type f -name *.sql -execdir psql -d "mabase" -f {} +

It says: find not following links files whatever the name with ext as «.sql» and execute in the directory of the file psql working with mabase and execute the file found.

| improve this answer | |

You could also simply do:

psql -c "BEGIN TRANSACTION;" -f file1.sql -f file2.sql -c "COMMIT;"
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.