DEV Community

Cover image for How to Reset PostgreSQL Password
Syeda Maham Fahim
Syeda Maham Fahim

Posted on • Edited on

How to Reset PostgreSQL Password

I'm writing this article because I recently ran into an issue with PostgreSQL where I had to reset the password. The steps are simple, but if you’re new to PostgreSQL or running it on Windows, it might get tricky. So, let’s break it down into clear steps that are easy to follow.

Step 1: Stop the PostgreSQL Service

First, we need to stop the PostgreSQL service before we can reset the password. Here’s how you do that:

  • Press Win + R, type services.msc, and hit Enter.

Win + R

  • Look for PostgreSQL in the list.
  • Right-click on it and choose Stop.

postres

Step 2: Start PostgreSQL in Single-User Mode

Now, we’ll start PostgreSQL in single-user mode. This mode allows us to reset the password without needing the current one.

  • Open Command Prompt as an administrator.

    To do this, press Win + S, type cmd, right-click on Command Prompt, and choose Run as administrator.

  • Now, navigate to the PostgreSQL bin directory where the executable is located. Typically, it’s in:

  cd "C:\Program Files\PostgreSQL\16\bin"
Enter fullscreen mode Exit fullscreen mode
  • After that, run PostgreSQL in single-user mode with this command:
  postgres --single -D "C:\Program Files\PostgreSQL\16\data" postgres
Enter fullscreen mode Exit fullscreen mode
  • Note: Replace "C:\Program Files\PostgreSQL\16\data" with your actual data directory path if it’s different.

Step 3: Reset the Password

Once PostgreSQL starts in single-user mode, you’ll see a prompt where you can run SQL commands. To reset the password for the postgres user, use this command:

ALTER USER postgres WITH PASSWORD 'your_new_password';
Enter fullscreen mode Exit fullscreen mode
  • Replace 'your_new_password' with the new password you want to set.

After entering the command, press Enter to reset the password.

Step 4: Restart the PostgreSQL Service

After resetting the password, exit the single-user mode by closing the Command Prompt window. Then, go back to the Services tool:

  • Right-click on PostgreSQL and choose Start to restart the service.

Step 5: Reconnect with pgAdmin

Finally, open pgAdmin 4 (or any other PostgreSQL client you use) and try connecting with the new password.

If it still doesn’t work, you may need to update the saved password:

  • Right-click on the server in the Object Browser.
  • Choose PropertiesConnection.
  • Enter the new password and click Save.

Simplified Process (For Quick Reference)

In simpler terms, here’s what you need to do:

  1. Stop PostgreSQL: Go to services.msc and stop PostgreSQL.
  2. Open Command Prompt as Admin: Search for cmd, right-click, and run as administrator.
  3. Navigate to PostgreSQL Folder: Use this command:
   cd "C:\Program Files\PostgreSQL\16\bin"
Enter fullscreen mode Exit fullscreen mode
  1. Run PostgreSQL in Single-User Mode:
   postgres --single -D "C:\Program Files\PostgreSQL\16\data" postgres
Enter fullscreen mode Exit fullscreen mode
  1. Reset the Password:
   ALTER USER postgres WITH PASSWORD 'your_new_password';
Enter fullscreen mode Exit fullscreen mode
  1. Restart PostgreSQL: Go back to Services and start PostgreSQL again.
  2. Reconnect with pgAdmin: Use the new password to connect.

Final Thoughts

I hope this guide makes it easy for you to reset your PostgreSQL password on Windows using single-user mode. It’s a lifesaver when you’re locked out of your database.

- - - - - - - - - - - - - - - - - - - - - 
Stay connected - @syedamaham.dev 🐬
- - - - - - - - - - - - - - - - - - - - -

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Immerse yourself in a wealth of knowledge with this piece, supported by the inclusive DEV Community—every developer, no matter where they are in their journey, is invited to contribute to our collective wisdom.

A simple “thank you” goes a long way—express your gratitude below in the comments!

Gathering insights enriches our journey on DEV and fortifies our community ties. Did you find this article valuable? Taking a moment to thank the author can have a significant impact.

Okay