Home > OS >  MacOS Postgres in docker is not getting deleted after running docker-compose down
MacOS Postgres in docker is not getting deleted after running docker-compose down

Time:01-12

I am spinning up a Postgres database in Docker-compose and it worked great the first time. I wanted to test some things so I ran

docker-compose down

And after my changes. I ran docker-compose up. This time, I keep getting a message saying

PostgreSQL Database directory appears to contain a database; Skipping initialization

My docker-compose is as follows:

  postgres_db:
    image: postgres:14.1
    restart: unless-stopped
    environment:
      - POSTGRES_PASSWORD=${BP_ADMIN_PASSWORD}
    volumes:
      - ./admin/${BP_ENV:-dev}/database:/docker-entrypoint-initdb.d
      - ./data/postgres:/var/lib/postgresql/data
    command: postgres -c logging_collector=on -c log_rotation_age=1d -c log_directory=/mnt/log -c 
    ports:
      - "5432:5432"

I ran docker-compose down --volumes to try to get rid of everything, but it did not work. Then, I deleted the entire ./data/postgres folder on my local drive but it still failed. After, I commented out - ./data/postgres:/var/lib/postgresql/data but it still did not work.

How do I get rid of the existing database?

CodePudding user response:

Running docker-compose down --volumes will only delete Docker volumes. You're not using volumes; you're using bind-mounts, in which you are mounting a host directory inside your container:

    volumes:
      - ./admin/${BP_ENV:-dev}/database:/docker-entrypoint-initdb.d
      - ./data/postgres:/var/lib/postgresql/data

The way you delete that data is by using rm, as in:

rm -rf ./data/postgres/*

If you wanted to use a Docker volume for the database directory, that would look like:

version: "3"

services:
  postgres_db:
    image: postgres:14.1
    restart: unless-stopped
    environment:
      - POSTGRES_PASSWORD=${BP_ADMIN_PASSWORD}
    volumes:
      - ./admin/${BP_ENV:-dev}/database:/docker-entrypoint-initdb.d
      - pgdata:/var/lib/postgresql/data
    command: postgres -c logging_collector=on -c log_rotation_age=1d -c log_directory=/mnt/log -c 
    ports:
      - "5432:5432"

volumes:
  pgdata:

In this case, running docker-compose down -v (or --volumes, if you prefer) would delete the database volumes.

  •  Tags:  
  • Related