Query grants for a table in postgres

后端 未结 7 656
栀梦
栀梦 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 23:09

    Here is a script which generates grant queries for a particular table. It omits owner's privileges.

    SELECT 
        format (
          'GRANT %s ON TABLE %I.%I TO %I%s;',
          string_agg(tg.privilege_type, ', '),
          tg.table_schema,
          tg.table_name,
          tg.grantee,
          CASE
            WHEN tg.is_grantable = 'YES' 
            THEN ' WITH GRANT OPTION' 
            ELSE '' 
          END
        )
      FROM information_schema.role_table_grants tg
      JOIN pg_tables t ON t.schemaname = tg.table_schema AND t.tablename = tg.table_name
      WHERE
        tg.table_schema = 'myschema' AND
        tg.table_name='mytable' AND
        t.tableowner <> tg.grantee
      GROUP BY tg.table_schema, tg.table_name, tg.grantee, tg.is_grantable;
    
    0 讨论(0)
提交回复
热议问题