Django cannot connect to database in container

I have created docker-compose file

version: "3"

services:
  web:
    build: .
    command: bash -c "python manage.py migrate && python manage.py runserver 0.0.0.0:8000"
    ports:
      - 8000:8000
    depends_on:
      - postgresql
    volumes:
      - .:/app
  postgresql:
    image: postgres:latest
    environment:
      POSTGRES_DB: pgdb
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: admin
    ports:
      - "5432:5432"

and when I run compose up, my db runs successfully, but eventually container with django cannot connect to my postgresql

setting of project:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        # 'NAME': 'diary',
        # 'USER': 'postgres',
        # 'PASSWORD': 'admin',
        # 'HOST': 'localhost',
        # 'PORT': '5432'

        'NAME': os.getenv("DB_NAME"),
        'USER': os.getenv("DB_USER"),
        'PASSWORD': os.getenv("DB_PASSWORD"),
        'HOST': os.getenv("DB_HOST"),
        'PORT': os.getenv("DB_PORT")
    }
}

dockers logs :

2025-01-13 21:35:18 web-1         | django.db.utils.OperationalError: connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed: No such file or directory
2025-01-13 21:35:18 web-1         |     Is the server running locally and accepting connections on that socket?

what can cause such error? Even tho in previous work with same properties all worked correctly

Вернуться на верх