How to Fix FATAL: password authentication failed for user “postgres” in ubuntu 20.4
After a fresh installation of PostgreSQL on my machine, I was frustrated by the fact that it didn’t work seamlessly with my Django application which works perfectly on my other machine.
On running the machine I kept getting FATAL: password authentication failed for user “postgres” error, and after googling for some time I realized that most of the fixes were for one to instal pg_admin yet this seemed not right for me to add yet another installation.
So decided to dig deep to find a terminal fix to the issues. I’m pleased to share it here.
So when you run
psql -U postgres
psql: error: FATAL: Peer authentication failed for user “postgres”
To fix this run
sudo -i -u postgres
xxx@xxxx:~$ psql
psql (13.3 (Ubuntu 13.3–1.pgdg20.04+1))
Type “help” for help.
You then alter postgres user to add password postgres
postgres=# ALTER USER postgres PASSWORD ‘postgres’;
ALTER ROLE
Then exit from postgres back to the terminal user
postgres=# \q
xxxx@xxxx:~$ exit
logout
After this run sudo
sudo vi /etc/postgresql/13/main/pg_hba.conf
to change database administrative login by socker peer to md4 and any other instances of peer
Exit vim and restart postgres service with
sudo service postgresql restart
Then run
psql -U postgres
and your good to go.
Thanks