ThatSoftwareDude
Developer Tools, Guides and Articles

Menu

How to Reset Your Forgotten PostgreSQL Password on Windows

How to Reset Your Forgotten PostgreSQL Password on Windows

Forgetting the default root password that you set up 6 months ago in your PostgreSQL database installation is a part of being a developer. I myself, tend to do this maybe twice a year. Particularly when setting up a new laptop.

But there is a quick fix, if you're on Windows and I will walk you through the process that has worked for me since I figured it out.

Edit your '' file

Start by locating the pg_hba.conf file in your Postgres installation folder. The file path should look something like the following:

C:\Program Files\PostgreSQL\16\data

If you have multiple versions of PostgreSQL installed, be sure to check that you're in the right version folder.

Open the pg_hba.conf file in a plain text editor and you'll need to edit the authentication method for the local type from the following:

local all        postgres               peer or scram-sha-256

to the following:

local all        postgres               trust

This change allows anyone to log in to this particular user account. But we're only doing it temporarily and we'll revert back when finished. Save the changes and close the file.

Restart your Postgres service

Type the Winkey + R shortcut and enter services.msc in order to launch the Windows Services management tool and locate the PostgreSQL service running on your machine.

Windows Services panel

Click restart service and wait until it finishes. Once the service restarts you should be able to login without needing to enter a password.

Login without a password

In your command prompt, type the following:

psql -U postgres

You should not be asked for a password at this point and be logged in to the user instance.

Change your password

In order to change the password for that specific user instance, type the following command:

ALTER USER postgres PASSWORD 'new password goes here'

Revert back your '' file

Once you have successfully reset your password, edit the pg_hba.conf file back to its original trust levels. And you should be good to go.

Troubleshooting tips

  • If you don't see a 'postgres' user entry in your pg_hba.conf file, then it's likely that the default user for local connections is handled by the 'all' entry.
  • Be sure to close the pg_hba.conf file after making edits, as the OS might not pick up the changes if left open.
  • If you've made all the changes listed and you still can't login without a password, check to make sure that you are editing the right version installation.
Walter G. author of blog post
Walter Guevara is a Computer Scientist, software engineer, startup founder and previous mentor for a coding bootcamp. He has been creating software for the past 20 years.

Get the latest programming news directly in your inbox!

Have a question on this article?

You can leave me a question on this particular article (or any other really).

Ask a question

Community Comments

No comments posted yet

Add a comment