How To Fix - FATAL: Peer authentication failed for user "postgres" Error

1 min read

Peer authentication failed error arrives when you try to login to your PostgreSQL user but authentication fails because by default psql connects over UNIX sockets using peer authentication instead of password authentication.

In this article we will learn how to get rid of FATAL: Peer authentication failed for user "postgres" error by enforcing password authentication over Unix sockets peer method,

First, navigate to the /etc/postgresql/10/main directory.

cd /etc/postgresql/10/main

Note that 10 is the PostgreSQL version it can be different for you.

Here resides the pg_hba.conffile we need to do some changes here you may need sudo access for this.

sudo nano pg_hba.conf

Scroll down the file till you find this -

# Database administrative login by Unix domain socket
local   all             postgres                                peer

Here change the peer to md5 as follows.

# Database administrative login by Unix domain socket
local   all             postgres                                md5
  • peer means it will trust the authenticity of UNIX user hence does not prompt for the password.
  • md5 means it will always ask for a password, and validate it after hashing with MD5.

Now save the file and restart the Postgres server.

sudo service postgresql restart

And that's it!

Try to log in now.

sudo -u postgres psql
Password:
postgres=#

WEB DEVELOPMENT

Latest Articles

Latest from djangocentral

Capturing Query Parameters of request.get in Django

In Django, the request object contains a variety of information about the current HTTP request, including the query parameters. Query parameters are a way to pass additional information in the URL and are used to filter or sort data. The request object p…
Read more →

2 min read

Understanding related_name in Django Models

In Django, related_name is an attribute that can be used to specify the name of the reverse relation from the related model back to the model that defines the relation. It is used to specify the name of the attribute that will be used to access the relat…
Read more →

2 min read