Django Docker Setup: OperationalError - "FATAL: database 'guardian_grid' does not exist" with PostgreSQL After Running for a Few Days
I’m experiencing a recurring issue with my Django project configured to run with PostgreSQL in Docker. After a few days of smooth operation, the PostgreSQL database unexpectedly fails, and I receive the following error:
django.db.utils.OperationalError: connection to server at "postgres" (172.18.0.2), port 5432 failed: FATAL: database "guardian_grid" does not exist
I’ve shared my Docker Compose configuration below. The setup includes volumes for persistent data and backup storage for PostgreSQL. Despite this, the database sometimes "disappears," and I’m unsure why this is happening. I’m looking for insights into potential causes and solutions to ensure the database remains intact.
volumes:
guardian_grid_local_postgres_data: {}
guardian_grid_local_postgres_data_backups: {}
services:
postgres:
build:
context: .
dockerfile: ./compose/production/postgres/Dockerfile
image: guardian_grid_local_postgres
container_name: guardian_grid_local_postgres
volumes:
- guardian_grid_local_postgres_data:/var/lib/postgresql/data
- guardian_grid_local_postgres_data_backups:/backups
- /home/ehabsami/Desktop/guardian_grid/init.sql:/docker-entrypoint-initdb.d/init.sql
env_file:
- .envs/.local/.postgres
ports:
- "5432:5432"
healthcheck:
test: ["CMD-SHELL", "pg_isready -U postgres"]
interval: 10s
retries: 5
guardian_grid:
build:
context: .
dockerfile: ./compose/production/django/Dockerfile # Ensure correct path for Django Dockerfile
restart: always
image: guardian_grid_local_django
container_name: guardian_grid_local_django
depends_on:
postgres:
condition: service_healthy
redis:
condition: service_started
volumes:
- .:/app:z
command: /bin/sh -c "/run.sh" # Ensure run.sh exists
ports:
- "8000:8000"
env_file:
- .envs/.local/.django
redis:
image: "redis:alpine"
ports:
- "6379:6379"
# Python and dependencies setup
FROM docker.io/python:3.12.4-slim-bookworm AS python
# Python build stage
FROM python AS python-build-stage
ARG BUILD_ENVIRONMENT=local
RUN apt-get update && apt-get install -y build-essential libpq-dev && apt-get clean
COPY requirements.txt .
RUN pip wheel --wheel-dir /usr/src/app/wheels -r requirements.txt
# Python run stage
FROM python AS python-run-stage
ARG BUILD_ENVIRONMENT=local
ARG APP_HOME=/app
ENV PYTHONUNBUFFERED=1
ENV PYTHONDONTWRITEBYTECODE=1
ENV BUILD_ENV=${BUILD_ENVIRONMENT}
WORKDIR ${APP_HOME}
RUN apt-get update && apt-get install --no-install-recommends -y \
sudo git bash-completion nano ssh gettext libpq-dev \
&& apt-get purge -y --auto-remove -o APT::AutoRemove::RecommendsImportant=false \
&& rm -rf /var/lib/apt/lists/*
COPY --from=python-build-stage /usr/src/app/wheels /wheels/
RUN pip install --no-cache-dir --no-index --find-links=/wheels/ /wheels/* && rm -rf /wheels/
COPY . ${APP_HOME}
COPY ./run.sh /run.sh
RUN chmod +x /run.sh
COPY ./run.sh /run_prod.sh
RUN chmod +x /run_prod.sh
postgres docker file
FROM docker.io/postgres:16
COPY ./compose/production/postgres/maintenance /usr/local/bin/maintenance
RUN chmod +x /usr/local/bin/maintenance/*
RUN mv /usr/local/bin/maintenance/* /usr/local/bin && rmdir /usr/local/bin/maintenance
Despite configuring persistent volumes, the database sometimes “disappears,” leading to the error. I’m not sure if there’s an issue with how the volumes are mounted, a configuration error, or something else entirely. Any insights on why this may be happening or how I can ensure my database stays persistent and functional would be much appreciated.
Questions
Is there something I’m missing in the Docker Compose or PostgreSQL setup that could cause the database to be deleted or inaccessible?
Could there be a Docker volume configuration or permission issue that might cause the database to “disappear” after running for a while?
Are there specific configurations to ensure long-term persistence of the PostgreSQL database in Docker?