Cannot drop a role that is granted to connect database

前端 未结 1 500
半阙折子戏
半阙折子戏 2021-01-25 04:01

I am using PostgreSQL 10.4 and I found a strange behavior.

If we create an role and grant it to CONNECT the database:

CREATE ROLE dummy;
GRA         


        
1条回答
  •  醉梦人生
    2021-01-25 04:33

    There are some very unintuitive permission requirements when using REASSIGN.

    I have found that when a superuser account is not available (as in the case of RDS or Cloud SQL) I have to grant the target role to my current role in order to reassign or drop owned objects from the target role. For instance, if my active user is postsgres, and I'm trying to remove user_a:

    > DROP OWNED BY user_a
    ERROR:  permission denied to drop objects
    > GRANT user_a TO postgres;
    GRANT ROLE
    > DROP OWNED BY user_a;
    DROP OWNED
    

    Now, it becomes a little trickier if user_a happens to be a member of postgres, especially if it happens to inherit that membership through some other role, let's call it schema_admin...

    > DROP OWNED BY user_a
    ERROR:  permission denied to drop objects
    > GRANT user_a TO postgres;
    ERROR:  role "user_a" is a member of role "postgres"
    
    -- Alright, let's try to revoke it...
    > REVOKE postgres FROM user_a;
    REVOKE ROLE
    > GRANT user_a TO postgres;
    ERROR:  role "user_a" is a member of role "postgres"
    
    -- It's still a member through the inherited grant - trying to revoke again doesn't work:
    > REVOKE postgres FROM user_a;
    WARNING:  role "metabase" is not a member of role "postgres"
    REVOKE ROLE
    
    -- So you have to identify the role it's inheriting from, and revoke that:
    > REVOKE schema_admin FROM user_a;
    REVOKE ROLE
    > GRANT user_a TO postgres;
    GRANT ROLE
    
    -- Now just to be safe, I'll reassign owned objects before actually dropping everything:
    > REASSIGN OWNED BY user_a TO postgres;
    REASSIGN OWNED
    > DROP OWNED BY user_a;
    DROP OWNED
    > DROP ROLE user_a;
    DROP ROLE;
    

    Voila!

    Note: There is another widely-referenced and effective answer here: https://sysadmintips.com/services/databases/postgresql-error-permission-denied-to-reassign-objects/ which works great, as long as you are able to create and log in as a new temporary user. However, in some contexts, that is a problem in itself (and then you also have the extra cleanup to handle of removing that temporary role when you're done), so I tried to avoid that here.

    0 讨论(0)
提交回复
热议问题