Docker Community Forums

Share and learn in the Docker community.

Error in Dockerizing Django with Postgres and Pgadmin

docker- compose.yml

version: "3.3"
   
services:
  db:
    image: postgres
    environment:
      - POSTGRES_DB=postgres
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=postgres
     # - POSTGRES_HOST_AUTH_METHOD="trust"
      - POSTGRES_PORT=5432

    ports:
      - "5432:5432"

  pgadmin:
    image: dpage/pgadmin4
    depends_on:
      - db
    ports:
      - "5051:80"
    environment:
      PGADMIN_DEFAULT_EMAIL: pgadmin4@pgadmin4.com
      PGADMIN_DEFAULT_PASSWORD: pgadmin4
    restart: always

  web:
    build: .
    command: python manage.py runserver 0.0.0.0:8000
    ports:
      - "8000:8000"
    links:
      - db:db
    depends_on:
      - db

settings.py

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

Error while executing python3 manage.py makemigrations:
django.db.utils.OperationalError: could not translate host name “db” to address: Temporary failure in name resolution

I tried adding - POSTGRES_HOST_AUTH_METHOD="trust" however the error remained.

I also tried changing 'HOST': 'db' to 'HOST': 'localhost' and then I can run the make migrations but I get this error:
Is the server running on host “localhost” (::1) and accepting TCP/IP connections on port 5432?

I saw many similar questions but none of the answers seems to fix the problem. Any suggestions according to my docker-compose and setting?