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

LaundroMat picture LaundroMat · Dec 2, 2016 · Viewed 15k times · Source

I followed this tutorial on Digital Ocean to install PostgreSQL 9.5 on an Ubuntu 16.04 server to use with Django 1.10.

Everything went smoothly, but I can't get my Django app to connect to the database (or so it seems). App and database are on the same server.

Here are some settings, configs and reports:

The error I get:

File "/home/mathieu/web/agencies/lib/python3.5/site-packages/psycopg2/__init__.py", line 164, in connect
conn = _connect(dsn, connection_factory=connection_factory, async=async)
django.db.utils.OperationalError: FATAL:  role "django" does not exist

My Django project's database settings:

DATABASES = {
'sqlite3': {
    'ENGINE': 'django.db.backends.sqlite3',
    'NAME': os.path.join(BASE_DIR, 'db.sqlite3')
},
'default': {
    'ENGINE': 'django.db.backends.postgresql',
    'NAME': 'agencies',
    'USER': 'django',
    'PASSWORD': '<password>',
    'HOST': 'localhost',
    'PORT': '5432',
}}

The hba_file

postgres=# SHOW hba_file;
hba_file
--------------------------------------
/etc/postgresql/9.5/main/pg_hba.conf

Its contents (well, the relevant part anyway):

# TYPE  DATABASE        USER            ADDRESS                 METHOD

# "local" is for Unix domain socket connections only
local   all             all                                     peer
# IPv4 local connections:
host    all             all             127.0.0.1/32            md5
# IPv6 local connections:
host    all             all             ::1/128                 md5
# Allow replication connections from localhost, by a user with the
# replication privilege.
#local   replication     postgres                                peer
#host    replication     postgres        127.0.0.1/32            md5
#host    replication     postgres        ::1/128                 md5

Users and database in psql

postgres=# \du
                               List of roles
Role name |                         Attributes                         | Member of
-----------+------------------------------------------------------------+-----------
django    |                                                            | {}
postgres  | Superuser, Create role, Create DB, Replication, Bypass RLS | {}


postgres=# \l
                              List of databases
Name    |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges
-----------+----------+----------+-------------+-------------+-----------------------
agencies  | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =Tc/postgres         +
                                                            |  postgres=CTc/postgres+
                                                            |  django=CTc/postgres
postgres  | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |
template0 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
          |          |          |             |             |  postgres=CTc/postgres
template1 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
          |          |          |             |             | postgres=CTc/postgres

I followed the exact same steps on a VM (running Linux Mint, I should say) and all went fine and dandy...

I can't for the life of me figure out what's or where things are going wrong.

Answer

Rob picture Rob · Aug 18, 2018

You may see this error if you have postgres installed both locally (and running) and a docker container both trying to occupy the same port.

If the local instance starts first and is occupying a port that the docker image is also trying to use, docker won't necessarily tell you this.

When you try to run django or other ./manage.py commands that need the database you'll see the same error because the app will not see the database you're looking for.

In this case, you can change the port on your locally installed postgres by stopping the server, clicking Server Settings and changing the port. You'll have to update settings.py on any apps you have depending on that old port.

In my experience, after doing this if you restart the docker db container you won't see this error anymore.