↑ Top

How To Reset Your Postgresql Password On Windows 2025

ullas kunder

Designer & Developer

📝 README: Resetting PostgreSQL Password on Windows (PostgreSQL 17.x)

📚 Purpose

This guide is for students or developers who forgot their PostgreSQL postgres user password on Windows, and want to reset it quickly and cleanly without reinstalling PostgreSQL.

🧠 What You'll Learn

  • How to locate the pg_hba.conf file
  • How to open it in VS Code or Notepad from terminal
  • How to temporarily disable password authentication
  • How to reset the password from psql
  • How to revert security settings

🐘 Requirements

  • PostgreSQL installed (e.g. 17.x)
  • Admin access to your PC
  • PowerShell or Command Prompt
  • Optional: VS Code installed

🧩 Bonus: Add PostgreSQL to PATH (Optional)

Add this to system environment variables:

C:\Program Files\PostgreSQL\17\bin

Then restart your terminal to use psql from anywhere.

🚀 Step-by-Step Guide

1️⃣ Open PowerShell as Admin

🔍 Press Win, search for PowerShell, then right-click → Run as Administrator

2️⃣ Locate Your PostgreSQL Data Directory

Run this command to check default install path:

cd "C:\Program Files\PostgreSQL\17\data"

If you're not sure where PostgreSQL is installed:

  • Open Services (services.msc)
  • Find your PostgreSQL service (e.g. postgresql-x64-17)
  • Right-click → Properties
  • Look at Path to executable — it will include the data directory path

3️⃣ Open pg_hba.conf in a Text Editor

Option A: Using Notepad

notepad .\pg_hba.conf

Option B: Using VS Code (if installed)

code .\pg_hba.conf

4️⃣ Temporarily Allow Passwordless Login

Inside pg_hba.conf, change all scram-sha-256 to trust like this:

local   all             all                                     trust
host    all             all             127.0.0.1/32            trust
host    all             all             ::1/128                 trust

local   replication     all                                     trust
host    replication     all             127.0.0.1/32            trust
host    replication     all             ::1/128                 trust

💾 Save the file and close the editor

5️⃣ Restart PostgreSQL

Restart-Service -Name postgresql-x64-17

6️⃣ Connect to PostgreSQL Without a Password

psql -U postgres -d postgres

You’ll enter psql without being asked for a password.

7️⃣ Reset the Password

Inside the psql prompt (postgres=#), run:

ALTER ROLE postgres WITH PASSWORD 'YourNewStrongPassword';

Replace 'YourNewStrongPassword' with something secure.

8️⃣ Restore Authentication to scram-sha-256

Edit pg_hba.conf again and revert changes:

local   all             all                                     scram-sha-256
host    all             all             127.0.0.1/32            scram-sha-256
host    all             all             ::1/128                 scram-sha-256

local   replication     all                                     scram-sha-256
host    replication     all             127.0.0.1/32            scram-sha-256
host    replication     all             ::1/128                 scram-sha-256

💾 Save and close.

9️⃣ Restart PostgreSQL Again

Restart-Service -Name postgresql-x64-17

🔟 Final Test

Try logging in again:

psql -U postgres -d postgres

✅ You’ll be prompted for a password — use the new one you set. You're back in!

🧼 Tips for Clean Setup

  • Store passwords securely in a password manager
  • Don't leave pg_hba.conf in trust mode for long — it's insecure
  • Use a .env file for apps connecting to PostgreSQL instead of hardcoding passwords

🧰 Optional: VS Code Integration

If you're using VS Code:

  • Open the data folder directly:

    code "C:\Program Files\PostgreSQL\17\data"
    
  • Install SQL extensions in VS Code to make editing SQL scripts easier

🆘 Troubleshooting

ProblemSolution
psql: password authentication failedPassword is wrong or pg_hba.conf still requires scram-sha-256
psql not recognizedMake sure PostgreSQL's bin folder is in your system PATH
"Connection refused"PostgreSQL service is not running — restart it