如何在postgres中查询授予对象的所有GRANTS?
例如我有“mytable”表:
GRANT SELECT, INSERT ON mytable TO user1
GRANT UPDATE ON mytable TO user2
我需要一些能给我的东西:
user1: SELECT, INSERT
user2: UPDATE
答案 0 :(得分:87)
\z mytable
为您提供表中的所有授权,但您必须由个别用户将其拆分。
答案 1 :(得分:85)
我已经找到了:
SELECT grantee, privilege_type
FROM information_schema.role_table_grants
WHERE table_name='mytable'
答案 2 :(得分:23)
如果你真的想要每个用户一行,你可以按受让人分组(要求PG9 +用于string_agg)
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)
答案 3 :(得分:20)
尝试以下查询。它将为您提供表格中所有用户及其权限的列表。
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 a.tablename='your_table_name';
答案 4 :(得分:7)
此查询将列出所有数据库和模式中的所有表(取消注释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 |
...
答案 5 :(得分: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;
答案 6 :(得分:1)
添加到@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;