[英]pg_restore error: role XXX does not exist
Trying to replicate a database from one system to another.试图将数据库从一个系统复制到另一个系统。 The versions involved are 9.5.0 (source) and 9.5.2 (target).涉及的版本是 9.5.0(源)和 9.5.2(目标)。
Source db name is foodb
with owner pgdba
and target db name will be named foodb_dev
with owner pgdev
.源数据库名称为foodb
,所有者为pgdba
,目标数据库名称为foodb_dev
,所有者为pgdev
。
All commands are run on the target system that will host the replica.所有命令都在将托管副本的目标系统上运行。
The pg_dump
command is: 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;
This runs without errors.这运行没有错误。
The corresponding pg_restore
is:对应的pg_restore
是:
pg_restore --no-owner --if-exists -1 -c -U pgdev -d foodb_dev schema_backup.dump
which throws error:引发错误:
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 ...
If I generate the dump file in plain text format ( -Fp
) I see it includes several entries like:如果我以纯文本格式( -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;
that try to set privileges for user pgdba
who of course doesn't even exist as a user on the target system which only has user pgdev
, and thus the errors from pg_restore
.尝试为用户pgdba
设置权限,该用户当然甚至不作为目标系统上的用户存在,只有用户pgdev
,因此来自pg_restore
的错误。
On the source db the privileges for example of the dump_thread
table:在源数据库上,例如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 |
A quick solution would be to simply add a user pgdba
on the target cluster and be done with it.一个快速的解决方案是简单地在目标集群上添加一个用户pgdba
并完成它。
But shouldn't the --no-owner
take care of not including owner specific commands in the dump in the first place?但是, --no-owner
不应该首先在转储中不包括所有者特定的命令吗?
I realized the --no-owner
is not the same as the -x
.我意识到--no-owner
与-x
不同。 I added the -x
to all pg_dump
commands, which means:我将-x
添加到所有pg_dump
命令中,这意味着:
-x, --no-privileges do not dump privileges (grant/revoke)
which in effect excludes the offending GRANT
/ REVOKE
commands from the dump.这实际上从转储中排除了有问题的GRANT
/ REVOKE
命令。 Problem resolved.问题已解决。
要恢复数据库,请运行以下命令:
pg_restore -x --no-owner -d <db name> backup.dump
Restore DB backup using below command使用以下命令恢复数据库备份
pg_restore --no-privileges --no-owner -h localhost -p <DB_Port> -U <DB_User> -d <DB_Name>-1 <DB_Backup_Path> pg_restore --no-privileges --no-owner -h localhost -p <DB_Port> -U <DB_User> -d <DB_Name>-1 <DB_Backup_Path>
Use flag --no-privileges To prevent restoration of access privileges (grant/revoke commands) and使用标志--no-privileges来防止恢复访问权限(授予/撤销命令)和
--no-owner To prevent setting ownership of objects to match the original database --no-owner防止设置对象的所有权以匹配原始数据库
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.