Error Message: “Peer authentication failed for user ‘username’” when connecting to a PostgreSQL database from a Django application
Error Message:
psycopg2.OperationalError: FATAL: Peer authentication failed for user “username”
I was setting up a friend’s project on my Ubuntu 12.04 laptop last night and ran into this database connection problem when I tried to ran his Django application.
There are actually 2 solutions to this problem:
Solution 1:
In your settings.py file, in the database settings, set the ‘HOST’ to ‘localhost’ instead of an empty string.
Solution 2:
Modify the /etc/postgresql/9.1/main/pg_hba.conf file and change the authentication setting of local from peer to md5.
Peer authentication basically uses the currently logged in user’s credentials to authenticate, which in my case doesn’t have access to the database.
MD5 authentication is password-based authentication and will use the username and password you specified in the settings file.
Tags: security, tech, database, postgresql