Postgresql – Retrieve or reset the password for PostgreSQL on Linux

linuxpasswordpostgresql

I have installed Postgres on my Linux system. I forgot the admin password I want to reset the password. How can I do that?

Best Answer

  1. If you want to reset your password you need to go to the pg_hba.conf file. You can do this by giving in the following command into Linux.
sudo subl /etc/postgresql/12/main/pg_hba.conf
  1. After gaining access to the file you must change all local locations from Method md5 to trust so you can enter Postgresql without permission or without being asked for a password. Then place the following command into Linux:
sudo /etc/init.d/postgresql restart
  1. Subsequently, run the succeeding command to get into PostgreSQL:
sudo -u postgres psql
  1. And last but not least run the common SQL Command to reset the password for User:
ALTER USER postgres with PASSWORD 'anewremembrablepassword';

Of course, you could also use another user than postgres if you considerably created one. Make sure you go back to the pg_hba.conf file to change the methods back to md5 so you always give in a password as for better security or leave it and never give in a password.

I hope I could be helpful in solving this matter.

Thanks!