[Fixed]-Django.db.utils.OperationalError: FATAL: role "django" does not exist

15👍

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.

👤Rob

6👍

Refer the link click here. It is explained.

Your settings has

‘USER’: ‘django’,

But as error says that user doesn’t exist , that means you have not created the user.

Just go into interactive session of psql and enter these commands.

CREATE DATABASE agencies;
CREATE USER django WITH PASSWORD 'password';
ALTER ROLE django SET client_encoding TO 'utf8'; 
ALTER ROLE django SET default_transaction_isolation TO 'read committed'; 
ALTER ROLE django SET timezone TO 'Asia/Kolkata';

GRANT ALL PRIVILEGES ON DATABASE agencies TO django;
\q

then in settings.py

‘PASSWORD’: ‘password’,

password should not be enclosed inside < >.

1👍

If you are using Docker, and it’s listening to the 5432 port, you should kill other processes that are listening too.

To do this, type this command to see which processes are using port 5432:

$ lsof -i:5432

This will look like this:

COMMAND     PID           USER   FD   TYPE             DEVICE SIZE/OFF NODE NAME
com.docke 15178 andre.carvalho   86u  IPv6 0xb128ac87cd34cc69      0t0  TCP *:postgresql (LISTEN)
postgres  16670 andre.carvalho    7u  IPv6 0xb128ac87dc50c569      0t0  TCP localhost:postgresql (LISTEN)
postgres  16670 andre.carvalho    8u  IPv4 0xb128ac87d2378541      0t0  TCP localhost:postgresql (LISTEN)

After this, it’s easy: Just kill the other process, with the command:

kill -9 16670

(Note that the process is identified by the PID.)

0👍

I think you forgot to add priveleges to user ‘django’:

GRANT ALL PRIVILEGES ON DATABASE agencies TO django;
👤okay

Leave a comment