Support Our Site

To ensure we can continue delivering content and maintaining a free platform for all users, we kindly request that you disable your adblocker. Your contribution greatly supports our site's growth and development.

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

1 min read

Table of Contents

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 socketlocal   all             postgres                                peer

Here change the peer to md5 as follows.

# Database administrative login by Unix
 domain socketlocal   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 psqlPassword:postgres=#


WEB DEVELOPMENT

Latest Articles

Latest from djangocentral

How to Use Subquery() in Django With Practical Examples

In the realm of web development, Django stands as a powerful and versatile framework for building robust applications. One of the key aspects of developing efficient and optimized web applications is handling database queries effectively. In this article…
Read more →

4 min read

DRF Serializer: Handling OrderedDict and Converting It to a Dictionary or JSON

In Django Rest Framework (DRF) tests, when you access serializer.data, you might encounter an OrderedDict instead of a regular dictionary. This behavior is intentional and reflects the design of DRF's serialization process.Understanding the Problem The u…
Read more →

3 min read

Django Rest Framework CheetSheet: Mastering API Development

Django Rest Framework (DRF) is a powerful toolkit that makes building robust and scalable web APIs with Django a breeze. Whether you're a seasoned Django developer or a newcomer, having a comprehensive cheat sheet at your disposal can be a game-changer. …
Read more →

5 min read

How to Perform NOT Queries in Django ORM

In Django, performing NOT queries allows you to exclude certain records from the query results based on specific conditions. The NOT operator, represented by the tilde (~) when used in conjunction with the Django ORM's Q object, helps you construct compl…
Read more →

3 min read