Django setting : psycopg2.OperationalError: FATAL: Peer authentication failed for user "indivo" Django setting : psycopg2.OperationalError: FATAL: Peer authentication failed for user "indivo" python python

Django setting : psycopg2.OperationalError: FATAL: Peer authentication failed for user "indivo"


I have similar problem and solved it with this answer by adding localhost to the database HOST settings in settings.py, so your database settings should look like this:

DATABASES = {    'default':{        'ENGINE':'django.db.backends.postgresql_psycopg2', # '.postgresql_psycopg2', '.mysql', or '.oracle'        'NAME':'indivo', # Required to be non-empty string        'USER':'indivo', # Required to be non-empty string        'PASSWORD':'ritvik',        'HOST':'localhost', # <- Changed from empty string to localhost        'PORT':'', # Set to empty string for default.        },}


By default in many Linux distros, client authentication is set to "peer" for Unix socket connections to the DB. This is set in the pg_hba.conf config file for postgresql. The psycopg2 python library documentation states:

- *host*: database host address (defaults to UNIX socket if not provided)

So if you leave the host option blank, your script will try to connect and use the Unix username:password for authentication. To fix, you can either:

  1. set the "host" option explicitly to 127.0.0.1 into the config code you pasted into your question.
  2. Modify the pg_hba.conf file to use md5 for socket connections so it uses usernames and passwords stored in the postrgres DB user store (not recommended as this may break authentication for system users)


You need to set pg_hba.conf to use md5 authentication for the user, db and source IP of interest. See the client authentication chapter of the documentation.

Search for pg_hba.conf on Stack Overflow for tons more information.