Postgres datafile separate from database image

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.

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

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

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

volumes:
  mydbdata:

Now the other:

You can’t do that because the database is not initialized in the image. But if you take a look at docker-entrypoint.sh 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.

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.

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. You could even mount a Windows folder into the container, but I don’t recommend this for databases.

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.

Happy to help! Thanks for leaving a comment.diebestetest