postgresql - view schema privileges

后端 未结 9 2021
再見小時候
再見小時候 2020-12-24 12:17

Is there a query I can run to show currently assigned privileges on a particular schema?

i.e. privileges that were assigned like so:

GRANT USAGE ON S         


        
9条回答
  •  清歌不尽
    2020-12-24 13:05

    For current question can try this one:

    SELECT r.rolname AS role_name,
           n.nspname AS schema_name,
           p.perm AS privilege
    FROM pg_catalog.pg_namespace AS n
        CROSS JOIN pg_catalog.pg_roles AS r
        CROSS JOIN (VALUES ('USAGE'), ('CREATE')) AS p(perm)
    WHERE has_schema_privilege(r.oid, n.oid, p.perm)
    --      AND n.nspname <> 'information_schema'
    --      AND n.nspname !~~ 'pg\_%'
    --      AND NOT r.rolsuper
    

    Could be pretty low in performance at database with a lot of objects and users with which I have come across. So i've got possible workaround using aclexplode() default function like this:

    SELECT  oid_to_rolname(a.grantee) AS role_name,
            n.nspname AS schema_name,
            a.privilege_type AS privilege_type
    FROM pg_catalog.pg_namespace AS n,
            aclexplode(nspacl) a
    WHERE n.nspacl IS NOT NULL 
            AND oid_to_rolname(a.grantee) IS NOT NULL 
    --      AND n.nspname <> 'information_schema'
    --      AND n.nspname !~~ 'pg\_%'
    

    But, be careful, last one doesn't include privileges which users have obtained from PUBLIC role. Where oid_to_rolname() is simple custom function SELECT rolname FROM pg_roles WHERE oid = $1.

    And, like @Jaisus, my task required to have all privileges which all users have. So i have similar to schema privileges queries for table, views, columns, sequences, functions, database and even default privileges.

    Also, there is helpful extension pg_permission where I get logic for provided queries and just upgraded it for my purposes.

提交回复
热议问题