DEV Community

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

Collapse
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 \
  postgres
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!