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
3
  • It looks like there's indentation issue. web as well as db should be preceded with two spaces in this case. Commented Apr 14, 2020 at 16:05
  • What are you running that produces that error? (Are you attempting to run migrations from a Dockerfile, for example?) Commented Apr 14, 2020 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 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. Commented Apr 14, 2020 at 16:18

1 Answer 1

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"
Sign up to request clarification or add additional context in comments.

2 Comments

environment: - "POSTGRES_HOST_AUTH_METHOD=trust" What do these lines mean?
@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).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.