- 1 year ago
Forgot Admin Password on Postgres (Windows Installation), can't reset
Comment
1 / 1
If you've forgotten the administrative password for PostgreSQL on a Windows installation, you'll need to perform a few steps to reset it. Here's how you can do it:
Stop PostgreSQL Service:
- Open the Services window by pressing Win + R, typing
services.msc
, and hitting Enter. - Find the PostgreSQL service, which is usually named "postgresql-x64-<version>" or something similar.
- Right-click on the service and select "Stop."
- Open the Services window by pressing Win + R, typing
Modify pg_hba.conf:
- Navigate to the PostgreSQL data directory. This is usually located at
C:\Program Files\PostgreSQL\<version>\data
. - Open the
pg_hba.conf
file in a text editor. - Look for a line that resembles:css
host all all 127.0.0.1/32 md5
- Change
md5
totrust
for this line. This will allow you to connect without a password temporarily. Save the file.
- Navigate to the PostgreSQL data directory. This is usually located at
Restart PostgreSQL Service:
- Go back to the Services window.
- Start the PostgreSQL service.
Connect to PostgreSQL:
- Open a command prompt with administrative privileges.
- Navigate to the PostgreSQL
bin
directory, usually located atC:\Program Files\PostgreSQL\<version>\bin
. - Run the following command to connect to the PostgreSQL database as the superuser (postgres):
psql -U postgres
Change Password:
- Once connected, you can change the password for the postgres user using the following SQL command:sql
ALTER USER postgres PASSWORD 'new_password';
- Once connected, you can change the password for the postgres user using the following SQL command:
Restore pg_hba.conf:
- Go back to the
pg_hba.conf
file and change the modified line fromtrust
back tomd5
.
- Go back to the
Restart PostgreSQL Service:
- Restart the PostgreSQL service from the Services window.
Test Connection:
- Try connecting to PostgreSQL using the new password to ensure everything is working as expected.
Remember to keep your password secure and ensure that you have a way to recover it in the future without compromising security.
Please note that these steps are provided as general guidance, and you should always follow best practices for securing your database. If you're not comfortable performing these steps, consider seeking assistance from someone with experience in PostgreSQL administration.