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';
但是如果我删除数据库然后运行相同的命令,它会抱怨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
我一直在 pg_restore 文档https://www.postgresql.org/docs/15/app-pgrestore.html和其他一些似乎相关的问题https://stackoverflow.com/questions/58970102/how -to-restore-a-postgres-backup-error-cannot-drop-the-current-open-databas但我对我做错了什么感到有点茫然。
从您链接的文档中:
如果数据库已经存在,则无法创建它。如果它尚不存在,则无法连接到它以创建它自己。因此,连接到不同的数据库(传统上使用“postgres”)以创建新的“weatherdata”数据库。