I have read stackoverflow posts, but could not find a solution to my problem of persisting data with a volume with postgres:
First i create a volume:
docker volume create pgdata
Then i run the postgres container:
docker run -d -v pgdata:/var/lib/postgresql -e POSTGRES_PASSWORD=password -p 5432:5432 postgres
Then i create a database connecting with sql:
psql postgresql://postgres:password@localhost:5432/postgres
After stopping the container i restart with:
docker run -d -v pgdata:/var/lib/postgresql -e POSTGRES_PASSWORD=password -p 5432:5432 postgres
The database is lost. Should it not stay there, because i use the same volume?
EDIT: PGDATA="/var/lib/postgresql/data/pgdata" needs to be added and it works as well as the solution of David Maze