![](/img/trans.png)
[英]PostgreSQL pg_restore error from pgadmin4 showing role doesn't exist
[英]pg_restore error: role XXX does not exist
试图将数据库从一个系统复制到另一个系统。 涉及的版本是 9.5.0(源)和 9.5.2(目标)。
源数据库名称为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
命令。 问题已解决。
要恢复数据库,请运行以下命令:
pg_restore -x --no-owner -d <db name> backup.dump
使用以下命令恢复数据库备份
pg_restore --no-privileges --no-owner -h localhost -p <DB_Port> -U <DB_User> -d <DB_Name>-1 <DB_Backup_Path>
使用标志--no-privileges来防止恢复访问权限(授予/撤销命令)和
--no-owner防止设置对象的所有权以匹配原始数据库
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.