Postgres refuses to launch, directory is empty

Hello guys I have tried to mount the persistent volume to the PostgreSQL, but Postgres just screws me over saying

postgres_1       | initdb: directory "/var/lib/postgresql/data" exists but is not empty
postgres_1       | If you want to create a new database system, either remove or empty
postgres_1       | the directory "/var/lib/postgresql/data" or run initdb
postgres_1       | with an argument other than "/var/lib/postgresql/data".

and I can not get my head around why? Here is my docker-compose.yml. I am running on MacOS High Sierra (APFS), Docker CE.

version: '3'
services:
  postgres:
    image: postgres:10.1-alpine
    ports:
      - "5432:5432"
    networks:
      - db
    environment:
      POSTGRES_USER: x
      POSTGRES_PASSWORD: y
    volumes:
      - pgdata:/var/lib/postgresql/data
networks:
  proxy:
    driver: bridge
  db:
    driver: bridge
volumes:
  pgdata:

the pgdata folder is not emptyā€¦

1 Like

even if the volume is new? Or how should I create empty volume?

the ā€˜volumeā€™ is a docker term for the connectivity to a host folderā€¦ doesnā€™t have any concept of data in the folder.
THAT is an application (postgres) thing

the directory pgdata should have NO files in you are creating a NEW databaseā€¦

if you are REUSING an existing database then there can be files present (but then you would not say ā€˜create databaseā€™ā€¦

In the docker-compose.yml the OP quoted, the volume is a Docker-managed volume, not a bind-mounted host directory.

docker volume ls will show the volumes that exist. Since the default assumption is that they hold valuable data like, say, your database, the default tooling is a little hesitant to delete them; and when you restart the container it will pick up the old volume again. docker volume rm will delete an (unused) volume thatā€™s lying around. You need to give an explicit docker-compose down -v option to cause Compose to remove named volumes.

its still a bind mounted volume, just using the volume id from docker create volume.

the reuse is the key behavior difference

I have nuked every volume, it got stuck somehow. :confused:

and its ok now?
need 2o chars

Yes, it works! Docker compose still refuses to watch the changes on the drive so I have to manually build the images. But the issue is solved.

I followed this answer and it worked Docker-Compose postgres upgrade initdb: error: directory "/var/lib/postgresql/data" exists but is not empty - Stack Overflow