pg_restore 错误:“关系不存在”并创建新数据库

Ian*_*n23 6 postgresql psql pg-dump exists pg-restore

我已经备份了我想要恢复到新数据库中的特定表,使用:

call pg_dump -Fc -h server -d database -U user -p password -v -f dump.sql -t public.table1 -t public.table2
Run Code Online (Sandbox Code Playgroud)

我没有问题。

然后我想通过使用 pg_restore 创建一个新数据库来恢复转储:

call pg_restore --clean --create -d temp -h server -p password -U user dump.sql
Run Code Online (Sandbox Code Playgroud)

这给了我一个“数据库临时不存在”的错误。据我所知,这应该已经创建了数据库并恢复了它。

但是,我然后手动创建“临时”数据库并运行:

call pg_restore --clean --create -d temp -h server -p password -U user dump.sql
Run Code Online (Sandbox Code Playgroud)

这将贯穿始终,但不会创建表并给我一个错误“ relation table1”和“ relation table2”不存在,并且只为两个表创建相应的 id_sewuences。

我真正想要的是不必手动创建新数据库,并且备份中的所有表都通过 pg_restore 使用以下命令恢复到全新的数据库中:

call pg_restore --clean --create -d temp -h server -p password -U user dump.sql
Run Code Online (Sandbox Code Playgroud)

据我了解。

请帮助,非常令人沮丧

Dan*_*ité 7

--create-d一起使用时,该参数-d不是要创建的数据库的名称,它是现有数据库的名称连接到运行CREATE DATABASE语句,因为这是不可能创建一个数据库,如果你不是已经连接到另一个数据库.

这被记录为:

使用此选项时,以 -d 命名的数据库仅用于发出初始 DROP DATABASE 和 CREATE DATABASE 命令。所有数据都恢复到出现在存档中的数据库名称。

这就是为什么不存在数据库临时pg_restore错误的原因

您应该createdb在还原过程中添加一个步骤,因为无论如何您都希望创建一个特定的数据库名称,而不是来自备份的名称。

对于第二个问题,这是一个完全不同的问题,如果没有完整的错误消息,很难猜测为什么会发生。

另请注意,-p后面不是密码而是端口号。