I’m trying to connect my local django project to a Postgres DB container. I am not sure why I get this error "django.db.utils.OperationalError: connection failed: :1), port 5432 failed: FATAL: role "demo" does not exist". Could you please help me figure out where I went wrong?
Here is my .env file:
POSTGRES_USER=demo
POSTGRES_PASSWORD=demo
POSTGRES_NAME=demo
Here is my docker-compose.yml:
volumes:
postgres_data:
services:
db:
image: postgres:15
volumes:
- postgres_data:/var/lib/postgresql/data/
env_file:
- .env
ports:
- "5432:5432"
And here is the databases section of my settings.py:
DATABASES = {
"default": {
"ENGINE": "django.db.backends.postgresql",
"NAME": env('POSTGRES_NAME'),
"USER": env('POSTGRES_USER'),
"PASSWORD": env('POSTGRES_PASSWORD'),
"HOST": "localhost", # set in docker-compose.yml
"PORT": 5432, # default postgres port
}
}
2
Answers
It’s possible that the issue is related to the PostgreSQL user and password not being properly set during the container initialization.
Check if you have an already running service on port
5432
, like an old local PostgreSQL instance (uselsof -i:5432
). If not, try deleting thepostgres_data
of your PostgreSQL Docker database and start it again.