Check Your PostgreSQL Username from the Command Line

The fastest way to see which PostgreSQL user you are logged in as is to open a terminal or command prompt and type a single command. If you are already connected to a PostgreSQL database, the command \du (inside psql) shows all users on that server. If you need to know your current login before connecting, the command whoami (on Mac or Linux) or echo %username% (on Windows) shows your operating system user — which is often the same as your PostgreSQL username, though not always.

To see this in action: open your terminal, type psql to enter the PostgreSQL prompt (you may need to specify a host or port if your database is not on your local machine), then type \du and press Enter. You will see a list of all PostgreSQL users on that server, with your current user marked. The username appears in the leftmost column.

Key Takeaways

  • The command \du inside psql shows all PostgreSQL usernames on your server, with your current user identified.
  • Your PostgreSQL username is separate from your operating system username, even if they happen to match.
  • If you cannot remember your username, you can look it up in your database connection settings or ask your database administrator.
  • On Windows, PostgreSQL often creates a default user called postgres during installation; on Mac and Linux, it usually matches your system username.

Find Your Username in Connection Settings

If you have already set up a connection to PostgreSQL in a tool like pgAdmin, DBeaver, or a code editor, your username is stored in the connection profile. Open the connection settings or properties panel — the exact location depends on your tool, but it is usually under "Edit Connection" or "Connection Properties." Look for a field labeled "User," "Username," or "Login Name." This is the PostgreSQL username that tool uses to connect on your behalf.

If you are using a web process or backend code that connects to PostgreSQL, the username is often in a configuration file. Look for a file named .env, config.js, database.yml, or settings.py (the exact name depends on your programming language). Search for a line containing user=, username=, or DB_USER=. The value after the equals sign is your PostgreSQL username.

Query the PostgreSQL System Tables

If you are already logged into PostgreSQL and want to confirm your current username programmatically, you can run a SQL query. Type SELECT current_user; at the psql prompt and press Enter. PostgreSQL will return the username you are currently logged in as. This is useful when you are working inside a script or process and need to verify which user the connection is running under.

You can also query the system table that stores all user information. Type SELECT usename FROM pg_user; to see a list of all users on the server. The column usename (note the spelling — it is not "username") contains the PostgreSQL username for each row. This query works only if your current user has permission to read the pg_user table, which is true for most administrative accounts.

Recover a Forgotten Username

If you have lost track of your PostgreSQL username and cannot find it in your connection settings or configuration files, your database administrator can look it up for you. They can connect to the server and run \du or query pg_user to see all usernames. If you are the administrator and have lost your own username, you can often connect as the default postgres user (which is created during PostgreSQL installation) and create a new user or reset permissions on an existing one.

On a local machine where you installed PostgreSQL yourself, you can also check the PostgreSQL installation logs or documentation. During setup, PostgreSQL typically creates a superuser account — on Windows this is usually postgres, and on Mac or Linux it is usually your system username. If you are still stuck, reinstalling PostgreSQL and choosing a username you will remember is a valid last resort for a development machine.

Understand the Difference Between System and Database Users

A common source of confusion is that PostgreSQL usernames are separate from your operating system username. When you type whoami in your terminal, you get your system user. When you type \du in psql, you get PostgreSQL users. They can have the same name, but they are managed by different systems and do not have to match.

This matters because PostgreSQL can be configured to trust your system user automatically (a setup called "peer authentication"), which means you can type psql without a password if your system username matches a PostgreSQL username. But if they do not match, you will need to provide a password or specify a different username with the -U flag, like psql -U postgres.

Use the Correct Flag When Connecting

If you know your PostgreSQL username but need to connect to a database, use the -U flag followed by the username. For example, psql -U myusername -d mydatabase connects to mydatabase as the user myusername. If you do not specify a username, psql assumes you want to use your operating system username, which may or may not exist as a PostgreSQL user.

You can also specify the host and port if your database is not running on your local machine. The full command looks like psql -U myusername -h 192.168.1.100 -p 5432 -d mydatabase. The -h flag sets the host, -p sets the port (5432 is the default), and -d sets the database name. Once you are connected, you can verify your username again with SELECT current_user;

Frequently Asked Questions

What if I type \du and see no users listed?

You likely do not have permission to view the user list, or you are not connected to a PostgreSQL server. Try typing SELECT current_user; to confirm you are logged in. If that returns a username, you are connected but lack permission to list other users — ask your database administrator for access.

Is my PostgreSQL username the same as my Linux or Windows username?

Not necessarily. PostgreSQL creates its own set of users during installation. On Linux, the default user is often your system username. On Windows, it is usually postgres. They can match by coincidence, but they are managed separately and do not have to be the same.

Can I change my PostgreSQL username?

Yes. If you are logged in as a superuser (usually postgres), you can run ALTER USER oldname RENAME TO newname; to rename a user. If you are not a superuser, you can change only your own password with ALTER USER currentname WITH PASSWORD 'newpassword';

What if I forgot the password for my PostgreSQL user?

If you have access to the server as a superuser or system administrator, you can reset it. Log in as postgres and run ALTER USER targetuser WITH PASSWORD 'newpassword'; On a local development machine, you can also reinstall PostgreSQL and set a new password during setup.

How do I see what databases a specific user can access?

Type \du+ username to see detailed information about that user, including which databases they have permission to use. You can also query SELECT * FROM information_schema.role_table_grants WHERE grantee='username'; to see specific table-level permissions.