Docker
PostgreSQL
Data Persistence
Volumes
Database Management

How to persist data in a dockerized postgres database using volumes

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

Persisting Data in a Dockerized PostgreSQL Database Using Volumes

Docker's design principle is to enable stateless and ephemeral containers. When a container is removed, all of its data is destroyed. In scenarios where data persistence is crucial, such as for databases, Docker volumes provide a reliable method. This article explores the ins and outs of using Docker volumes to persist data with a PostgreSQL database.

Concept of Docker Volumes

Docker volumes are a preferred mechanism for persisting data generated by and used by Docker containers. Volumes are stored outside of the container’s writable layer, avoiding that layer's performance and storage constraints. They enable data sharing among multiple containers and outlive the lifecycle of individual containers.

Setting Up PostgreSQL with Docker Volumes

Below, we outline the steps to deploy a PostgreSQL Docker container using a volume to persist its data.

  1. Install Docker: Ensure you have Docker installed on your system. You can verify installation by running docker --version.
  2. Pull the PostgreSQL Image: To start, pull the official PostgreSQL image from Docker Hub.
bash
   docker pull postgres:latest
  1. Create a Docker Volume: Create a named volume that PostgreSQL can use to store its data.
bash
   docker volume create pgdata
  1. Run a PostgreSQL Container: Launch a PostgreSQL container with the created volume. Here’s an example command:
bash
   docker run --name my-postgres -e POSTGRES_PASSWORD=mysecretpassword -v pgdata:/var/lib/postgresql/data -d postgres
  • --name my-postgres gives the container a friendly name.
  • -e POSTGRES_PASSWORD=mysecretpassword sets the PostgreSQL root account password.
  • -v pgdata:/var/lib/postgresql/data mounts the pgdata volume to the PostgreSQL data directory.
  • -d postgres runs the container detached, using the postgres image.
  1. Interact with PostgreSQL: To enter the running PostgreSQL container and interact with the database, use:
bash
   docker exec -it my-postgres psql -U postgres

You'll be dropped into the PostgreSQL interactive terminal, where you can perform database operations.

Persisting Data Across Container Restarts

To demonstrate data persistence, you can insert data, stop or remove the running container, and start it again:

sql
CREATE TABLE test (id serial PRIMARY KEY, name VARCHAR (100));
INSERT INTO test (name) VALUES ('Hello World');

Stop and remove the container:

bash
docker stop my-postgres
docker rm my-postgres

Run the container again:

bash
docker run --name my-postgres -e POSTGRES_PASSWORD=mysecretpassword -v pgdata:/var/lib/postgresql/data -d postgres

Verify data persists:

sql
1SELECT * FROM test;
2-- Output:
3--  id |   name
4-- ----+-----------
5--   1 | Hello World

Advantages and Disadvantages

Here's a summary of using Docker volumes for data persistence:

AdvantagesDisadvantages
Data survives container restartsVolumes are Docker-specific, less portable on non-Docker systems
Allows sharing data between containersRequires learning curve for volume management
Optimized performance for storageMismanagement can lead to dangling volumes

Additional Details

  • Creating and Pruning Volumes: Use docker volume ls to list volumes and docker volume rm VOLUME_NAME to remove unused volumes. docker volume prune can clean up all unused volumes.
  • Data Volume Containers: Instead of directly mounting to host paths, you can use data volume containers that hold the data volume and mount it to other containers.
  • Backups: Regular database backups are essential. While volumes protect data from container removal, they won’t shield it from corruption or host-level failures.
  • Security Concerns: Be cautious with environment variables for sensitive data. Consider using Docker Secrets for production environments.

Conclusion

Combining Docker with PostgreSQL via volumes is an effective way to maintain data persistency across container lifecycles. By leveraging Docker volumes, developers can ensure their applications remain both flexible and resilient, augmenting the benefits of containerized environments with persistent storage solutions.


Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

All Rights Reserved.