PostgreSQL is a powerful, open-source relational database management system widely used in production environments. One of the most common administrative tasks is listing all users in PostgreSQL. Whether you are a beginner learning database basics or an intermediate professional managing production systems, understanding how PostgreSQL users work is essential.
This guide provides a detailed, beginner-friendly explanation of how to list PostgreSQL users, including SQL queries, psql commands, real-world use cases, and best practices.
In PostgreSQL, users are known as roles. A role can act as:
PostgreSQL treats users and roles the same internally, which provides flexibility and fine-grained access control.
Knowing how to list users in PostgreSQL is critical for database administration and security management.
PostgreSQL stores metadata about users and roles in system catalogs. The most important catalog for listing users is:
This system view contains all roles, including login users and group roles.
If you are using the PostgreSQL interactive terminal (psql), the easiest way to list users is with a built-in command.
\du
This command displays:
Role name | Attributes | Member of -----------+------------------------------------------------------------+----------- postgres | Superuser, Create role, Create DB, Replication, Bypass RLS | {} app_user | | {} readonly | Cannot login | {}
This output clearly shows which users can log in and what privileges they have.
For scripting, automation, or application-level access, SQL queries are preferred.
SELECT rolname FROM pg_roles;
This query returns all roles, including users and groups.
To list only users who can log in:
SELECT rolname FROM pg_roles WHERE rolcanlogin = true;
You can retrieve detailed user attributes using additional columns.
SELECT rolname, rolsuper, rolcreatedb, rolcreaterole, rolreplication, rolcanlogin FROM pg_roles;
| Column Name | Description |
|---|---|
| rolname | Name of the PostgreSQL user or role |
| rolsuper | Indicates superuser privileges |
| rolcreatedb | Permission to create databases |
| rolcreaterole | Permission to create roles |
| rolreplication | Replication privileges |
| rolcanlogin | Whether the role can log in |
Imagine you are managing a production PostgreSQL server. You want to verify which users have login access.
SELECT rolname FROM pg_roles WHERE rolcanlogin = true;
This helps you identify:
To see which users can connect to a specific database:
SELECT datname, usename FROM pg_database d JOIN pg_user u ON has_database_privilege(u.usename, d.datname, 'CONNECT');
This query is useful in multi-database environments.
Listing all users in PostgreSQL is a fundamental skill for database administrators and developers. Whether you use psql commands or SQL queries, understanding PostgreSQL roles helps improve security, performance, and maintainability.
By mastering PostgreSQL user listing techniques, you gain better control over access management and system reliability.
PostgreSQL treats users and roles as the same object. A user is simply a role with login privileges.
You can filter login-enabled users using the rolcanlogin column from pg_roles.
Yes, you can list PostgreSQL users using SQL queries in any database client or application.
pg_user is a legacy view that shows only login users, while pg_roles shows all roles.
Using the postgres superuser for applications increases security risks and violates the principle of least privilege.
Copyrights © 2024 letsupdateskills All rights reserved