psql,无法将数据库内容复制到另一个 - 不能在事务块内 运行 -

psql, can't copy db content to another - cannot run inside a transaction block-

我想将本地计算机的内容复制到远程计算机(在 docker 内)。 出于某种原因,它比我预期的要复杂: 当我尝试将数据复制到远程数据时,我得到这个 "ERROR: CREATE DATABASE cannot run inside a transaction block".

好的...所以我进入我的 docker 容器,在里面添加了规则 \set AUTOCOMMIT。但我仍然收到此错误。

这是我执行的命令:

// backup
pg_dump -C -h localhost  -U postgres woof | xz >backup.xz

然后在我的远程计算机中:

xz -dc backup.xz | docker exec -i -u postgres waf-postgres psql --set ON_ERROR_STOP=on --single-transaction

但每次我得到这个 "CREATE DATABASE cannot run inside a transaction block" 无论我尝试什么。即使我将自动提交设置为 "on"。

这是我的问题:我不知道什么是事务块。而且我不明白为什么将一个数据库复制到另一个数据库需要如此痛苦:我的远程数据库是空的。那么为什么有这么多大惊小怪,为什么 psql 不能强制我想要的东西?

我的目标只是将本地数据库复制到远程数据库。

这里发生的事情是:你用 -C 键添加 CREATE DATABASE 语句,然后尝试用 --single-transaction 运行 psql,所以内容脚本被包装到 BEGIN;...END;,您不能在其中使用 CREATE DATABASE

因此,针对现有数据库删除 -C 和 运行 psql,或者删除 psql--single-transaction。根据您的实际需要做出决定...

来自 man pg_dump:

-C

--create

Begin the output with a command to create the database itself and reconnect to the created database. (With a script of this form, it doesn't matter which database in the destination installation you connect to before running the script.) If --clean is also specified, the script drops and recreates the target database before reconnecting to it.

来自 man psql:

--single-transaction

This option can only be used in combination with one or more -c and/or -f options. It causes psql to issue a BEGIN command before the first such option and a COMMIT command after the last one, thereby wrapping all the commands into a single transaction. This ensures that either all the commands complete successfully, or no changes are applied.