Postgres查询每个用户的角色

时间:2017-01-31 19:29:36

标签: sql postgresql roles

我正在尝试创建一个显示用户及其所有角色的查询。我已经知道如何询问一个特定用户的角色:

SELECT oid, rolname FROM pg_roles WHERE
pg_has_role( 'name_of_user', oid, 'member');

知道该怎么做吗?

1 个答案:

答案 0 :(得分:1)

运行psql -E以查看psql中元命令背后的语句:

vao=# \du
********* QUERY **********
SELECT r.rolname, r.rolsuper, r.rolinherit,
  r.rolcreaterole, r.rolcreatedb, r.rolcanlogin,
  r.rolconnlimit, r.rolvaliduntil,
  ARRAY(SELECT b.rolname
        FROM pg_catalog.pg_auth_members m
        JOIN pg_catalog.pg_roles b ON (m.roleid = b.oid)
        WHERE m.member = r.oid) as memberof
, r.rolreplication
, r.rolbypassrls
FROM pg_catalog.pg_roles r
ORDER BY 1;
**************************

                                   List of roles
 Role name |                         Attributes                         | Member of
-----------+------------------------------------------------------------+-----------
 ro        | Cannot login                                               | {}
 rw        | Cannot login                                               | {}
 vao       | Superuser, Create role, Create DB, Replication, Bypass RLS | {ro,rw}