1
  • Error Type: django.db.utils.OperationalError

django.db.utils.OperationalError: could not translate host name "db" to address: Name or service not known

changes i made in settings.py file

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

changes i made in dockor-compose.yml file

version: '3.7'

services:
  web:
    build: .
    command: python /code/manage.py runserver 0.0.0.0:8000
    volumes:
    - .:/code
    ports:
    - 8000:8000
    depends_on:
    - db
  db:
    image: postgres:11
Yash Marmat
  • 1,077
  • 10
  • 18
  • It looks like there's indentation issue. web as well as db should be preceded with two spaces in this case. – magma Apr 14 '20 at 16:05
  • What are you running that produces that error? (Are you attempting to run migrations from a Dockerfile, for example?) – David Maze Apr 14 '20 at 16:12
  • after making changes in database (as shown in above settings.py file) i installed psycopg2 (by using this command -> docker-compose exec web pipenv install psycopg2-binary==2.8.4) then i used "docker-compose down" command then i ran this command --> docker-compose up -d --build. But It didnt loaded my local host page http://127.0.0.1:8000/ and when in tried to run migrations it generated above error. Also the indendation is not the issue i uploaded it wrongly but now i updated that. – Yash Marmat Apr 14 '20 at 16:18

1 Answers1

3

I solved it finally!

got sorted out just needed to place envioronments in docker-compose.yml file. In the following manner

version: '3.7'

services:
  web:
    build: .
    command: python /code/manage.py runserver 0.0.0.0:8000
    volumes:
    - .:/code
    ports:
    - 8000:8000
    depends_on:
    - db
  db:
    image: postgres:11
    environment:
    - "POSTGRES_HOST_AUTH_METHOD=trust"
Yash Marmat
  • 1,077
  • 10
  • 18
  • ```environment: - "POSTGRES_HOST_AUTH_METHOD=trust"``` What do these lines mean? – Tareq Monwer Mar 15 '21 at 15:40
  • 1
    @TareqMonwer the environment variable basically handles the user information like username and password. Its upto you to define those fields manually in the evironment or can just pust the code which i used (which does not require any authentication check). – Yash Marmat Jun 08 '21 at 07:17