我发现更好的方法(我似乎记得这是从 psql 内置的一些查询中获取的,或者可能是 information_schema 视图)是使用has_*_privilege
函数,并简单地将它们应用于用户和对象的所有可能组合的集合。这还将考虑通过某些组角色访问对象。
例如,这将显示哪些用户有权访问非目录表和视图:
select usename, nspname || '.' || relname as relation,
case relkind when 'r' then 'TABLE' when 'v' then 'VIEW' end as relation_type,
priv
from pg_class join pg_namespace on pg_namespace.oid = pg_class.relnamespace,
pg_user,
(values('SELECT', 1),('INSERT', 2),('UPDATE', 3),('DELETE', 4)) privs(priv, privorder)
where relkind in ('r', 'v')
and has_table_privilege(pg_user.usesysid, pg_class.oid, priv)
and not (nspname ~ '^pg_' or nspname = 'information_schema')
order by 2, 1, 3, privorder;
可能的特权在描述中详细说明has_*_privilege
职能在http://www.postgresql.org/docs/current/static/functions-info.html#FUNCTIONS-INFO-ACCESS-TABLE http://www.postgresql.org/docs/current/static/functions-info.html#FUNCTIONS-INFO-ACCESS-TABLE.
“CREATE TEMP”是数据库级权限:它允许用户使用pg_temp_*
架构。可以用以下方法进行测试has_database_privilege(useroid, datoid, 'TEMP')
.