pg_restore error: role XXX does not exist
pg_restore error: role XXX does not exist
正在尝试将数据库从一个系统复制到另一个系统。涉及的版本是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
的目标系统上什至不作为用户存在,因此来自 [=20] 的错误=].
在源数据库上的权限,例如 dump_thread
table:
# \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 --no-privileges --no-owner -h localhost -p <DB_Port> -U <DB_User> -d <DB_Name>-1 <DB_Backup_Path>
使用标志
--no-privileges 防止恢复访问权限(grant/revoke 命令)和
--no-owner 防止设置对象的所有权来匹配原始数据库
要恢复数据库,运行以下命令:
pg_restore -x --no-owner -d <db name> backup.dump
正在尝试将数据库从一个系统复制到另一个系统。涉及的版本是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
的目标系统上什至不作为用户存在,因此来自 [=20] 的错误=].
在源数据库上的权限,例如 dump_thread
table:
# \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 --no-privileges --no-owner -h localhost -p <DB_Port> -U <DB_User> -d <DB_Name>-1 <DB_Backup_Path>
使用标志 --no-privileges 防止恢复访问权限(grant/revoke 命令)和
--no-owner 防止设置对象的所有权来匹配原始数据库
要恢复数据库,运行以下命令:
pg_restore -x --no-owner -d <db name> backup.dump