DEV Community

Discussion on: How To Install and Run PostgreSQL using Docker ?

marcosvrsdev profile image
Marcos Rezende

Hey great article! But why not use Docker for production environment?!

shree_j profile image
ShreeJ Author

Its not about using docker for production environment.
As the DB is hosted in a container and not attached to any volume, once we delete the container (like docker rm <container_id>), the data in DB will be lost permanently.
Which will not be the way a production app should work.

dwaynebradley profile image
Dwayne Bradley

I agree with Marcos...nice article J Shree! :-)

It might also be helpful to show folks how to save their data even after running docker rm <container_id>. This is how I normally accomplish this:

First, create a local directory to hold the data:

mkdir -p /home/<your_user_id_here>/pgdata
Enter fullscreen mode Exit fullscreen mode

Then start PostgreSQL using a volume mount so the container will store the data in this newly created local directory:

docker run \
  -d \
  --name postgresql-container \
  -p 5432:5432 \
  -e POSTGRES_PASSWORD=somePassword \
  -v /home/<your_user_id_here>/pgdata:/var/lib/postgresql/data \
Enter fullscreen mode Exit fullscreen mode

Using this method, you can be safe in knowing that even if you accidentally run docker rm <container_id> that you can restart PostgreSQL again and have all of you data just as you left it previously.

Hope this helps!