postgresのオブジェクトに付与されたすべてのGRANTSを照会するにはどうすればよいですか?
たとえば、「mytable」というテーブルがあります。
GRANT SELECT, INSERT ON mytable TO user1
GRANT UPDATE ON mytable TO user2 私に与える何かが必要です:
user1: SELECT, INSERT
user2: UPDATE回答:
\z mytable from psqlはテーブルからすべての権限を提供しますが、その後、個々のユーザーごとに分割する必要があります。
\zはpsql用です。また、psqlはPostgreSQLへのコマンドラインインターフェイスです。
                    ユーザーごとに1行が本当に必要な場合は、権限受領者ごとにグループ化できます(string_aggにはPG9 +が必要です)
SELECT grantee, string_agg(privilege_type, ', ') AS privileges
FROM information_schema.role_table_grants 
WHERE table_name='mytable'   
GROUP BY grantee;これは次のようなものを出力するはずです:
 grantee |   privileges   
---------+----------------
 user1   | INSERT, SELECT
 user2   | UPDATE
(2 rows)GRANTpg_dump出力のような正確なs を持つことができますか?
                    以下のクエリは、スキーマ内のテーブルに対するすべてのユーザーとその権限のリストを提供します。
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';上の詳細はhas_table_privilages見つけることができるここに。
has_table_privilege(usename, contact(schemaname, '.', tablename), ...)曖昧さを避けるために言いたいと思います。
                    このクエリは、すべてのデータベースとスキーマのすべてのテーブルをリストします(WHERE特定のデータベース、スキーマ、またはテーブルをフィルターするために、句の行のコメントを解除します)。特定の特権が付与されているかどうか:
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;出力例:
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 |
...@shrutiの答えに追加
特定のユーザーのスキーマ内のすべてのテーブルの権限をクエリするには
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;FROM pg_tables AS a CROSS JOIN pg_user AS b、SQL 92のようにコンマで行うのではなく、クロス結合を明示的に記述することをお勧めしますfrom pg_tables a, pg_user b
                    以下は、特定のテーブルに対する付与クエリを生成するスクリプトです。所有者の特権を省略します。
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;