1

Hello I occured an error like in title during composing Django web app. I get this error

Is the server running on host "localhost" (::1) and accepting
web_1  |        TCP/IP connections on port 5432?

I have no idea how can i solve it, I have already, restarted Postgresql database and docker but I neither of these work.

settings.py

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'fitshop',
        'USER': 'fitshopuser',
        'PASSWORD': 'fitpass',
        'HOST': 'localhost',
        'PORT': '5432',
    }
}

docker-compose.yml

version: "3"
   

    services:
      db:
        image: postgres
        volumes:
          - ./data/db:/var/lib/postgresql/data
        environment:
          - POSTGRES_DB=postgres
          - POSTGRES_USER=fitshopuser
          - POSTGRES_PASSWORD=postgres
        ports:
          - "5432:5432"
      web:
        build: .
        command: python manage.py runserver 0.0.0.0:8000
        volumes:
          - .:/code
        ports:
          - "8000:8000"
        depends_on:
          - db
Kay
  • 591
  • 1
  • 7
  • 27

1 Answers1

3

When running in docker you could use the service name as the hostname. So, your Django database settings would be like this:

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

The values in the settings should be equal to the environment variables provided in docker-compose file for db

        environment:
          - POSTGRES_DB=postgres          # NAME
          - POSTGRES_USER=fitshopuser     # USER
          - POSTGRES_PASSWORD=postgres    # PASSWORD

As per your docker configuration for Postgres, docker initializes a database named postgres with username as fitshopuser, password as postgres and hostname as db on port 5432.
Should be using these values in Django settings.

About the error:

Is the server running on host "localhost" (::1) and accepting web_1 | TCP/IP connections on port 5432?

with depends_on in service named web it only checks the service db is running or not. It does not mean that the Postgres database is up and running.

You can find about this in the official docker documentation here.

        depends_on:
          - db

Inorder to check if check if Postgres is ready and is accepting connections over port 5432, use scripts like wait-for-it

OR

I would simple install postgresql-client in service named web and using the command pg_isready check if Postgres is ready.

Add these lines to the dockerfile.

RUN apt-get install -y postgresql-client

# provide permissions to run the entrypoint script (update path accordingly)
RUN chmod +x /path/to/entrypoint.sh

Update/Add entrypoint.sh file.

# entrypoint.sh
while ! pg_isready -h db -p 5432 --username=fitshopuser ; do
    echo "Postgres is unavailable - sleeping"
    sleep 2
done

Specify the entrypoint to service named web.

      web:
        build: .
        command: python manage.py runserver 0.0.0.0:8000
        volumes:
          - .:/code
        restart: always
        ports:
          - "8000:8000"
        entrypoint: "/path/to/entrypoint.sh"   # Update path
        depends_on:
          - db
Achuth Varghese
  • 2,356
  • 1
  • 4
  • 18
  • It does not work, even when I do not want to use docker, just write "python manage.py runserver" i get the same error. – Kay Oct 10 '21 at 19:39
  • The error that you provided says that the Postgres database is not ready yet and is not accepting connections. Potgres docker takes a little bit of time to configure its database and to start accepting connections over port 5432. – Achuth Varghese Oct 10 '21 at 19:42
  • @Kay check if the Postgres is up and running and is accepting connections over port 5432. – Achuth Varghese Oct 10 '21 at 20:05