1

I am learning to user PostgreSQL and Django together in Ubuntu 11.10, and I have found that I need to switch to the user I created when I installed PostgreSQL ("postgres") in the Terminal (via "sudo su postgres") in order to create and then access databases to work with in Django. Unfortunately, the "postgres" user doesn't have file writing privileges on my local file system, so when I try to do certain things like add model objects to the database that have an image field, I'm blocked. But if I switch to my normal Ubuntu user name in the Terminal and try to access my admin site on Django's dev server, I get an error like this:

OperationalError at /admin/

FATAL:  Peer authentication failed for user "postgres"

Request Method:     GET
Request URL:    http://127.0.0.1:8000/admin/
Django Version:     1.3.1
Exception Type:     OperationalError
Exception Value:    

FATAL:  Peer authentication failed for user "postgres"

Exception Location:     /usr/local/lib/python2.7/dist-packages/django/db/backends/postgresql_psycopg2/base.py in _cursor, line 140
Python Executable:  /usr/bin/python
Python Version:     2.7.2

I'm trying to read up on the PostgreSQL docs to give my normal user access to the databases I've created for my Django projects, but I can't figure it out because I don't understand the relationship between PostgreSQL roles/users and Ubuntu users. Could someone please explain to me how I can give my normal Ubuntu username access to my databases in PostgreSQL? I will probably need a specific list of instructions as I've tried to piece it together using the PostgreSQL docs and I'm totally lost.

cfedermann
  • 3,286
  • 19
  • 24
GChorn
  • 1,267
  • 1
  • 19
  • 36
  • 1
    Possible duplicate of http://stackoverflow.com/questions/8167602/django-connection-to-postgresql-peer-authentication-failed – cfedermann Apr 17 '12 at 07:08
  • It's not clear what you're really trying to do here. If an object is managed by the database engine, you normally want to access that object through your database connection, not directly. What are you trying to do when you get this error? – kgrittn Apr 17 '12 at 11:57

2 Answers2

9

cfedermann was correct; this is a duplicate of the question at:

Django connection to PostgreSQL: "Peer authentication failed"

and the solution is the same. In the 'settings.py' file for my Django project, under the DATABASES section, my 'HOST' was set to an empty string because there is a built-in comment there (meaning, a comment written by a Django developer) that literally says, 'Set to empty string for localhost.' Apparently this isn't true, as only after I explicitly entered 'localhost' was I able to connect to my psql database under my default Linux username.

I guess the lesson here is, to all you Django other newbies out there trying to use it with postgresql in development, make sure you enter in 'LOCALHOST' in your 'settings.py' file, or you won't be able to connect to a psql database stored on your own machine!

Thanks again cfedermann for pointing me to the answer.

Community
  • 1
  • 1
GChorn
  • 1,267
  • 1
  • 19
  • 36
1

Postgres ident notes

Your normal user may not be allowed to login from local host. If your django project is running on the same machine as your PG database then you need to check the pg_ident.conf file.

Basically you can set postgres so that your unix user is automatically mapped to a postgre user so when you run pgsql it attempts to log you in. You can block a user from logging in on an external IP, or from local host. So check these settings first before fiddling with your Django settings.

ELSE try this

okay you should probably do this

sudo passwd postgres 

Enter a password for the unix postgres user then

su postgres
psql
create database djangodb;
create role djangouser with password 'something';

Try those user details in your settings.py file. More on postgres here

ALSO

Ident methods and The pg_hba.conf file

This helps you yes?

nialloc
  • 805
  • 7
  • 17
  • Hi nialloc, thanks for the tip. Unfortunately when I open up pg_indent.conf to get a look at what's in it (using sudo gedit /etc/postgresql/9.1/main/pg_indent.conf), all that comes up is an empty file. Do I need to open it up some other way to see what's inside? – GChorn Apr 18 '12 at 12:43
  • Thanks for the extra clarification, but if you look below you'll see I answered my own question after realizing it really was a duplicate as suggested by cfedermann. Thanks again though! – GChorn Apr 20 '12 at 16:33