40

I am getting problem in Django project setting with POSTGRESQL.

Here is my setting.py database setting

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':'', # Set to empty string for localhost.
        'PORT':'', # Set to empty string for default.
        },
}

Now in postgres backend what I have done is .

rohit@rohit-desktop:~$ sudo su - postgres
postgres@rohit-desktop:~$ createuser --superuser indivo   # create a super user indivo
postgres@rohit-desktop:~$ psql  # open psql terminal 
psql (9.1.8)
Type "help" for help.

postgres=# \password indivo  # set the password ritvik
Enter new password: 
Enter it again: 
postgres=# \q   #logout 
postgres@rohit-desktop:~$ createdb -U indivo -O indivo indivo  #create db indivo 

Unfortunately when i am trying to syncdb I am getting the error .

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

Please help me out what might I am doing wrong here .

juliocesar
  • 5,706
  • 8
  • 44
  • 63
masterofdestiny
  • 2,751
  • 11
  • 28
  • 40
  • Duplicate question answered here : [Django connection to PostgreSQL: “Peer authentication failed”][1] [1]: http://stackoverflow.com/questions/8167602/django-connection-to-postgresql-peer-authentication-failed – user3404455 Jul 22 '14 at 23:59

4 Answers4

48

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.
        },
}
Eric Palakovich Carr
  • 22,701
  • 8
  • 49
  • 54
juliocesar
  • 5,706
  • 8
  • 44
  • 63
  • 7
    Thanks for that answer, it also worked for me; despite the fact that the comment says `# Set to empty string for localhost.` :( – contradictioned Jan 09 '14 at 18:53
26

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)
queso
  • 1,110
  • 8
  • 6
  • 2
    Maybe add the info, that you'll have to restart the postgresql server after editing the ``pg_hba.conf``: ``sudo service postgresl restart`` – Tobias Lorenz Mar 10 '15 at 10:47
6

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.

Craig Ringer
  • 307,061
  • 76
  • 688
  • 778
  • please help me how to set in postgresql 9.0 – masterofdestiny Apr 04 '13 at 08:06
  • 2
    @masterofdestiny Honestly, you really should read the manual. It covers all this in depth. On Ubuntu `pg_hba.conf` is in `/etc/postgresql/9.1/main/pg_hba.conf`. Otherwise ... it's all the manual, read [the docs on `pg_hba.conf`](http://www.postgresql.org/docs/current/static/auth-pg-hba-conf.html). – Craig Ringer Apr 04 '13 at 08:08
  • This line did it for me: host all all 127.0.0.1/32 md5 – Harlin Apr 30 '16 at 13:45
0

I too was facing similar problem during setting up postgresql database with Django project.

My System is running RHEL CentOS 7 with postgresql version 10. I search a lot but couldnot find actually what is happening until I saw log at /var/lib/pqsql/10/data/log/postgresql*.log

2017-11-10 00:31:40.499 IST [14129] DETAIL: Connection matched pg_hba.conf line 85: "host all all ::1/128 ident"

So I just changed that particular line in file /var/lib/pgsql/10/data/pg_hba.conf : from host all all ::1/128 ident

to

host all all ::1/128 md5

with that I am able to create database & table using manage.py migrate

Thanks for all who has helped me to find this solution. especially @juliocesar and official documentation for postgresql client authentication

Gopal Prasad
  • 1,887
  • 2
  • 12
  • 12