Question

i'm trying to make a query that shows the users and all the roles that they have, i already know how to ask about the roles of one particular user:

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

Any idea how to do it?

Was it helpful?

Solution

USERS are themselves ROLES that can login. CREATE USER is actually just an alias. What you probably want is some kind of query that psql issues with \du as a starting point. You can see what psql issues with -E,

********* 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;
**************************

OTHER TIPS

Okay, i found the way to do it, if anyone is interested:

SELECT r.rolname as "Users",    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 "Assigned roles"
FROM pg_catalog.pg_roles r
ORDER BY 1;
Licensed under: CC-BY-SA with attribution
Not affiliated with dba.stackexchange
scroll top