【问题标题】:pg_restore error: role XXX does not existpg_restore 错误:角色 XXX 不存在
【发布时间】:2016-09-13 06:35:58
【问题描述】:

试图将数据库从一个系统复制到另一个系统。涉及的版本是 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 不应该首先在转储中不包括所有者特定的命令吗?

【问题讨论】:

    标签: database postgresql restore


    【解决方案1】:

    我意识到--no-owner-x 不同。我将-x 添加到所有pg_dump 命令中,这意味着:

    -x, --no-privileges          do not dump privileges (grant/revoke)
    

    实际上从转储中排除了有问题的GRANT/REVOKE 命令。问题已解决。

    【讨论】:

    • 是否有任何方法可以在没有 -x 选项的情况下恢复数据库。在我的情况下,转储是由其他人提供给 mr 因此我无法使用)x 选项修改它
    • pg_restore --no-privileges --no-owner ... 允许您指定在还原时不包括所有权或权限,无论这些权限和所有权是否包含在pg_dump
    • @user1448319 这应该是答案。在恢复时跳过所有者/特权比在转储时要好得多。
    • @mivk:值得注意的是,如果在pg_dump 中使用(默认)“普通”格式选项,则无法在恢复时应用--no-privileges/--no-owner 之类的选项,因此将它们与pg_dump 一起使用是唯一的选择。
    • 删除所有权和特权有什么不可预见的缺点吗?
    猜你喜欢
    • 2017-05-25
    • 1970-01-01
    • 2017-01-23
    • 2011-11-04
    • 1970-01-01
    • 2017-09-02
    • 1970-01-01
    • 2018-12-02
    • 1970-01-01
    相关资源
    最近更新 更多