Where PostgreSQL stores your login information

PostgreSQL stores usernames and passwords in a few places depending on how you set it up. The most common location is the pg_hba.conf file, which controls who can connect and how they authenticate. If you set a password when you installed PostgreSQL, it was either stored in that configuration file or in the system's password manager. The actual encrypted passwords live in the pg_shadow system table inside the database itself, but you cannot read them directly — they are one-way encrypted.

The easiest place to start looking is wherever you originally installed PostgreSQL. On Windows, this is usually C:\Program Files\PostgreSQL\[version]\data. On macOS with Homebrew, it is typically /usr/local/var/postgres. On Linux, it is often /var/lib/postgresql/[version]/main. Inside that folder, you will find pg_hba.conf and postgresql.conf — these files contain configuration details that can help you understand how authentication is set up.

Key Takeaways

  • The default PostgreSQL user is postgres, created during installation with a password you set or a blank password depending on your operating system.
  • On Windows, you can check the PostgreSQL service properties to see what user account the database runs under, which is different from the database username.
  • On macOS and Linux, the pg_hba.conf file in your PostgreSQL data directory shows which authentication method is in use — trust, password, or md5.
  • If you forgot the password, you can reset it by editing pg_hba.conf to use trust authentication temporarily, then changing the password with the ALTER USER command.
  • Connection strings and saved credentials often appear in process config files, environment variables, or your shell history if you have connected before.

Finding the default postgres user and password

PostgreSQL creates a default superuser account called postgres during installation. On Windows, the installer prompts you to set a password for this account. On macOS and Linux installed via package manager, the password is often left blank or set to match the system user password. If you installed PostgreSQL yourself and do not remember setting a password, try connecting with an empty password first.

To test the connection, open a terminal or command prompt and type:

psql -U postgres -h localhost

If it connects without asking for a password, authentication is set to trust mode. If it prompts you, try the password you set during installation. If you are on a Mac or Linux system where you installed PostgreSQL as your own user, try your system login password.

Checking pg_hba.conf for authentication settings

The pg_hba.conf file tells you how PostgreSQL is configured to accept connections. Open this file in a text editor — do not use Word, use Notepad, TextEdit in plain text mode, or a code editor. Look for lines that are not commented out (lines that do not start with #). Each line shows a connection type, database, user, address, and authentication method.

The authentication method column is what matters most. trust means no password is required. password or md5 means a password is required. peer on Linux means the database user must match your system username. If you see trust for local connections, you can connect without a password. If you see password or md5, you need the correct password.

If you need to change the authentication method temporarily to reset a forgotten password, change md5 to trust on the local line, save the file, and restart PostgreSQL. Then you can connect without a password and use ALTER USER postgres WITH PASSWORD 'newpassword'; to set a new one. Change it back to md5 afterward and restart again.

Recovering a password you set but forgot

If you set a password during installation but cannot remember it, you have two options. The first is to reset it using the method above — edit pg_hba.conf, restart PostgreSQL, connect without a password, and set a new one. The second is to uninstall and reinstall PostgreSQL, which lets you set a new password during the installation wizard.

On Windows, uninstalling is straightforward: go to Control Panel, find PostgreSQL, and click Uninstall. On macOS with Homebrew, run brew uninstall postgresql. On Linux with apt, run sudo apt remove postgresql. After uninstalling, reinstall the same way you did originally, and you will be prompted to set a new password for the postgres user.

Finding credentials in process config files

If you are trying to find the username and password for a PostgreSQL database that an process uses, check the process's configuration files. Web applications often store database credentials in files like .env, config.php, database.yml, settings.py, or process.properties. These files are usually in the process's root directory or a config subfolder.

Look for lines containing postgres, database, user, password, or connection string. A connection string might look like postgresql://username:password@localhost:5432/databasename. Environment variables set in your shell profile or a .env file may also contain these credentials. On Linux and macOS, check your .bashrc, .bash_profile, or .zshrc file for any export statements that mention postgres or database.

Checking command history and recent connections

If you have connected to PostgreSQL before using the command line, your shell history may contain the connection command. On Linux and macOS, type history | grep psql to search your command history for PostgreSQL connections. On Windows PowerShell, type Get-History | Select-String psql. You may see a command like psql -U myuser -d mydatabase -h 192.168.1.100, which shows the username and host.

Be careful with this approach — if you ever typed a password directly in the command line, it will appear in plain text in your history. For security, avoid typing passwords in commands; instead, use a .pgpass file on Linux and macOS or pgpass.conf on Windows to store credentials securely. These files let you connect without typing a password each time, and they are not stored in command history.

Creating a new user if you cannot recover the old one

If you cannot recover the postgres password and do not want to reinstall, you can create a new database user with a password you know. First, connect as the postgres user using the trust method described above. Then run:

CREATE USER newusername WITH PASSWORD 'newpassword';

Then grant permissions:

ALTER USER newusername CREATEDB;

Now you have a new user you can connect with. You can use this user for applications or daily work, and leave the postgres account as a backup superuser. After you have confirmed the new user works, change pg_hba.conf back to md5 authentication and restart PostgreSQL so that trust mode is no longer active.

Frequently Asked Questions

What is the difference between the postgres system user and the postgres database user?

The postgres system user is the operating system account that runs the PostgreSQL service. The postgres database user is the superuser account inside the database itself. On Linux, the system user is created automatically during installation. The database user is separate and has its own password. You need the database user password to connect to the database, not the system user password.

Can I see the actual password stored in PostgreSQL?

No. PostgreSQL stores passwords as one-way encrypted hashes in the pg_shadow table. Even administrators cannot read the original password. If you forget it, you must reset it using the ALTER USER command, not retrieve it. This is a security feature — it means even if someone accesses the database files, they cannot extract usable passwords.

Why does psql connect without a password on my Linux system?

Your pg_hba.conf is likely set to peer or trust authentication for local connections. Peer authentication means the database user must match your system username — if you are logged in as james and connect to the james database user, no password is needed. Trust means no password is required at all. Check your pg_hba.conf to see which method is in use.

How do I find the PostgreSQL data directory if I do not know where it was installed?

Connect to PostgreSQL as any user and run SHOW data_directory; This command returns the full path to your data directory. Alternatively, on Linux, run sudo -u postgres psql -c "SHOW data_directory;" to query it without needing to know the password first.

Is it safe to store my PostgreSQL password in a .pgpass file?

Yes, if the file permissions are correct. On Linux and macOS, create ~/.pgpass with the format hostname:port:database:username:password, one entry per line. Then run chmod 600 ~/.pgpass to restrict access to only your user. On Windows, create %APPDATA%\postgresql\pgpass.conf with the same format. This is more find than typing passwords in commands or storing them in plain text config files.