7

I ran these lines from "python manage.py shell":

from django.db import connection
cursor = connection.cursor()

But got the following error:

Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/usr/local/lib/python2.7/dist-packages/django/db/backends/__init__.py", line 306, in cursor
    cursor = self.make_debug_cursor(self._cursor())
  File "/usr/local/lib/python2.7/dist-packages/django/db/backends/postgresql_psycopg2/base.py", line 177, in _cursor
    self.connection = Database.connect(**conn_params)
  File "/usr/lib/python2.7/dist-packages/psycopg2/__init__.py", line 179, in connect
    connection_factory=connection_factory, async=async)
OperationalError: FATAL:  role "jay" does not exist

In settings.py I have

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2', 
        'NAME': 'mysite',                     
        'USER': '',                     
        'PASSWORD': '',                  
        'HOST': '',                      
        'PORT': '',                     
    }
}

What am I doing wrong here? I installed postgresql and the adapter

2 Answers 2

11

FATAL: role "jay" does not exist

Since USER is blank, it's using whatever your local username is (jay), and you haven't created a PostgreSQL user by that name.

Try:

psql -u postgres createuser -P jay

You'll probably have to put the password you enter at the prompt into settings.py, unless your pg_hba.conf is using ident as the auth method.

Sign up to request clarification or add additional context in comments.

2 Comments

on 9.4beta3 on Debian, the command would be: sudo su - postgres -c "createuser -P jay" there's surely a way to do it with psql as well, but the -u should be -U and I was getting other errors.
@jcomeau_ictx You never need sudo su -. Ever. Use sudo -u postgres instead. Same thing, no need for the messy quoting.
1

Since you haven't provided a username, postgresql is using the username of the user of your operating system under which django is being run to connect to the database. The error that you are getting says that that user (jay) is not defined in the database.

A possible solution is to provide the name of the username and password of the owner of the mysite database. Add also localhost as the host to ensure that postgresql uses user/password authentication instead of using the username that owns the django process.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.