Change password of postgres account in Postgres

Summary

To change an unknown default password for the `postgres` account on a Windows installation, begin by modifying the `pg_hba.conf` file. Temporarily update the authentication method for local IPv4 connections (127.0.0.1/32) from `md5` to `trust`, enabling passwordless access. Then, log in using `psql -U postgres` and execute `ALTER USER postgres WITH PASSWORD 'your_new_password';` to set the desired password. Conclude by reverting the `pg_hba.conf` entry back to `md5` and restarting the PostgreSQL service to restore secure, password-based authentication.

When installing Postgres on Windows, there is some default account created for user to login. One of them is postgres, but we often don;t know what's the password for this account when we first login using this account. We need to change the password for this account. How to change it?

Step 1. Modify the pg_hba.conffile

Go to the /data/ and open the pg_hba.conf.

# TYPE  DATABASE        USER            ADDRESS                 METHOD

# IPv4 local connections:
host    all             all             127.0.0.1/32            md5

For the method, the default is md5 which means it needs a password, so here we need to change the method to trust and then save it

host    all             all             127.0.0.1/32            trust

This means user can login without any password.

Step 2 . Login to Postgres server

Once the above is done, we can login to Postgres using psql command(Postgres client) to login:

psql -U postgres

Then we can enter the psql command line mode, now we can execute the below SQL command:

ALTER USER postgres WITH PASSWORD '';

Once the above is executed, the password is changed

Step 3. Revert the login method

Now modify the pg_hba.conf file and change the method back to md5 and save it and restart the Postgres service.

Now you can using the new password to login now.

PASSWORD POSTGRES USER ACCOUNT

  RELATED

  COMMENTS

0

No comment for this article.