1

I am currently working my way through this tutorial on how to make a basic blog using Django, but I wanted to use a PostgreSQL database instead of a sqlite database. Django does not auto-create databases for non sqlite, and I am having trouble creating mine. I thought I had done it properly, but whenever I try to sync my DB, I see a bunch of filepaths and then the error

"django.db.utils.OperationalError: FATAL:  role [username] does not exist"

I have tried googling the problem and trying a few of the solutions I found, however none of them seem to be working.

Thanks in advance for any guidance you can provide.

1
  • 4
    DId you create a user in your postgres database for django to use? Commented Jun 24, 2015 at 20:42

2 Answers 2

5

Having encountered this from a beginner perspective in the past, I'll go more in-depth -- first, a note on how to read the Django errors:

django.db.utils.OperationalError

means that there was an error in django’s database utility (the bits that connect up django to different types of DBs like MySQL, Postgres, and SQLite), hence the db.utils part. OperationalErrors are things that are returned from the database itself when you try to run something, which means that the you can isolate Django and focus on the database interaction itself. If Django didn’t exist and you ran the same commands through psql (postgres’ command line utility), you’d still see:

FATAL: role [username] does not exist

Also you generally can tell it’s an error from the database because of the all caps. Relational databases LOVE CAPS LOCK.

As far as solving it, the error is pretty self-descriptive. The thing you need to look at is your settings.py file (as kmmnbvnr pointed out). In your database configuration, you should see a username and password. Postgres is complaining that you are trying to connect to the configured DB with a user that doesn’t exist, so you need to create it. Here are the commands using PSQL instead of the sudo commands kmmbvnr used:

Connect to the database with the command line utility:

$ psql -d postgres

If you created a database that was not the default postgres, use that instead for the database name,

i.e. $ psql -d my_database_name

Edit: On certain OSs you may need to add a -U <database user> if it fills in your OS username and not the default "postgres" username.

i.e. $ psql -d postgres -U postgres (postgres is the default user).

CREATE ROLE testrole WITH LOGIN ENCRYPTED PASSWORD 'password';

ALTER ROLE testrole WITH CREATEDB;

grant all privileges on database postgres to testrole;

Change testrole and password to whatever you’d like, so long as they match the username/password in settings.py. Do note the ' ' around the password, but not around the username. Those are important. Also note that Postgres frowns upon using the builtin ‘postgres’ user for non-administrative tasks for security reasons, so it’s best to create another user to use with Django. Also, the last lines about granting permissions are a bit permissive, but for a local test configuration, it’ll be your easiest bet. Consult the Postgres docs on privileges for production environments / proper security.

Lastly, you can test, independent of django, by consulting the man page on PSQL (man psql on linux / mac) on how to login with a specific user with the command line. I believe the command is:

$ psql -d postgres -U testrole -W

Where the -d is specifying the database (i.e. mydatabase or postgres), the -U is the user to connect with, and the -W means prompt for a password.

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

Comments

1

Assuming you are on a linux host

sudo su postgres
createuser myuser -P
createdb -O myuser mydb

And ensure that you have in settings.py

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

}

1 Comment

For other OS host you can check this tutorial - djangogirls.gitbooks.io/django-girls-tutorial-extensions/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.