Django problem cannot connect to PostgreSQL server in Docker container
I have this logs
2024-10-21 23:55:07 django.db.utils.OperationalError: connection to server at "localhost" (::1), port 5432 failed: Connection refused
2024-10-21 23:55:07 Is the server running on that host and accepting TCP/IP connections?
2024-10-21 23:55:07 connection to server at "localhost" (127.0.0.1), port 5432 failed: Connection refused
2024-10-21 23:55:07 Is the server running on that host and accepting TCP/IP connections?
but my PostgreSQL server is working proprely
there are my postgres logs
2024-10-21 23:50:15 2024-10-21 18:50:15.916 UTC [29] LOG: database system was shut down at 2024-10-21 18:50:14 UTC
2024-10-21 23:50:15 2024-10-21 18:50:15.918 UTC [1] LOG: database system is ready to accept connections
database setting inside django
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'pgdb',
'USER': 'postgres',
'PASSWORD': 'admin',
'HOST': 'localhost',
'PORT': '5432',
}
}
compose file
version: '3.8'
services:
web:
build: .
command: bash -c "python /app/manage.py migrate && python /app/manage.py runserver 0.0.0.0:8000"
ports:
- 8000:8000
depends_on:
- postgres
volumes:
- .:/app
postgres:
image: postgres:latest
environment:
POSTGRES_DB: pgdb
POSTGRES_USER: postgres
POSTGRES_PASSWORD: admin
ports:
- "5433:5432"
dockerfile
FROM python:3.11
WORKDIR /app
COPY . /app
RUN pip install -r requirements.txt && pip install psycopg2-binary
COPY . .
I want just run django server on docker, I have tried to run my friend's completed project also using docker, firstly the same error was thrown, but then it started working properly, I dont know what should I do