How to Store PostgreSQL Database Files Outside the Docker Container?

0
7
Asked By CuriousCoder92 On

I'm trying to set up PostgreSQL 18 in Docker and want to store the database files on my host machine instead of within the container. This way, I could easily set up a new container using the same database. However, after configuring my Docker Compose setup, my host folder remains empty. Here's my setup:

```yaml
postgres:
image: docker.io/library/postgres:18
container_name: postgres
environment:
- POSTGRES_USER=user
- POSTGRES_PASSWORD=123456
- POSTGRES_DB=meshcentral
- PGDATA=/var/lib/postgresql/18/docker
volumes:
- ./postgres:/var/lib/postgresql
restart: unless-stopped
ports:
- "5332:5432"
networks:
- internal-database
healthcheck:
test: [ "CMD-SHELL", "pg_isready -d postgres" ]
interval: 30s
timeout: 10s
retries: 5
```

Even with and without setting PGDATA, I can still see the database files when I attach to the container, but they don't seem to be reflected on the host. Am I misunderstanding the way volumes work?

2 Answers

Answered By PostgresNinja88 On

It sounds like you're not quite mapping the volume correctly. Double-check the path on the right side of your volume mapping. With PostgreSQL 18, you should map it to `/var/lib/postgresql/18/docker` since it's the new structure they introduced. Also, make sure your user has the right permissions for that host folder — it might be a permissions issue that’s causing the files not to show up on your host.

Answered By DockerDude42 On

Just to add, it's important to ensure your Docker daemon is configured correctly, especially if you're using a VM or Docker Desktop. Sometimes using a relative path like `./postgres` can lead to unexpected results, and you might want to inspect the container with `docker inspect` to see where exactly it's mapping to on your host.

InquiringMind07 -

That’s a good point! I think something like `./postgres` could point to an unexpected location, especially in a VM setup. I'll definitely check my path mappings.

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.