Query grants for a table in postgres

后端 未结 7 657
栀梦
栀梦 2020-12-22 22:19

How can I query all GRANTS granted to an object in postgres?

For example I have table \"mytable\":

GRANT SELECT, INSERT ON mytable TO user1
GRANT UPD         


        
7条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-22 22:50

    The query below will give you a list of all users and their permissions on the table in a schema.

    select a.schemaname, a.tablename, b.usename,
      HAS_TABLE_PRIVILEGE(usename, quote_ident(schemaname) || '.' || quote_ident(tablename), 'select') as has_select,
      HAS_TABLE_PRIVILEGE(usename, quote_ident(schemaname) || '.' || quote_ident(tablename), 'insert') as has_insert,
      HAS_TABLE_PRIVILEGE(usename, quote_ident(schemaname) || '.' || quote_ident(tablename), 'update') as has_update,
      HAS_TABLE_PRIVILEGE(usename, quote_ident(schemaname) || '.' || quote_ident(tablename), 'delete') as has_delete, 
      HAS_TABLE_PRIVILEGE(usename, quote_ident(schemaname) || '.' || quote_ident(tablename), 'references') as has_references 
    from pg_tables a, pg_user b 
    where a.schemaname = 'your_schema_name' and a.tablename='your_table_name';
    

    More details on has_table_privilages can be found here.

提交回复
热议问题