When installing PostgreSQL on Windows, there is no predefined default password for the postgres
user (the superuser). Instead, during the installation process, you are required to set a password for the postgres
user. Here’s what to know:
Default Password for Postgres
- No default password exists. You must specify a password for the
postgres
user during the PostgreSQL installation process. - If you cannot recall the password or it was not explicitly set, you will need to reset it (explained below).
What If You Forgot the Password?
If you’ve forgotten the postgres
user password, you can reset it by following these steps:
1. Edit the PostgreSQL pg_hba.conf
File
- Navigate to the PostgreSQL data directory. The default path is:
C:\Program Files\PostgreSQL\<version>\data
- Open the
pg_hba.conf
file in a text editor (e.g., Notepad or Notepad++). - Locate the authentication method for the
postgres
user. It will look like:host all all 127.0.0.1/32 md5
- Change
md5
totrust
:host all all 127.0.0.1/32 trust
This allows you to connect without a password temporarily.
- Save the file and restart the PostgreSQL service:
- Press
Win
+R
, typeservices.msc
, and press Enter. - Find PostgreSQL in the list, right-click it, and select Restart.
- Press
2. Reset the Postgres Password
- Open the Command Prompt or SQL Shell (psql).
- Connect to the database as the
postgres
user:psql -U postgres
- Run the following command to reset the password:
ALTER USER postgres WITH PASSWORD 'newpassword';
Replace
'newpassword'
with your desired password. - Exit the prompt by typing:
\q
3. Revert the pg_hba.conf
File
- Change the authentication method in
pg_hba.conf
back tomd5
(orscram-sha-256
if you prefer stronger authentication):host all all 127.0.0.1/32 md5
- Save the file and restart the PostgreSQL service again.
Tips to Avoid Losing Passwords
- Save the password securely (e.g., in a password manager) during installation.
- Use environment variables or configuration files for applications to avoid hardcoding the
postgres
credentials.