How to resolve FATAL: Peer authentication failed for user “postgres”
It might be one of the first times you are setting up a postgres database and then you would have encountered this error when you try to log in to the database for the first time. This error message can seem pretty surprising since you must have followed all the steps outlined in a guide to the T. Well, here is why you faced this error.
The connection failed because by default psql connects over UNIX sockets using peer authentication, that requires the current UNIX user to have the same user name as psql. So you will have to create the UNIX user postgres and then login as postgres or use sudo -u postgres psql database-name for accessing the database (and psql should not ask for a password).
If you intend to force password authentication over Unix sockets instead of the peer method, try changing the following pg_hba.conf* line:
FROM
TYPE DATABASE USER ADDRESS METHOD local all all peer
TO
TYPE DATABASE USER ADDRESS METHOD local all all md5
- peer means it will trust the identity (authenticity) of UNIX user. So not asking for a password.
- md5 means it will always ask for a password, and validate it after hashing with MD5.
- trust means it will never ask for a password, and always trust any connection.
Hope you learned something new!