Say I have files:
file1.sql
file2.sql
file3.sql
I need all three files to be executed in one transaction. I am looking for a bash script like:
psql -h hostname -U username dbname -c "
begin;
\i file1.sql
\i file2.sql
\i file3.sql
commit;"
Error with the error Syntax error at or near "\".
I also tried connecting to the database first and then crashing, for example:
psql dbname
begin;
\i file1.sql
\i file2.sql
\i file3.sql
commit;
This also fails because the begin command is only executed when the connection is complete.
Is it possible to execute multiple .sql files in a single transaction using PostgreSQL and bash?
Edit:
The rough structure of each file is similar:
SET CLIENT_ENCODING TO 'WIN1251';
\i file4.sql
\i file5.sql
<etc>
RESET CLIENT_ENCODING;
source
share