Where PostgreSQL stores your login credentials
PostgreSQL stores usernames and passwords in two places: the server itself keeps an encrypted record of every user account, and your local machine may have a .pgpass file that holds saved credentials for quick login. The method you use to find them depends on whether you set up the database yourself, inherited it from someone else, or are trying to recover access to an existing installation.
If you installed PostgreSQL on your own computer, you created at least one user account during setup — usually called postgres, which is the superuser account that can do anything on the server. If someone else set up the database or you are accessing a remote server, you will need to ask them for the credentials, or use the recovery methods below if you have administrator access to the machine where PostgreSQL runs.
Key Takeaways
- The default superuser account is named postgres, and the password you set during installation is the only way to retrieve it if you forget it.
- Saved passwords on your local machine live in a .pgpass file in your home directory, formatted as hostname:port:database:username:password.
- If you have administrator access to the PostgreSQL server, you can reset a forgotten password by editing the pg_hba.conf file to allow passwordless login, then changing the password from inside PostgreSQL.
- Remote PostgreSQL servers require you to contact the database owner or administrator — there is no way to retrieve credentials you do not have access to.
- PostgreSQL usernames are not the same as your operating system username; they are separate accounts managed only by PostgreSQL itself.
Checking your .pgpass file for saved credentials
If you have logged into PostgreSQL before on your current computer, your credentials may already be saved in a hidden file called .pgpass. This file lives in your home directory and contains login information for databases you have connected to. Open a terminal or command prompt and navigate to your home directory, then look for the file.
On Mac or Linux, type cat ~/.pgpass to display the file contents. On Windows, the file is usually at C:\Users\YourUsername\AppData\Roaming\postgresql\.pgpass — you can open it with Notepad. Each line in the file follows this format: hostname:port:database:username:password. For example, localhost:5432:mydb:postgres:mypassword means the username is postgres and the password is mypassword.
If the .pgpass file does not exist or does not contain the credentials you need, move to the next section. The file only stores passwords you have explicitly saved, so a missing file does not mean the password is lost — it just means you have not saved it yet.
Resetting the postgres password if you have server access
If you installed PostgreSQL yourself and have administrator access to the machine where it runs, you can reset the postgres password even if you have forgotten it. This process involves temporarily allowing passwordless login, then setting a new password from inside PostgreSQL.
First, locate the pg_hba.conf file, which controls how PostgreSQL handles login attempts. On Windows, it is usually in C:\Program Files\PostgreSQL\[version]\data\pg_hba.conf. On Mac, it is often in /Library/PostgreSQL/[version]/data/pg_hba.conf. On Linux, try /etc/postgresql/[version]/main/pg_hba.conf or /var/lib/pgsql/[version]/data/pg_hba.conf. Open the file with a text editor.
Look for a line that says local all postgres peer or local all all peer. Change the word peer at the end to trust. Save the file. Then restart PostgreSQL — on Windows, use Services; on Mac or Linux, use sudo systemctl restart postgresql or the equivalent for your system.
Now open a terminal or command prompt and type psql -U postgres to log in without a password. Once inside PostgreSQL, type ALTER USER postgres WITH PASSWORD 'newpassword';, replacing newpassword with your new password. Then type \q to exit. Change the pg_hba.conf file back to peer or md5, restart PostgreSQL again, and you will now be able to log in with the new password.
Finding usernames in an existing PostgreSQL installation
If you can log into PostgreSQL as any user (including the postgres superuser), you can see all usernames on the server. Connect to PostgreSQL using whatever credentials you have, then type \du to list every user account. This command shows the username, whether the account can create databases, whether it can create other users, and other permissions.
If you need to know which user owns a specific database, type \l to list all databases and see their owners. This helps you understand which account you should be using to connect to a particular database. Remember that PostgreSQL usernames are completely separate from your operating system username — a user named alice in PostgreSQL has nothing to do with a system user named alice.
Recovering access to a remote PostgreSQL server
If the PostgreSQL server is on a different machine — a web host, a cloud provider, or a colleague's computer — you cannot reset the password yourself. You will need to contact whoever manages that server and ask them to reset your password or provide you with the correct credentials.
If you are the one who set up the remote server but cannot remember the password, contact your hosting provider's support team. Explain that you need to reset the PostgreSQL superuser password. Most providers have a process for this, though it may require you to prove you own the account. If the server is on a cloud platform like AWS, Azure, or Google Cloud, check that platform's documentation for password recovery — the process varies by provider.
What to do if you do not know the installation method
If you inherited a PostgreSQL installation from someone else or found one already running on a machine, start by checking whether you have administrator access to that machine. If you do, follow the password reset steps in the section above. If you do not, you will need to contact the person who set it up.
Before asking them, check the .pgpass file on your local machine — you may already have saved credentials from a previous login. Also check any documentation, README files, or setup notes that might be in the PostgreSQL installation directory or in your email history. Many people write down the initial password somewhere accessible, and finding it is faster than asking for a reset.
Frequently Asked Questions
Can I see the postgres password in plain text anywhere?
No. PostgreSQL stores passwords encrypted, and there is no way to decrypt them. If you forget the password, you must reset it using the pg_hba.conf method described above, or contact your server administrator. The only exception is the .pgpass file on your local machine, which stores passwords in plain text for convenience — but only if you saved them there yourself.
What is the default password for PostgreSQL?
There is no default password. During installation, you set a password for the postgres superuser account. If you did not write it down and cannot remember it, you will need to reset it using the pg_hba.conf method or contact your system administrator.
Is the postgres username the same as my computer username?
No. PostgreSQL usernames are separate accounts managed only by PostgreSQL, not by your operating system. You can have a PostgreSQL user named postgres while your computer username is something completely different. They do not interact with each other.
How do I find the username and password for a database someone else created?
Ask the person who created it. They are the only one who knows what username and password they assigned. If they are no longer available, contact your system administrator — they may have access to the server and can reset the password or create a new account for you.
Can I have multiple usernames on the same PostgreSQL server?
Yes. A PostgreSQL server can have many user accounts, each with different permissions and passwords. Use the \du command while logged in as any user to see all accounts on that server. Each user can connect to different databases or the same database, depending on what permissions the superuser has granted them.