Fix: psql peer authentication failed for user

PostgreSQL is a relational database management system (RDMS), also known as an extendable form of SQL, and the relational database management system is used to manage the web and mobile applications’ data in structured tables. PostgreSQL’s popularity is its automatically updatable views and stored procedures.

Different operating systems, including Linux distributions, support PostgreSQL. You can explore more about the installation of PostgreSQL on Ubuntu by visiting the mentioned link.

It is observed by the users that sometimes they have to face errors while using PostgreSQL. One of the errors mostly faced by the users is “psql Peer authentication failed for user,” and this blog is particularly the explanation of this error. This blog will find the reason(s) and all possible solution(s) to resolve this error. The blog’s contents are:

Fix: psql Peer authentication failed for user

From the name of the error, it is easy to figure out that this error is associated with the authentication process. When the users try to connect with the “PostgreSQL user,” they may face such types of errors.

For example, we are trying to connect with the “mydatabase” of the “John” user, and the error message appears on the screen:

$ psql mydatabase John

The reason behind this error has been explained in the next section.

Reason: Restriction by Default Rules

There are different rules to access the database in PostgreSQL, which are set in the configuration file of PostgreSQL. The configuration file of PostgreSQL is with the name “pg_hba.conf” and its location can be found with the command:

$ locate pg_hba.conf

Note: To use the locate command in Ubuntu, you have to install the “plocate” package:

$ sudo apt install placate -y

By locating the location of the pg_hba.conf file, we find the location “/etc/postgresql/14/main/pg_hba.conf”. We can display the file’s default rules using the command:

$ sudo cat /etc/postgresql/14/main/pg_hba.conf

Scrolling down the scroll bar, we will find the script similar to the one below:

We found out that by default, the rules are set to the “peer,” and the three different rules are:

peerIt will not ask for the password. The user should be only “Postgres
md5It will prompt a message to enter the password
trustIt will allow all the connections without asking for passwords

When the rule is set to the “peer” then it will not allow any other user to log in the PostgreSQL other than the “Postgres”.

Solution: Change the Rules

The solution to the “psql Peer authentication failed for user” is simple: either use the “Postgres” user or change the rules. To change the rules, open the configuration file of PostgreSQL with the nano text editor:

$ sudo nano /etc/postgresql/14/main/pg_hba.conf

Scroll down the file until you find the “# “local” is for Unix domain socket connections only” with the default set rule “peer”:

Replace “peer” with the new set rule “md5”:

Then save the file by using the CTRL+S and then close the nano text editor with the CTRL+X shortcut key of nano text editor.

To implement the new changes, the service of the psql must be restarted:

$ sudo systemctl restart postgresql

Now again try to run the command to access the “mydatabase” with the user “John”:

$ psql mydatabase John

It will ask for the password of user John:

After providing the correct password, you will be logged in to the “mydatabase” without facing any error.


To fix “psql Peer authentication failed for user”, either you have to log in with the “Postgres” user or change the default rule from “peer” to “md5”. The method of changing the rules in the configuration file of PostgreSQL has been explained in this blog. Also, all three rules are explained with their applications.