postgres: Using volumes for persistent data:'initdb: directory "/var/lib/postgresql/data" exists but is not empty'
I want to use docker volume named “taiga-postgres-pers” to store postgres data between removing ( postgres container (named “taiga-postgres”)
The code for “taiga-postgres” is:
docker run \
--name taiga-postgres \
-v taiga-postgres-pers:/var/lib/postgresql/data \
-e POSTGRES_PASSWORD=mypassword -d postgres
The container exits immediately with error in logs:
initdb: directory “/var/lib/postgresql/data” exists but is not empty If you want to create a new database system, either remove or empty the directory “/var/lib/postgresql/data” or run initdb with an argument other than “/var/lib/postgresql/data”.
So how can i attach existing volume to newly created postgres docker container?
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Comments: 18 (3 by maintainers)
Links to this issue
Commits related to this issue
- Fix missing php-fpm config and use volume for postgres data The former to fix the error: 'session_regenerate_id(): Cannot regenerate session id - headers already sent' See: * https://stackoverflow.c... — committed to rija/gigadb-website by deleted user 6 years ago
- Fix missing php-fpm config and use volume for postgres data The former to fix the error: 'session_regenerate_id(): Cannot regenerate session id - headers already sent' See: * https://stackoverflow.c... — committed to rija/gigadb-website by deleted user 6 years ago
- Specify subdirectory for db data https://github.com/docker-library/postgres/issues/263 — committed to atc0005/cachet-docker by atc0005 5 years ago
- Change PGDATA path to avoid issue in container startup See: https://github.com/docker-library/postgres/issues/263 — committed to Cytomine-ULiege/Cytomine-bootstrap by urubens 5 years ago
- task/Azure-shakeout data-pipelines-infrastructure/docker-compose-celery.yml docker-compose.yml * fix PostgreSQL initdb issue in K8s as per https://github.com/docker-library/postgres/issues/263 * add ... — committed to loum/data-pipelines-infrastructure by loum 4 years ago
I managed to run Postgres (after loosing some data and a lot of experimentation) like so (left out some stuff as well):
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.Did you use a docker volume plugin to create the volume? 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 managed it by adding a subPath to my Deployment yaml
not 100% sure this is correct, I’m new to this, found on stackoverflow
subPath
worksalso check https://github.com/docker-library/postgres/issues/263#issuecomment-431637087
I actually found what the problem was, and it is indeed not related to the docker env variables. For future references, if the volume is mounted in the POD/Deloypment, don’t export the PGDATA env variable because it will tell initdb to make a new one and to remove the old data.
Running this command gives me the same error:
docker run --name some-postgres -v E:\tmp:/var/lib/postgresql/data -d postgres:9.6.15
The folder
tmp
was not empty and postgres needs an empty folder at start.Now i get
Windows here.
Hi e-nouri, thanks for posting back the fix!. I have the same issue but not the experience to know where to stop the PGDATA env var being exported. How do I do this please as I cannot see it in the pod yml? Is this done in the postgres image (9.6.6) and can be over-ridden by the k8s yml?
Please reopen this, as of now I am using postgres:9.6.6 in Kubernetess 1.8 on GCE, when I updated the cluster I lost all of my data because this does not work.
The wanted behaviour: I mount the volume (persistant claim) to the docker and the database files are stored in the mounted claim, each time I mount that volume, I always find my database there.
The acctual behavior of the postgres:9.6.6 docker:
I also ssh’ed into the POD and tested that the PG_VERSION is there and not empty.
The documentation on the docker/_/postgres is confusing and not clear at all.