pg_restore 在导入备份时都会抱怨数据库是否存在

Geu*_*uis 3 postgresql pg-restore

Postgresql 版本:15 备份是从版本 12 开始的。

(我不知道 --schema-only 是否是问题的一部分。)

我有一个数据库的备份 tar 文件,我正在尝试恢复它,但我一直遇到奇怪的冲突。

如果我预先创建数据库weatherdata并尝试使用该--create选项,则会收到数据库已存在的预期错误。

postgres@logs:~$ pg_restore -e --create --schema-only -U postgres -d weatherdata /home/geuis/postgres-bak-3-31-22.tar 
pg_restore: error: could not execute query: ERROR:  database "weatherdata" already exists
Command was: CREATE DATABASE weatherdata WITH TEMPLATE = template0 ENCODING = 'UTF8' LC_COLLATE = 'en_US.UTF-8' LC_CTYPE = 'en_US.UTF-8';
Run Code Online (Sandbox Code Playgroud)

但是如果我删除数据库然后运行相同的命令,它会抱怨weatherdata 存在

postgres=# DROP DATABASE weatherdata;
DROP DATABASE
postgres=# exit
postgres@logs:~$ pg_restore -e --create --schema-only -U postgres -d weatherdata /home/geuis/postgres-bak-3-31-22.tar 
pg_restore: error: connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed: FATAL:  database "weatherdata" does not exist
Run Code Online (Sandbox Code Playgroud)

我一直在 pg_restore 文档https://www.postgresql.org/docs/15/app-pgrestore.html和其他一些似乎相关的问题/sf/ask/4127907171/ -to-restore-a-postgres-backup-error-cannot-drop-the-currently-open-databas但我对我做错了什么感到有点茫然。

jja*_*nes 6

从您链接的文档中:

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

如果数据库已经存在,则无法创建它。如果它尚不存在,您将无法连接到它以创建自身。因此,连接到另一个数据库(传统上使用“postgres”)以创建新的“weatherdata”数据库。