我试图在PostgreSQL的单个事务中运行多个SQL文件。在Linux环境中,实际上可以通过使用here-document实现:
psql -U postgres -h localhost -d mydatabase << files
BEGIN;
\i file1.sql
\i file2.sql
commit;
files
但我无法在Windows环境中实现相同的功能。
将所有内容放在一个文件中,例如
\i file1.sql
\i file2.sql
然后使用-f
参数调用psql。要强制单个事务使用--single-transaction
psql -U postgres -h localhost -d mydatabase --single-transaction -f the_script.sql