0

I've been hitting the following error for awhile now and can't seem to fix it...

django.db.utils.OperationalError: could not connect to server: Connection refused
Is the server running on host "127.0.0.1" and accepting
TCP/IP connections on port 5432?

Multiple resources stated it was simply due to the HOST setting within my DATABASES, but the following is what I am working with and still can't get it to work:

DATABASES = {
    'default': {
        'HOST': 'db',
        'ENGINE': 'django_postgrespool2',
        'NAME': os.environ.get('PROJECT_HEARSAY_DB_NAME'),
        'USER': os.environ.get('PROJECT_HEARSAY_DB_USER'),
        'PASSWORD': os.environ.get('PROJECT_HEARSAY_DB_PASSWORD'),
        'PORT': os.environ.get('PROJECT_HEARSAY_DB_PORT'),
    }
}

Here is the Dockerfile for my Django app:

FROM python:3
ENV PYTHONUNBUFFERED 1
COPY . /app
WORKDIR /app
RUN pip install --upgrade pip
RUN pip install -r requirements.txt

Here is the Dockerfile for my Postgresql DB:

FROM postgres

And here is the docker-compose.yml that I am working with:

version: "3"

services:

  postgresql:
    build:
      context: ./db
    container_name: db.postgresql
    ports:
      - 5432:5432
    environment:
      POSTGRES_DB: "db_name"
      POSTGRES_USER: "username"
      POSTGRES_PASSWORD: "password"

  django:
    restart: always
    build:
      context: ./api
    command:  bash -c "./manage.py migrate && ./manage.py runserver 0.0.0.0:8000"
    container_name: api.django
    ports:
      - 8000:8000
    depends_on:
      - postgresql

I'm curious if anyone here could shed some light on what I am doing wrong.

Thank you.

1 Answer 1

2

Your container name is db.postgresql, so your connection string should be db.postgresql or postgresql in the host name.

DATABASES = {
    'default': {
        'HOST': 'db.postgresql',
        'ENGINE': 'django_postgrespool2',
        'NAME': os.environ.get('PROJECT_HEARSAY_DB_NAME'),
        'USER': os.environ.get('PROJECT_HEARSAY_DB_USER'),
        'PASSWORD': os.environ.get('PROJECT_HEARSAY_DB_PASSWORD'),
        'PORT': os.environ.get('PROJECT_HEARSAY_DB_PORT'),
    }
}

you can verify the connection

docker exec api.django bash -c "nslookup db.postgresql"

You will get the Postgres container IP inside your Django container.

Sign up to request clarification or add additional context in comments.

5 Comments

Hey, I appreciate the response. I deleted all containers and images, applied your changes, and rebuilt using docker-compose up and am getting the same error. As for your connection test, it is giving me a bash: nslookup: command not found
So ns lookup is not installed on your docker image and it should not point to 127.0.0.1 in connection
Do you mind explaining what you mean by it should not point to 127.0.0.1 in connection? I'm unsure of what you are referring too. I believe that is the default Django points too, but I thought I was altering that with the command command: bash -c "./manage.py migrate && ./manage.py runserver 0.0.0.0:8000" within my docker-compose.yml
No, I mean in database connection error it should not say 127.0.0.1, how the db connection try to connect with 127.0.0.1?
Verify and double check, there is some thing that override db host, it should point to container ip not to local host

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.