You need to mount a volume to the data
directory of PostgreSQL.
You can use the following, using the docker-compose
file:
version: "3"
services:
test-postgresql:
image: postgres:9.4
container_name: test-postgresql
ports:
- "5432:5432"
environment:
POSTGRES_PASSWORD: dna44100
volumes:
- ./init.sql:/docker-entrypoint-initdb.d/init.sql
- ./folder-on-host:/var/lib/postgresql/data
With the docker-compose file you can start the container with docker-compose up
and stop the container with docker-compose down
. The database and settings are saved on the specified directory (./folder-on-host
).
If you want to remove the volume you can use the command: docker-compose down -v
You can also use the docker run
to mount a volume, using the -v
or -volume
option:
docker run -v ./folder-on-host:/var/lib/postgresql/data yourimagename