Docker-Compose postgres upgrade initdb: error: directory "/var/lib/postgresql/data" exists but is not empty
Quoting @yosifkit from this issue
The volume needs to be empty or a valid already initialized postgres database with the file
PG_VERSION
in there so the init can be skipped.... If there are any files or folders in there like
lost+found
it will probably fail to initialize. If there are files that you want to keep in the volume (or have no control over) you could adjust thePGDATA
environment variable to point to a sub-directory in there like-e PGDATA=/var/lib/postgresql/data/db-files/
.
So I added PGDATA
to the environment
section of the compose file to solve the issue (notice the some_name
at the end):
services:
postgres:
image: postgres:12
environment:
PGDATA: /var/lib/postgresql/data/some_name/
I had the same issue today, I fixed it by removing the content of the volume db_data in your case
docker volume ls
docker volume inspect db_data <-- will show you the mountpoint
I went to the directory (mountpoint) ex: /path/data
cp data data.backup
cd data
rm -R *
And start services:
docker-compose up -d
I got this issue because the /var/lib/postgresql/data/postgresql.conf
and the /var/lib/postgresql/data
overlap in the docker container at /var/lib/postgresql/
.
An example of a broken config is:
version: "3.8"
services:
db:
image: "postgres:10"
ports:
- "5432:5432"
volumes:
- ./postgresql.conf:/var/lib/postgresql/data/postgresql.conf
- ./pg-data:/var/lib/postgresql/data
To avoid this I tell PostgreSQL to find it's config in /etc/postgresql.conf
instead so the overlapping volumes don't occur like this:
version: "3.8"
services:
db:
image: "postgres:10"
command: ["postgres", "-c", "config_file=/etc/postgresql.conf"]
ports:
- "5432:5432"
volumes:
- ./postgresql.conf:/etc/postgresql.conf
- ./pg-data:/var/lib/postgresql/data
This is similar to what pmsoltani suggests, but I move the location of the postgresql.conf
file instead of the data directory.