Search code examples
postgresqldockerdocker-compose

docker-compose can't reset postgresql database


Whenever I try to run docker-compose up I am getting PostgreSQL Database directory appears to contain a database; Skipping initialization I do not want this. I want to completely wipe my postgres database and reinitialize.

I've tried deleting every container, volume, and image on the docker client and I've tried docker-compose down --volumes . Both times I still get PostgreSQL Database directory appears to contain a database; Skipping initialization .

This is my compose env:

# This is passed into postgres container for compose
POSTGRES_PASSWORD=db_pass
POSTGRES_USER=db_user
POSTGRES_DB=test_db
# For heroku
DATABASE_URL="postgres://db_user:db_pass@postgres:5432/test_db"

This is my compose.yml

  postgres:
    image: postgres:16
    ports:
      - 5432:5432
    volumes:
      - ~/apps/postgres:/var/lib/postgresql/data
    env_file:
      - .compose.env
      - .env

Solution

  • /apps/postgres directory is created when your container is created. The volumes parameter in the Docker Compose file maps a directory from the host file system to a directory within the container.

    If you didn't delete the /apps/postgres directory, your database won't be cleared. You can delete the /apps/postgres directory ✅