How to list all the users in PostgreSQL
PostgreSQL is a free relational database management system, which emphasizes flexibility and SQL compliance. It is sometimes referred to as Postgres. Viewing every user in the database could be essential for database management and website administration needs. This Answer will examine how to list each user in PostgreSQL. These methods may also list PostgreSQL users and roles or users and permissions.
We will list every user in PostgreSQL using two different techniques so that we can choose which is the best and easiest to use.
SQL queries
The
psqlcommand
SQL queries
The first method we will use to list all the users in PostgreSQL is by writing SQL queries. The database user's information is accessible via pg_user. We must access the pg_catalog.pg_user user columns containing data on all users to obtain a list of PostgreSQL users using SQL.
Let's see this in action.
Coding example
SELECT usename AS username,CASEWHEN usesuper THENCAST('superuser' AS pg_catalog.text)ELSECAST('No user' AS pg_catalog.text)END user_attributeFROM pg_catalog.pg_userORDER BY username asc;
Explanation
Line 1: We select the
usenameusing an aliasusername.Line 2: We introduced the
CASEexpression, which is the same as the if/else statement in most programming languages.Line 3: The
usesuperis a boolean type for a"superuser", so what we are saying here is that if the user is a"superuser", perform the operations on lines 4 and 6.Line 4: We print out the
"superuser"usingCASThere because we have to perform a conversion between two data types.Line 5: We use the
ELSEstatement.Line 6: We print out
"No user".Line 7: We commit this transaction using the
ENDkeyword.Line 8: We'll fetch all the results from the
pg_usertable which gives us access to information about database users.Line 9: Lastly, we order the result in ascending order.
We get an output username and user_attribute with a user named postgres who has the attribute "superuser". Note that this may be different on your computer.
There is another way to achieve the same result using SQL. Using this method we will select the fields we want directly from the pg_user table.
Coding example
SELECT usesysid as id, usename as name,usesuper as super_user,passwd as password_sha1FROM pg_shadowORDER BY usename;
Explanation
Line 1: We select the
usesysidusing an aliasid, whereusesysidis the ID of the user.Line 2: We select the
usenameusing an aliasname, whereusenameis the user name.Line 3: We select
usesuperusing an aliassuper_user, whereusesuperis a boolean to check if the user is a superuser.Line 4: We select
passwdusing an aliaspassword_sha1, wherepasswdis the password in text but we can't see it because it is hashed.ββLine 5: The
pg_shadowcontains information about database users.Line 6: Lastly, we order the result by their username.
The psql command
We can also list the users in PostgreSQL by interacting directly via the shell or the terminal. There is a command called psql that can be called upon to list existing users in our PostgreSQL shell.
The \du command is used to achieve this purpose, and we get an output containing a table with "role name" and other properties.
First, use the Postgres user to connect to the PostgreSQL server.
$ psql -U postgres
We will need to supply the password for the user.
We will then use the \du command to display a list of all user accounts (or roles) on the active PostgreSQL server.
postgres \du
The \du+ command, which adds a new column named description, can be used to display more information.
Here is an example of how it works:
\du+
Most of the time, we will want to list all the users in the PostgreSQL server, but the most important of them is to check the number of users we have connected to the database and the role they all possess. Using the \du or \du+ commands, or with the help of the SELECT statement to query the user data from the pg_catalog.pg_user catalog and we get a list showing the list of all users on the current database server.