What is the default password for Postgres

Learn what is the default password for postgres with practical examples, diagrams, and best practices. Covers postgresql, windows, postgresql-9.3 development techniques with visual explanations.

Understanding PostgreSQL Default Passwords and Security Best Practices

Hero image for What is the default password for Postgres

Explore the default password behavior of PostgreSQL, especially on Windows installations, and learn essential security practices to protect your database.

When setting up a new PostgreSQL database, especially on Windows, a common question arises: "What is the default password?" Unlike some other database systems, PostgreSQL doesn't typically come with a universal 'default' password that's widely known or hardcoded. This article will clarify how PostgreSQL handles initial user authentication, particularly for the postgres superuser, and guide you through securing your installation.

The 'postgres' Superuser and Initial Setup

During the installation process, PostgreSQL usually prompts you to set a password for the postgres superuser. This user is the database administrator and has full privileges over all databases. If you're installing on Windows using the official installer, you will almost certainly be asked to provide a password for this user. If you proceed without explicitly setting one, the installer might default to a blank password or use a system-level authentication method, depending on the version and specific installer options chosen.

Authentication Methods and pg_hba.conf

PostgreSQL uses a client authentication configuration file named pg_hba.conf (Host-Based Authentication) to determine how clients can connect to the database. This file specifies which hosts can connect, which users they can connect as, and what authentication method they must use. Common authentication methods include:

  • trust: Allows anyone to connect without a password (highly insecure).
  • ident / peer: Uses the operating system's user identity.
  • md5 / scram-sha-256: Requires a password, encrypted using MD5 or SCRAM-SHA-256 hashing.
  • password: Requires a password, sent in plain text (less secure than md5/scram-sha-256).

On Windows, the installer often configures pg_hba.conf to use md5 for local connections, requiring the password you set during installation. For connections from other machines, it might default to md5 or scram-sha-256.

flowchart TD
    A[PostgreSQL Client Request] --> B{Check pg_hba.conf}
    B -->|Match Found| C{Authentication Method?}
    C -->|trust| D[Access Granted]
    C -->|ident/peer| E{OS User Match?}
    E -->|Yes| D
    C -->|md5/scram-sha-256| F{Password Provided?}
    F -->|Yes & Correct| D
    F -->|No or Incorrect| G[Access Denied]
    E -->|No| G

PostgreSQL Client Authentication Flow

Recovering or Changing the Password

If you've forgotten the postgres superuser password, you can reset it by temporarily modifying the pg_hba.conf file to allow trust authentication for the postgres user from localhost. This allows you to connect without a password, then use SQL commands to set a new password. Remember to revert pg_hba.conf immediately after changing the password.

1. Locate pg_hba.conf

Find the pg_hba.conf file in your PostgreSQL data directory (e.g., C:\Program Files\PostgreSQL\9.3\data\pg_hba.conf on Windows).

2. Edit pg_hba.conf

Open the file with a text editor and locate the line for local connections for the postgres user. Change its authentication method to trust.

host all postgres 127.0.0.1/32 trust

3. Restart PostgreSQL Service

Restart the PostgreSQL service for the changes to take effect. On Windows, you can do this via the Services manager (services.msc).

4. Connect and Reset Password

Connect to PostgreSQL using psql as the postgres user without a password:

psql -U postgres

Then, set a new password:

ALTER USER postgres WITH PASSWORD 'your_new_strong_password';

5. Revert pg_hba.conf and Restart

Change the authentication method back to md5 or scram-sha-256 in pg_hba.conf and restart the PostgreSQL service again to re-enable secure authentication.

ALTER USER postgres WITH PASSWORD 'your_new_strong_password';

SQL command to change the password for the 'postgres' user.