Forgot Admin Password on Postgres (Windows Installation), can't reset

  • 1 year ago
Comment

ChatGPT
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:

  1. 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."
  2. 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 to trust for this line. This will allow you to connect without a password temporarily. Save the file.
  3. Restart PostgreSQL Service:

    • Go back to the Services window.
    • Start the PostgreSQL service.
  4. Connect to PostgreSQL:

    • Open a command prompt with administrative privileges.
    • Navigate to the PostgreSQL bin directory, usually located at C:\Program Files\PostgreSQL\<version>\bin.
    • Run the following command to connect to the PostgreSQL database as the superuser (postgres):
      psql -U postgres
  5. 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';
  6. Restore pg_hba.conf:

    • Go back to the pg_hba.conf file and change the modified line from trust back to md5.
  7. Restart PostgreSQL Service:

    • Restart the PostgreSQL service from the Services window.
  8. 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.