postgresql pg_restore使用了不正确的数据库名称

jum4pzuy  于 2023-05-28  发布在  PostgreSQL
关注(0)|答案(1)|浏览(132)

我想复制一个'本地' postgres数据库,其中生活在docker,到远程机器。本地docker的数据库默认名称为postgres,远程为proddb。我运行以下命令:

pg_dump --format=custom --dbname=postgresql://postgres:postgres@localhost:5432/postgres | PGPASSWORD=pa$$word pg_restore -v --clean --create --host=production.local --username=swasher --dbname=proddb

在终端中,我看到这个命令创建postgres数据库,而不是proddb

pg_restore: connecting to database for restore
pg_restore: dropping DATABASE postgres
pg_restore: creating DATABASE "postgres"
pg_restore: connecting to new database "postgres"
pg_restore: creating COMMENT "DATABASE postgres"
pg_restore: creating EXTENSION "hstore"
pg_restore: creating COMMENT "EXTENSION hstore"
pg_restore: terminated by user

为什么pg_restore忽略指定的dbname键?

h43kikqp

h43kikqp1#

如果将--create选项与pg_restore配合使用,则将首先执行恢复进程

CREATE DATABASE postgres;

然后使用连接到新数据库

\c postgres

并继续恢复转储。
如果另外添加--clean,则pg_restore将首先执行

DROP DATABASE postgres;

因此,使用pg_restore连接的数据库是无关紧要的,因为pg_restore无论如何都会创建并连接到数据库postgres
如果要将转储还原到其他数据库,请忽略--create选项,以便pg_restore连接到proddb并将转储还原到该数据库。

相关问题