How to Get the Name of the Current User in PostgreSQL

While working with the PostgreSQL database, identifying the user name is an important task that helps us manage access to the database. For this purpose, PostgreSQL offers several methods that retrieve the name of a user that is currently using the PostgreSQL database, such as CURRENT_USER, USER, and CURRENT_ROLE. Moreover, users can get a list of currently logged-in users or all users using different commands and functions.

This write-up demonstrates how to get the name of the current user in Postgres using the following content:

  • Method 1: Using CURRENT_ROLE Function
  • Method 2: Using CURRENT_USER Function
  • Method 3: Using the USER Function
  • Bonus Tip 1: Get Currently/Presently Logged-in Users
  • Bonus Tip 2: Get All Users

How to Get the Name of the Current User in Postgres Using CURRENT_ROLE?

Execute the CURRENT_ROLE function with the aid of a SELECT query to get the name of the current user. This function doesn't require any arguments and it is executed without parentheses:

SELECT CURRENT_ROLE;

The output snippet shows that the current user is “postgres”:

img

How to Get the Name of the Current User in Postgres Using CURRENT_USER?

Use the CURRENT_USER function with the assistance of a SELECT statement to fetch the name of the current user: The stated function doesn't need any arguments and must be executed without parentheses:

SELECT CURRENT_USER;

The desired function executed successfully and it retrieves “postgres” as output, which is nothing but the name of the current user:

img

How to Get the Name of the Current User in Postgres Using the USER Function?

Alternatively, users can execute the USER() function with the help of a SELECT statement to fetch the name of the current user. The stated function doesn’t require any parenthesis to execute:

SELECT USER;

The function executed successfully, and it returned "postgres" as the output, which is the name of the current user:

img

Bonus Tip 1: How to Get Currently Logged-in Users in Postgres?

Use the “pg_stat_activity” view with the “SELECT DISTINCT” clause to fetch the information of the currently logged-in users in PostgreSQL:

SELECT DISTINCT usename AS logged_in_users
FROM pg_stat_activity;

The below output snippet retrieves the names of the currently logged-in users:

img

Check out our dedicated guide on how to locate logged-in users in PostgreSQL for more details.

Bonus Tip 2: How to Get All Users in Postgres?

Postgres offers several methods to find/get all users in Postgres. The “\du” is one such method that retrieves the user name along with their attributes:

\du
img

For more details on getting the list of users, check out the following guide.

Conclusion

In PostgreSQL, the “CURRENT_USER”, “CURRENT_ROLE”, and “USER” methods are used to get the details of the current user. All these methods do not require parameters and are executed without any parentheses. Moreover, the “pg_stat_activity” view and “\du” command can be used to get currently logged-in users or all users, respectively. This post has elaborated on various methods to get the name of the current user in PostgreSQL.