3

I am trying to run multiple SQL files in a single transaction of PostgreSQL. In Linux environments this can actually be achieved by using here-document as:

psql -U postgres -h localhost -d mydatabase << files
BEGIN;
\i file1.sql
\i file2.sql
commit;
files

But I am unable to achieve the same in Windows environment.

3
  • Put everything in a SQL script and then use psql .. -f commands.sql Commented Dec 16, 2017 at 8:50
  • But i also need to have transaction in this Commented Dec 16, 2017 at 9:09
  • If the here document you have shown works in Linux, the this will also work as a single file in Windows Commented Dec 16, 2017 at 9:55

1 Answer 1

7

Put everything in a one file, e.g.

\i file1.sql
\i file2.sql

Then call psql with the -f parameter. To force a single transaction use --single-transaction

psql -U postgres -h localhost -d mydatabase --single-transaction -f the_script.sql
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.