postgresql - view schema privileges

后端 未结 9 1986
再見小時候
再見小時候 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 12:48

    The privileges are stored in the nspacl field of pg_namespace. Since it's an array field, you have to do a little fancy coding to parse it. This query will give you the grant statements used for users and groups:

    select 
    'grant ' || substring(
              case when charindex('U',split_part(split_part(array_to_string(nspacl, '|'),pu.usename,2 ) ,'/',1)) > 0 then ',usage ' else '' end 
              ||case when charindex('C',split_part(split_part(array_to_string(nspacl, '|'),pu.usename,2 ) ,'/',1)) > 0 then ',create ' else '' end 
           , 2,10000)
    || ' on schema '||nspname||' to "'||pu.usename||'";' 
    from pg_namespace pn,pg_user pu
     where  array_to_string(nspacl,',') like '%'||pu.usename||'%' --and pu.usename='' 
    and nspowner > 1 
    union
    select 
    'grant ' || substring(
              case when charindex('U',split_part(split_part(array_to_string(nspacl, '|'),pg.groname,2 ) ,'/',1)) > 0 then ',usage ' else '' end 
              ||case when charindex('C',split_part(split_part(array_to_string(nspacl, '|'),pg.groname,2 ) ,'/',1)) > 0 then ',create ' else '' end 
           , 2,10000)
    || ' on schema '||nspname||' to group "'||pg.groname||'";' 
    from pg_namespace pn,pg_group pg
     where array_to_string(nspacl,',') like '%'||pg.groname||'%' --and pg.groname='' 
     and nspowner > 1 
    

提交回复
热议问题