PG数据恢复must be member of role “xxxx“

        背景:有一份数据库备份文件,sql语句中包含了drop语句。postgresql数据中有一个模式:test_schema,owner是test_schema_onwer。有另外一个用户test_username2。test_username2具有test_schema的所有权限。

        test_username2才是操作test_schema的用户。

        现在使用test_username2登录,并执行恢复备份语句。然后出现了must be member of role "xxxx"。

        原因:只有模式的owner才能删除表和创建索引。

        解决办法:将test_schema的owner权限从test_schema_onwer改为test_username2。

实现:

先使用test_schema_onwer登录数据库:

-- 查看用户关系
postgres=> \du
                                      List of roles
    Role name    |                         Attributes                         | Member of 
-----------------+------------------------------------------------------------+-----------
 coremanage      | Create role                                                | {}
 coremanagetest1 |                                                            | {}
 postgres        | Superuser, Create role, Create DB, Replication, Bypass RLS | {}

-- 查看所有模式的owner
postgres=> \dn
       List of schemas
      Name      |   Owner    
----------------+------------
 core_manage_db | postgres
 public         | postgres
 test_schema    | coremanage
(3 rows)

-- 将 coremanagetest1  加入到 coremanage      
postgres=> grant coremanage to coremanagetest1 ;
GRANT ROLE
postgres=> \du
                                        List of roles
    Role name    |                     Attributes                         |  Member of   
-----------------+--------------------------------------------------------+--------------
 coremanage      | Create role                                            | {}
 coremanagetest1 |                                                        | {coremanage}
 postgres        | Superuser, Create role, Create DB, Replication, Bypass RLS | {}

-- 切换coremanagetest1 登录

postgres=> \c postgres coremanagetest1;
You are now connected to database "postgres" as user "coremanagetest1".
-- 修改owner
postgres=> alter schema test_schema owner to coremanagetest1; 
ALTER SCHEMA

-- 查看权限

postgres=> \dn
         List of schemas
      Name      |      Owner      
----------------+-----------------
 core_manage_db | postgres
 public         | postgres
 test_schema    | coremanagetest1
(3 rows)

这样就可以修改过来了

如果这时候需要收回coremanage的coremanagetest1权限。只需要在切换到coremanage登录然后执行:

revoke coremanage from coremanagetest1;

你可能感兴趣的:(sql,数据库,postgresql)