Query grants for a table in postgres

后端 未结 7 670
栀梦
栀梦 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:47

    This query will list all of the tables in all of the databases and schemas (uncomment the line(s) in the WHERE clause to filter for specific databases, schemas, or tables), with the privileges shown in order so that it's easy to see if a specific privilege is granted or not:

    SELECT grantee
          ,table_catalog
          ,table_schema
          ,table_name
          ,string_agg(privilege_type, ', ' ORDER BY privilege_type) AS privileges
    FROM information_schema.role_table_grants 
    WHERE grantee != 'postgres' 
    --  and table_catalog = 'somedatabase' /* uncomment line to filter database */
    --  and table_schema  = 'someschema'   /* uncomment line to filter schema  */
    --  and table_name    = 'sometable'    /* uncomment line to filter table  */
    GROUP BY 1, 2, 3, 4;
    

    Sample output:

    grantee |table_catalog   |table_schema  |table_name     |privileges     |
    --------|----------------|--------------|---------------|---------------|
    PUBLIC  |adventure_works |pg_catalog    |pg_sequence    |SELECT         |
    PUBLIC  |adventure_works |pg_catalog    |pg_sequences   |SELECT         |
    PUBLIC  |adventure_works |pg_catalog    |pg_settings    |SELECT, UPDATE |
    ...
    

提交回复
热议问题