试图将数据库从一个系统复制到另一个系统。涉及的版本是9.5.0(source)和9.5.2(target)。
源数据库名称为 foodb
,所有者为 pgdba
,目标数据库名称为 foodb_dev
,所有者为 pgdev
.
所有命令都在将托管副本的目标系统上运行。
pg_dump
命令是:
pg_dump -f schema_backup.dump --no-owner -Fc -U pgdba -h $PROD_DB_HOSTNAME -p $PROD_DB_PORT -d foodb -s --clean;
这运行没有错误。
对应的pg_restore
为:
pg_restore --no-owner --if-exists -1 -c -U pgdev -d foodb_dev schema_backup.dump
抛出错误:
pg_restore: [archiver (db)] Error while PROCESSING TOC:
pg_restore: [archiver (db)] Error from TOC entry 3969; 0 0 ACL public pgdba
pg_restore: [archiver (db)] could not execute query: ERROR: role "pgdba" does not exist
Command was: REVOKE ALL ON SCHEMA public FROM PUBLIC;
REVOKE ALL ON SCHEMA public FROM pgdba;
GRANT ALL ON SCHEMA public TO pgdba;
GRANT ...
如果我以纯文本格式 (-Fp
) 生成转储文件,我会看到它包含多个条目,例如:
REVOKE ALL ON TABLE dump_thread FROM PUBLIC;
REVOKE ALL ON TABLE dump_thread FROM pgdba;
GRANT ALL ON TABLE dump_thread TO pgdba;
GRANT SELECT ON TABLE dump_thread TO readonly;
尝试为用户 pgdba
设置权限,当然,在只有用户 pgdev
的目标系统上,该用户甚至不作为用户存在,因此出现错误来自 pg_restore
。
在源数据库上,dump_thread
表的示例权限:
# \dp+ dump_thread
Access privileges
-[ RECORD 1 ]-----+--------------------
Schema | public
Name | dump_thread
Type | table
Access privileges | pgdba=arwdDxt/pgdba+
| readonly=r/pgdba
Column privileges |
Policies |
一个快速的解决方案是简单地在目标集群上添加一个用户 pgdba
并完成它。
但是 --no-owner
难道不应该首先注意不在转储中包含所有者特定的命令吗?
最佳答案
我意识到 --no-owner
与 -x
不同。我将 -x
添加到所有 pg_dump
命令中,这意味着:
-x, --no-privileges do not dump privileges (grant/revoke)
这实际上从转储中排除了违规的 GRANT
/REVOKE
命令。问题已解决。
关于database - pg_restore 错误 : role XXX does not exist,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37271402/