0

settings.py file:

from decouple import config

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': config('POSTGRES_DB'),                      
        'USER': config('POSTGRES_USER'),
        'PASSWORD': config('POSTGRES_PASSWORD'),
        'HOST': config('DB_HOST'),
        'PORT': config('POSTGRES_PORT'),
    }
}

.env file:

# DJANGO
SECRET_KEY='foobar'
DEBUG=False
ALLOWED_HOSTS=localhost 127.0.0.1
# DATABASE
POSTGRES_DB=cheflist
POSTGRES_USER=postgres
POSTGRES_PASSWORD=123123
POSTGRES_HOST=localhost
POSTGRES_PORT=5433

DB_HOST=0.0.0.0

docker-compose.yml file:

version: "3.8"
services:
  db:
    container_name: db
    image: postgres:13-alpine
    restart: always
    env_file:
      - ./.env
    volumes:
      - ./postgres_data/db:/var/lib/postgresql/data/
    ports:
      - 5433:5433

The first question is that when I up the postgres image, everything goes well, BUT a message comes out in the logs:

db  | 2022-09-20 10:23:05.118 UTC [1] LOG:  listening on IPv4 address "0.0.0.0", port 5432
db  | 2022-09-20 10:23:05.119 UTC [1] LOG:  listening on IPv6 address "::", port 5432
db  | 2022-09-20 10:23:05.123 UTC [1] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
db  | 2022-09-20 10:23:05.142 UTC [21] LOG:  database system was shut down at 2022-09-20 10:19:44 UTC
db  | 2022-09-20 10:23:05.163 UTC [1] LOG:  database system is ready to accept connections

For some reason, the port to which postgres is connected in docker is 5432, although I specified 5433.

The second question. When I run the python manage.py runserver terminal throws an error:

django.db.utils.OperationalError: connection to server at "0.0.0.0", port 5433 failed: Cannot assign requested address (0x00002741/10049)
        Is the server running on that host and accepting TCP/IP connections?

... and it seems to me that this has something to do with my first question, since initially the image is not connected to the right port

1
  • You have to use 5432 port Commented Sep 20, 2022 at 10:47

1 Answer 1

1

postgresql would start on port 5432 by default. So in it's container, postgres service is up in 5432.

If You want to connect to it's container with another port, you should map that to 5432. So:

ports:
      - 5433:5432

Now you can connect with port 5433 in host.

But for connecting via containers, you should connect to 5432. So fix port to 5432 in settings and your second questions would be solved.


But if You want to change postgres port in it's container, you can use command: -p 5433:

services:
  db:
    container_name: db
    image: postgres:13-alpine
    restart: always
    env_file:
      - ./.env
    volumes:
      - ./postgres_data/db:/var/lib/postgresql/data/
    expose:
      - "5433"
    ports:
      - 5433:5433
    command: -p 5433

See SO Answer for more details.

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

Comments

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.