Open In App

How to List All Users in PostgreSQL

Last Updated : 03 Oct, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

If we need to access all the users in our PostgreSQL database, we are in the right place. Whether we are a database administrator or a developer, having a clear method to list all users is essential for managing access and permissions.

This article will guide us through a simple and effective way to retrieve a complete list of users with minimal effort.

How to List All Users in PostgreSQL

Having a basic understanding of PostgreSQL is essential to easily obtain the list of users. Listing users is useful for database administrators to:

  • Keep track of who has access to the database.
  • Manage roles and permissions.
  • Perform audits or maintain security compliance.
  • Identify inactive or unnecessary accounts.

Syntax:

To list users in PostgreSQL, there are different approaches depending on whether we are using the psql command-line interface. We can use the \du command in psql which is a simple way to retrieve a list of all roles, following is the syntax for this:

\du

1. List All Users in PostgreSQL Using Psql command

For listing the users in PostgreSQL we can run the command \du which can tell us about the list of all users, following are the steps for this:

Step 1: Login into PostgreSQL

First step is to login into the PostgreSQL database, for this simply open the command line from the start menu and type the following command in the terminal:

psql -U postgresql

Once we enter this command it will ask for the database password. After adding the password we will enter the postgreSQL where we can type and execute the commands now.

Login-into-PostgreSQL
Login into PostgreSQL.

Step 2: Get List of All Users

Once we have succssfully logged into the database, we can run the following command which will display us the list of all of the users who are present in the postgreSQL:

\du

Once this command is executed, shows us the roles of each of the users as well as the list of roles attributes for the users.

Get-List-of-All-Users
Get List of All Users

Output:

  • rolname: The name of the user/role.
  • rolsuper: Indicates if the user has superuser privileges (t for true, f for false).
  • rolcreaterole: Shows if the user can create new roles.
  • rolcreatedb: Indicates if the user has permission to create databases.
  • rolreplication: Shows if the user can initiate replication.

Step 3: Exit PostgreSQL Database:

Once we successfully see the list of users, we can exit the database, for this we will simply have to type the following command in the terminal and it will exit the databse for us:

\q
Exit-PostgreSQL-Database
Exit PostgreSQL Database.

2. List All Users in PostgreSQL using SQL Query

In PostgreSQL, user information is stored in the pg_catalog.pg_user table. By querying this table, we can retrieve a comprehensive list of all users along with their roles and attributes.

The following SQL query will provide a list of all users, including their role names and specific attributes such as whether they are superusers or have the ability to create databases.

Here is the SQL query to list all users in PostgreSQL.

SELECT usename AS role_name,
CASE
WHEN usesuper AND usecreatedb THEN
CAST('superuser, create database' AS pg_catalog.text)
WHEN usesuper THEN
CAST('superuser' AS pg_catalog.text)
WHEN usecreatedb THEN
CAST('create database' AS pg_catalog.text)
ELSE
CAST('' AS pg_catalog.text)
END role_attributes
FROM pg_catalog.pg_user
ORDER BY role_name desc;
PostgreSQL-List-User-Using-SQL
PostgreSQL List of all User using SQL

Explanation:

  • role_name: The name of the user or role.
  • role_attributes: Indicates the privileges associated with that user.

Conclusion

Listing users in PostgreSQL is a simple yet vital task for database management. We can use the \du command in the psql interface, PostgreSQL provides a variety of ways to view and manage users. This method can be used because it offers a very clear view into the roles and their associated permissions.


Next Article
Article Tags :

Similar Reads