skip to Main Content

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


  1. It’s possible that the issue is related to the PostgreSQL user and password not being properly set during the container initialization.

    environment:
      - POSTGRES_DB=${POSTGRES_NAME}
      - POSTGRES_USER=${POSTGRES_USER}
      - POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
    
    Login or Signup to reply.
  2. Check if you have an already running service on port 5432, like an old local PostgreSQL instance (use lsof -i:5432). If not, try deleting the postgres_data of your PostgreSQL Docker database and start it again.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search