Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

postgresql query to show the groups of a user

Tags:

sql

postgresql

If I create a user in a group, like:

create role user_1 login inherit in role group_1; 

later, with which query could I retrieve to which group(s) a user belongs to?

like image 309
Philippe Gonday Avatar asked Aug 24 '10 07:08

Philippe Gonday


People also ask

How do I list users in PostgreSQL?

Use \du or \du+ psql command to list all users in the current database server. Use the SELECT statement to query the user information from the pg_catalog.

What is group by in PostgreSQL?

The PostgreSQL GROUP BY clause is used in collaboration with the SELECT statement to group together those rows in a table that have identical data. This is done to eliminate redundancy in the output and/or compute aggregates that apply to these groups.

How do I list all tables in PostgreSQL?

To list the tables in the current database, you can run the \dt command, in psql : If you want to perform an SQL query instead, run this: SELECT table_name FROM information_schema.


2 Answers

Just to give a copy&pastable solution - On PostgreSQL (tested 8.4 and 9.3) you can do:

select rolname from pg_user join pg_auth_members on (pg_user.usesysid=pg_auth_members.member) join pg_roles on (pg_roles.oid=pg_auth_members.roleid) where pg_user.usename='USERNAME'; 

where USERNAME is the name of the login role you are interested in.

like image 154
alfonx Avatar answered Sep 18 '22 18:09

alfonx


From the psql command line:

\dg 

or

\du 
like image 23
John Kloian Avatar answered Sep 16 '22 18:09

John Kloian