Where PostgreSQL stores your username
Your PostgreSQL username is stored in the server's user list, not in a single file you can open. The easiest way to find it is to check what you used when you first connected to the database, or to ask your database administrator if someone else set up your account. If you have command-line access to the server itself, you can list all PostgreSQL users by running a query or checking the system files where PostgreSQL keeps user records.
The method you use depends on whether you already have access to PostgreSQL, whether you're on the same machine as the database, and whether you have administrator permissions. Most people find their username by looking at connection records or asking whoever manages the database.
Key Takeaways
- If you can connect to PostgreSQL, you already know your username — it's what you typed at the password prompt.
- To list all PostgreSQL users on a server you have access to, run psql -U postgres -l or connect and run \du inside the psql prompt.
- PostgreSQL user records live in the pg_user system table, which only administrators can view directly.
- If you've forgotten your username and don't have server access, contact your database administrator or hosting provider.
- The default PostgreSQL administrator account is named postgres, but custom usernames are common in production databases.
Check your connection history or configuration files
If you've connected to PostgreSQL before, your username is in the files or commands you used. Look for a .pgpass file in your home directory (on Linux or Mac) or in %APPDATA%\postgresql on Windows. This file stores connection credentials in plain text, one per line, in the format hostname:port:database:username:password.
You can also check your shell history or command-line scripts. If you've run psql -U myusername or psql --username=myusername before, that username is the one you need. On Windows, check any batch files or shortcuts that launch PostgreSQL tools — they often include the username in the command.
List all PostgreSQL users if you have server access
If you can log into the PostgreSQL server with any account, you can see all usernames. Connect to PostgreSQL using the psql command-line tool, then run \du to display all users and their roles:
First, open a terminal or command prompt and type psql -U postgres (or another username you know). You'll be prompted for a password. Once you're inside the psql prompt, type \du and press Enter. PostgreSQL will list every user account, along with their permissions and whether they can create databases or other users.
If you don't know any username to start with, try postgres — this is the default administrator account created when PostgreSQL is installed. If that doesn't work, you may need to use operating system authentication. On Linux or Mac, if you're logged in as the system user who owns the PostgreSQL process, you can sometimes connect without a password by running sudo -u postgres psql.
Query the system tables directly
Once you're connected to PostgreSQL as an administrator, you can run SQL to see all usernames. Type this query at the psql prompt:
SELECT usename FROM pg_user;
This returns a list of every PostgreSQL user on that server. The pg_user table is a system view that only superusers (administrators) can read, so you need to be logged in as an admin account to run this query. If you get a permission error, you don't have administrator rights on that database.
For more detail about each user — including whether they can log in, create databases, or create other users — run:
SELECT usename, usesuper, usecreatedb, usecreaterole FROM pg_user;
Check PostgreSQL configuration on the server itself
If you have direct access to the machine running PostgreSQL, you can inspect the files where user data is stored. PostgreSQL keeps user information in the pg_authid system table, which is stored in binary format inside the database cluster directory. This directory is usually /var/lib/postgresql/[version]/main on Linux or C:\Program Files\PostgreSQL\[version]\data on Windows.
You cannot read these files directly with a text editor — they're in PostgreSQL's internal format. Instead, use the psql tool to query the system tables, as described above. If PostgreSQL won't start or you can't connect, you may need to recover the database using PostgreSQL's recovery tools, which is beyond the scope of finding a username.
Contact your hosting provider or database administrator
If you don't have access to the PostgreSQL server and you've forgotten your username, the fastest option is to contact whoever manages the database. This might be your hosting provider, your company's database administrator, or the person who set up the account for you. They can tell you your username when ready or reset your password so you can log in.
When you contact them, have your account details ready — your email address, the name of the database you're trying to access, or any project or process name associated with the account. This helps them find your user record quickly.
Frequently Asked Questions
What's the difference between a PostgreSQL username and a database name?
A PostgreSQL username (or role) is the account you use to log in to the server. A database name is a specific collection of data within that server. One user can access multiple databases, and multiple users can access the same database. You need both the username and password to connect, but the database name is optional — you can connect to the server first and then choose which database to use.
Can I change my PostgreSQL username?
Yes, but only if you have administrator rights. Connect as a superuser and run ALTER USER oldname RENAME TO newname;. If you don't have administrator rights, ask your database administrator to change it for you. Changing a username doesn't affect the data in your databases — it only changes the account name you use to log in.
What if I'm locked out and can't remember any password?
On Linux or Mac, if you have system access, you can sometimes restart PostgreSQL in recovery mode or use peer authentication to log in without a password. On Windows or hosted systems, contact your database administrator or hosting provider — they can reset the password for any account. If you're the only administrator, you may need to use PostgreSQL's single-user mode, which requires stopping the database service.
Is the default postgres username always there?
Yes. PostgreSQL creates a superuser account named postgres during installation. This account cannot be deleted, though it can be disabled or have its password changed. If you have any access to the server at all, the postgres account is your entry point to finding other usernames.