在Postgres中查询表的授权


92

如何查询在postgres中授予对象的所有格兰仕?

例如,我有表“ mytable”:

GRANT SELECT, INSERT ON mytable TO user1
GRANT UPDATE ON mytable TO user2 

我需要带给我的东西:

user1: SELECT, INSERT
user2: UPDATE

Answers:


108

我已经找到了:

SELECT grantee, privilege_type 
FROM information_schema.role_table_grants 
WHERE table_name='mytable'

97

\z mytable 来自psql的命令为您提供了表中的所有授予,但随后您必须按单个用户将其拆分。


您会直接从sql窗格或pg命令行运行它吗?
Daniel L. VanDenBosch

2
@ DanielL.VanDenBosch:所有的meta命令,例如\z,都用于psql。psql是PostgreSQL的命令行界面。
Mike Sherrill'Cat Recall'17

29

如果您确实希望每位用户一行,则可以按受赠者分组(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)

1
几乎是我想要的,我可以拥有GRANTpg_dump之类的确切输出吗?
brauliobo

26

下面的查询将为您提供所有用户及其对表中表的权限的列表。

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参见此处


4
这是唯一计算从其他角色的成员资格获得的权限的答案,因此得到了我的投票。另一方面,我想has_table_privilege(usename, contact(schemaname, '.', tablename), ...)避免歧义。
Paul A Jungwirth

加一-这是纯金!
丹尼尔(Daniel)

9

该查询将列出所有数据库和模式中的所有表(取消注释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 |
...

这仅给出与执行它的用户匹配的行...并非所有授予
Ricky Levi

2

添加到@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;

假设您以具有适当权限的用户身份登录,这将很好地工作。Nitpick:我建议交叉连接应明确编写,例如,FROM pg_tables AS a CROSS JOIN pg_user AS b而不是用逗号进行SQL 92 编写的方式from pg_tables a, pg_user b
Davos

1

这是一个为特定表生成授权查询的脚本。它忽略了所有者的特权。

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;
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.