How to reset the password for postgres in Postgres

How to reset the password for the postgres user inside PostgreSQL in case Kevin has left the building and deleted all the passwords.

All commands are executed in a shell connected to the PG server. It is running RHEL.

Enable trusted login

This will allow you to log in locally without specifying a password. Be aware that the folder for your pg_hba.conf may be different. Also, the password you set for the postgres PostgreSQL user has to be the same password as the one used by the postgres linux user. If you do not know that password either, you can reset it from the OS using

sudo passwd postgres

Then…

sudo su -
nano /var/lib/pgsql/data/pg_hba.conf
  • Change the Method to “trust” for local connections. Remember what it was before you changed it.
  • Save the file

Reload the config and log in without a password

systemctl reload postgresql
exit
sudo -u postgres psql

Change the passord

ALTER USER postgres PASSWORD 'new password';
\q

Revert the login method change

  • Go back to pg_hba.conf and revert “trust” to whatever it was before. Preferably scram-sha-256.
  • Reload the Postgres config again

Test psql

sudo -u postgres psql

Now, whether or not you should ever use the postgres user is another story.