Query grants for a table in postgres

后端 未结 7 655
栀梦
栀梦 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 |
    ...
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-22 22:53

    \z mytable from psql gives you all the grants from a table, but you'd then have to split it up by individual user.

    0 讨论(0)
  • 2020-12-22 22:59

    Adding on to @shruti's answer

    To query grants for all tables in a schema for a given user

    select a.tablename, 
           b.usename, 
           HAS_TABLE_PRIVILEGE(usename,tablename, 'select') as select,
           HAS_TABLE_PRIVILEGE(usename,tablename, 'insert') as insert, 
           HAS_TABLE_PRIVILEGE(usename,tablename, 'update') as update, 
           HAS_TABLE_PRIVILEGE(usename,tablename, 'delete') as delete, 
           HAS_TABLE_PRIVILEGE(usename,tablename, 'references') as references 
    from pg_tables a, 
         pg_user b 
    where schemaname='your_schema_name' 
          and b.usename='your_user_name' 
    order by tablename;
    
    0 讨论(0)
  • 2020-12-22 23:01

    I already found it:

    SELECT grantee, privilege_type 
    FROM information_schema.role_table_grants 
    WHERE table_name='mytable'
    
    0 讨论(0)
  • 2020-12-22 23:03

    If you really want one line per user, you can group by grantee (require PG9+ for string_agg)

    SELECT grantee, string_agg(privilege_type, ', ') AS privileges
    FROM information_schema.role_table_grants 
    WHERE table_name='mytable'   
    GROUP BY grantee;
    

    This should output something like :

     grantee |   privileges   
    ---------+----------------
     user1   | INSERT, SELECT
     user2   | UPDATE
    (2 rows)
    
    0 讨论(0)
提交回复
热议问题