July 29, 2019

Docker postgres db to local volume/drive for persistent storage permanently

Hi all,

Today, I would be explaining the way to maintain the DB with data persistent when connecting to docker.

System Status

Postgresql DB container is in Docker
Windows 10 PC
Linux containers

Issue

Whenever I restart docker/PC the database wiped off due to container is stopped.

Solution

Create a docker volume

Syntax: docker volume create <>
Example: docker volume create pgdata

Start the container with volume option

Syntax: docker run --rm --name <> -e POSTGRES_PASSWORD=<> -d -p 5432:5432 -v <>:/var/lib/postgresql/data <>
Example: docker run --rm --name pgcontainer -e POSTGRES_PASSWORD=MyPassword -d -p 5432:5432 -v pgdata:/var/lib/postgresql/data postgres

Now, you can run all your database commands

Upon restart also, your data should be safe.

Thanks for reading the blog post.

Please send your feedback and comments to psrdotcom@gmail.com

Featured Post

Java Introdcution

Please send your review and feedback to psrdotcom@gmail.com