# Postgres datafile separate from database image

**URL:** <https://forums.docker.com/t/postgres-datafile-separate-from-database-image/71972>\
**Category:** Docker Desktop\
**Created:** [March 27, 2019, 1:25am UTC](https://forums.docker.com/t/postgres-datafile-separate-from-database-image/71972 "2019-03-27T01:25:25Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![geturnerlmco](https://avatars.discourse-cdn.com/v4/letter/g/9e8a1a/32.png) [@geturnerlmco](https://forums.docker.com/u/geturnerlmco)\
**Post date:** [March 27, 2019, 1:25am UTC](https://forums.docker.com/t/postgres-datafile-separate-from-database-image/71972/1 "2019-03-27T01:25:25Z")

</div>

I am looking for a pretty simple answer, hopefully. There are a LOT of answers when I Google, but nothing seems to do what I want.  
I have a compose file that starts postgres and wildfly as services and they connect on a common network, all good. What I cannot get to work, is a “datafile” that survives docker restarts, and I mean that I “rmi mypostgresimage” and data all go by-by. I am on Windows for development, but production will be on Unix.  
I plan on creating the tables as part of my image, then removing the schema scripts and saving as a new image for production. But I want to then load data to test with, and the data still exist after I restart docker, OR, even update my postgres version. So IAW, I want to be able to point a “postgres:latest” at my “volume” and then wildfly can read/maintain data in it. And hopefully, very similiar configuration whether I am in my Windows development environment, or on Unix production environment.

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![tekki](https://sea3.discourse-cdn.com/docker/user_avatar/forums.docker.com/tekki/32/21884_2.png) [@tekki](https://forums.docker.com/u/tekki)\
**Post date:** [March 27, 2019, 10:47am UTC](https://forums.docker.com/t/postgres-datafile-separate-from-database-image/71972/2 "2019-03-27T10:47:20Z")

</div>

Seems you ask two questions, a ‘pretty simple’ and a not so simple one.

> [@geturnerlmco](#):
>
> What I cannot get to work, is a “datafile” that survives docker restarts

That’s the simple one. You have to create a volume.

```auto
services:
  db:
    image: postgres
    ...
    volumes:
      - mydbdata:/var/lib/postgresql/data

volumes:
  mydbdata:

```

Now the other:

> [@geturnerlmco](#):
>
> I plan on creating the tables as part of my image

You can’t do that because the database is not initialized in the image. But if you take a look at [docker-entrypoint.sh](https://github.com/docker-library/postgres/blob/7e80419825e4bab4e749bc61334570ffc261ea5e/11/docker-entrypoint.sh#L145) of the postgres image, you see that during initialization it makes a loop over the files in `/docker-entrypoint-initdb.d`. This means you can place the SQL files in this folder that create all the tables you need in your container.

---

<div class="post-metadata">

**Author:** ![geturnerlmco](https://avatars.discourse-cdn.com/v4/letter/g/9e8a1a/32.png) [@geturnerlmco](https://forums.docker.com/u/geturnerlmco)\
**Post date:** [March 27, 2019, 8:34pm UTC](https://forums.docker.com/t/postgres-datafile-separate-from-database-image/71972/3 "2019-03-27T20:34:11Z")

</div>

I have done this a dozen times. The var/lib path is “part” of the postgres image, and is removed when the image is removed. Tables and data gone.  
I am trying to find out HOW to define mydbdata so that I can physically “see” the file(s) outside of the image.

---

<div class="post-metadata">

**Author:** ![tekki](https://sea3.discourse-cdn.com/docker/user_avatar/forums.docker.com/tekki/32/21884_2.png) [@tekki](https://forums.docker.com/u/tekki)\
**Post date:** [March 28, 2019, 7:07am UTC](https://forums.docker.com/t/postgres-datafile-separate-from-database-image/71972/4 "2019-03-28T07:07:59Z")

</div>

> [@geturnerlmco](#):
>
> The var/lib path is “part” of the postgres image, and is removed when the image is removed. Tables and data gone.

This is not correct, otherwise I would lose the data of my development projects every evening. With the `volumes` command an external and persistent volume is mounted into `/var/lib/postgresql/data`. Take a look at [the documentation](https://docs.docker.com/storage/volumes/). You could even mount a Windows folder into the container, but I don’t recommend this for databases.

---

<div class="post-metadata">

**Author:** ![geturnerlmco](https://avatars.discourse-cdn.com/v4/letter/g/9e8a1a/32.png) [@geturnerlmco](https://forums.docker.com/u/geturnerlmco)\
**Post date:** [March 28, 2019, 6:09pm UTC](https://forums.docker.com/t/postgres-datafile-separate-from-database-image/71972/5 "2019-03-28T18:09:25Z")

</div>

So I think this is where the problem is (and thank you, I read documentation quite well, but when it doesn’t “work”, I ask questions on a forum). I am trying to “develop” on Windows Docker CE. I think that is where the “volumes” are not working so well. I have tried to create a volume and then reference it like you are describing, but the Windows folder never has anything in it.

---

<div class="post-metadata">

**Author:** ![emmanue3765](https://avatars.discourse-cdn.com/v4/letter/e/f1d935/32.png) [@emmanue3765](https://forums.docker.com/u/emmanue3765)\
**Post date:** [April 2, 2019, 2:57pm UTC](https://forums.docker.com/t/postgres-datafile-separate-from-database-image/71972/6 "2019-04-02T14:57:20Z")

</div>

Happy to help! Thanks for leaving a comment.[diebestetest](https://www.diebestetest.de/)
