How to list users in PostgreSQL
Robert Cooper
Robert Cooper Senior Engineer at Basedash
· January 31, 2025
Robert Cooper
Robert Cooper Senior Engineer at Basedash
· January 31, 2025
When working with PostgreSQL, you may find it necessary to list or view all the users (also known as roles) available in your system. This guide provides a comprehensive overview of how to list users in PostgreSQL, along with related administrative tasks.
Basedash is built as an AI-native BI platform, so teams can go from ad hoc SQL to trusted answers and dashboards quickly, without the overhead of traditional BI setup.
psql -U your_username -d your_database_name
Replace your_username with your PostgreSQL username and your_database_name with the name of the database you want to connect to. If you’re the PostgreSQL server administrator, you might use “postgres” as the username.
To list all the users in PostgreSQL, use the \du command inside the psql interface:
\du
This will display a list of all roles, along with their attributes, such as superuser status, role creation capabilities, etc.
The output from \du shows the role name, attributes, and member of columns. Here’s what each column means:
SUPERUSERCREATEDB: Can create new databases.CREATEROLE: Can create new roles.REPLICATION: Can initiate streaming replication and backups.To display detailed information about a specific user, you can use the following SQL query:
SELECT * FROM pg_roles WHERE rolname='your_username';
Replace your_username with the name of the user you’re interested in.
To check the privileges a user has on tables, you can use:
\dp
Or, for a specific table:
\dp your_table_name
If you’re interested in finding out which users are currently connected to your PostgreSQL server, use:
SELECT usename, datname, client_addr FROM pg_stat_activity;
To see the databases to which a user has access, you can run:
SELECT datname FROM pg_database, pg_user WHERE usename = 'your_username' AND has_database_privilege(usename, datname, 'CONNECT');
Replace your_username with the name of the user you’re interested in.
Listing users and understanding their privileges is crucial for managing access and security in your PostgreSQL system. This guide provides you with the essential commands to do so. Always ensure that you manage user access carefully, especially in production environments. Regularly reviewing user roles and permissions helps in maintaining a secure and efficient system.
Written by
Senior Engineer at Basedash
Robert Cooper is a senior engineer at Basedash who builds full-stack product systems across SQL data infrastructure, APIs, and frontend architecture. His work focuses on application performance, developer velocity, and reliable self-hosted workflows that make data operations easier for teams at scale.
Basedash lets you build charts, dashboards, and reports in seconds using all your data.