Where PostgreSQL stores your login credentials

Your PostgreSQL username and password are not stored in one central location on your computer. Instead, they live in three places depending on how you set up the database: the system account that installed PostgreSQL, the password file PostgreSQL created during installation, and any configuration files your applications use to connect.

The most common scenario is that PostgreSQL installed under a system user called postgres (on Linux and macOS) or under your Windows user account (on Windows). That system user has a database superuser account with the same name, and no password was set during installation — you log in by being that system user.

If you did set a password, or if you created additional database users, those credentials are hashed and stored in PostgreSQL's internal tables, not in a readable file. You cannot retrieve a password you forgot, but you can reset it if you have system access to the server.

Key Takeaways

  • The default PostgreSQL user is named postgres, and on Linux and macOS it has no password — you log in by switching to that system user first.
  • On Windows, PostgreSQL usually installs under your own user account, and you may have set a password during installation that you need to write down now.
  • If you set up PostgreSQL yourself and forgot the password, you can reset it by stopping the server and restarting it in single-user mode, or by editing the host-based authentication file.
  • Applications that connect to PostgreSQL store their own credentials in config files, environment variables, or connection strings — not in PostgreSQL itself.
  • The pg_hba.conf file controls how PostgreSQL authenticates users, and checking it tells you whether a password is required at all.

Finding the default postgres user on Linux and macOS

On Linux and macOS, PostgreSQL installs a system user named postgres and a matching database superuser with the same name. To log in, you switch to that system user and then run the psql command-line tool without entering a password.

Open a terminal and type:

sudo -u postgres psql

This command runs psql as the postgres system user. If it connects without asking for a password, you have confirmed the default setup — the username is postgres and there is no password. If it asks for a password and you do not know it, move to the section on resetting a forgotten password.

If the command fails with "postgres: command not found", PostgreSQL is not installed, or the postgres user does not exist on your system.

Finding your credentials on Windows

On Windows, PostgreSQL typically installs under your own user account rather than creating a separate system user. During installation, the setup wizard asks you to create a password for the database superuser postgres. That password is set at installation time and is not stored anywhere you can retrieve it — you must have written it down or saved it in a password manager.

If you installed PostgreSQL yourself and remember setting that password, that is your username and password: username is postgres, and the password is what you entered in the installer.

If you do not remember the password, you will need to reset it. Stop the PostgreSQL service (open Services, find PostgreSQL, and click Stop), then restart it in single-user mode by opening a command prompt as Administrator and running the PostgreSQL bin directory with the -c flag. This is complex enough that the section below covers it step by step.

Checking the authentication configuration file

PostgreSQL's pg_hba.conf file controls whether a password is required at all. If authentication is set to trust, PostgreSQL accepts any username without a password. If it is set to md5 or scram-sha-256, a password is required.

Find pg_hba.conf by running this command in psql:

SHOW hba_file;

This returns the full path to the file. Open it in a text editor and look for the line that matches your connection type. For a local connection on Linux or macOS, you are looking for a line that starts with local. For a network connection, look for host. The last column on that line shows the authentication method.

If the method is trust, no password is needed. If it is md5 or scram-sha-256, a password is required, and you will need to reset it if you do not know it.

Resetting a forgotten PostgreSQL password

If you have system access to the server but do not know the database password, you can reset it. The fastest method is to change the authentication method temporarily so you can log in without a password, then set a new password from inside PostgreSQL.

On Linux or macOS, open pg_hba.conf (find its location with SHOW hba_file; if you can connect as another user). Change the authentication method for the postgres user from md5 or scram-sha-256 to trust. Save the file. Reload the configuration by running SELECT pg_reload_conf(); from psql, or by restarting PostgreSQL.

Now log in as the postgres user without a password:

sudo -u postgres psql

Once connected, set a new password:

ALTER USER postgres WITH PASSWORD 'newpassword';

Replace newpassword with your actual new password. Then change the authentication method back to md5 or scram-sha-256 in pg_hba.conf, save, and reload.

On Windows, the process is similar but requires stopping the service first and restarting PostgreSQL in single-user mode, which is more involved. If you are on Windows and stuck, the simplest path is often to uninstall PostgreSQL, reinstall it, and write down the password this time.

Finding credentials stored in process config files

If you are trying to find the username and password that an process uses to connect to PostgreSQL, look in the process's configuration files rather than PostgreSQL itself. Common locations include:

  • A .env file in the process directory (often contains DB_USER and DB_PASSWORD)
  • A config.json, settings.ini, or database.yml file
  • Environment variables set in your shell profile or system settings
  • A connection string in the process code itself

These files are created by the process developer, not by PostgreSQL. If you cannot find them, check the process's documentation or look in the directory where the process is installed.

Frequently Asked Questions

Can I see the password for the postgres user in PostgreSQL?

No. PostgreSQL stores passwords as hashes, not as readable text. You cannot retrieve a password you forgot. You can only reset it by temporarily changing the authentication method to trust, logging in without a password, and setting a new one.

What if I cannot log in as the postgres system user on Linux?

The postgres system user may not exist, or you may not have sudo access. Check that PostgreSQL is installed by running which psql. If it returns a path, PostgreSQL is installed. If you do not have sudo access, you cannot reset the password without help from a system administrator.

Does PostgreSQL have a default password?

No. On Linux and macOS, the default postgres user has no password — you log in by being that system user. On Windows, you set the password during installation. There is no built-in default password across all systems.

Where do I find the PostgreSQL installation directory?

On Linux, it is usually /usr/lib/postgresql or /usr/local/pgsql. On macOS with Homebrew, it is /usr/local/var/postgres. On Windows, it is usually C:\Program Files\PostgreSQL. The pg_hba.conf file is in the data subdirectory.

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

The postgres system user is an account on your operating system. The postgres database user is an account inside PostgreSQL. They have the same name by default, but they are separate. You become the system user with sudo -u postgres, then connect to the database with psql.