Open In App

How To Persist Data In A Dockerized Postgres Database Using Volumes ?

Docker and Postgres are two widely used tools in the software development field. Docker simplifies the deployment process by encapsulating applications within a container while Postgres provides a robust and reliable database to store and manage data. In this guide, I will first briefly discuss Docker and Postgres. Then I will guide you through the various steps to persist your Postgres container data using docker volumes.

What is Docker

Docker encapsulates the application and its dependencies into compact units called containers. Containers contain everything that an application needs to run such as libraries, system tools, code, and runtime. This approach greatly enhances portability and scalability. It removes the dependencies of building, testing, and running an application on a particular operating system and hardware. Docker is a very fast, lightweight, and resource-efficient tool. Unlike the traditional virtualization techniques, docker containers share the host operating system kernel which helps the developers to run more containers on a single host. This results in maximizing resource utilization and reducing infrastructure costs. Overall, we can say that Docker has become a very important tool for developers and organizations to accelerate their software deployment and delivery pipelines.



What is Postgres

PostgreSQL is commonly referred to as Postgres. Postgres is an open-source relational database that is used for storing and managing data efficiently. It ensures data integrity as it follows the ACID properties. Postgres provides a variety of features including transactions, complex queries, indexing, and replication. In addition to its core features, Postgres also supports internationalization and text search. These features make Postgres a suitable choice for diverse linguistic and text processing needs. Postgres is widely used in many industries such as healthcare, finance, and telecommunications. We can say overall Postgres is a comprehensive and versatile solution for storing, managing, and analyzing data.

Pre-requisites

Before moving to the next section make sure you have installed Docker on your system. If you have not installed follow these detailed geeksforgeeks articles to install docker on your system.



Steps to Persist Data In A Dockerized Postgres Using Volumes

Step 1: Create a docker volume. This volume will help in persisting the data.

docker volume create postgres_volume

Step 2: Run a Postgres docker container using docker volume.

docker run -d \
--name postgres \
-e POSTGRES_PASSWORD=gfg \
-v postgres_volume:/var/lib/postgresql/data \
postgres:latest

Step 3: Go inside the docker container.

docker exec -it postgres psql -U postgres

Step 4: Create a database.

CREATE DATABASE demo_db;

Step 5: Try to connect the database.

\c demo_db

Step 6: Create a table inside the database and insert some demo data . Then exit.

CREATE TABLE gfg_articles (id SERIAL PRIMARY KEY, name VARCHAR(255));

INSERT INTO gfg_articles (name) VALUES ('Docker 1'), ('Jenkins 2'), ('K8s 3');

You can use the below command to exit the Postgres terminal.

\q

Step 7: Now delete the Postgres docker container.

docker stop postgres
docker rm postgres

Step 8: Create again a Postgres docker container using the docker volume (created in Step 1).

docker run -d \
--name postgres \
-e POSTGRES_PASSWORD=gfg \
-v postgres_volume:/var/lib/postgresql/data \
postgres:latest

Step 9: Finally go inside the docker container and verify that whether the database and table is present or not. (run the commands below one by one )

docker exec -it postgres psql -U postgres
\c demo_db
SELECT * FROM gfg_articles;

Conclusion

Here in this guide we first learn what is Docker . Then learned some basics about Postgres . Then we have followed various steps to persist the Postgres data using docker volumes . We started by creating a Postgres docker container using a docker volume . Then added some dummy database and dummy table to the database . Then deleted the Postgres docker container and recreate the Postgres docker container to verify whether the Postgres data persists or not .

How to persist data in a dockerized postgres database using volumes-FAQ’s

What is docker volume ?

A docker volume is a persistent data storage mechanism . It helps the container to persist their data even after the container removal or restarts .

What happens if i delete the Docker volume containing Postgres data ?

If you delete Docker volume then all the Postgres data that is present inside the docker volume will be lost and can not be restored .

Why persisting Postgres data is required ?

Persisting Postgres data means data will be present even after the docker container is removed or restarted .

Are their any limitation using docker volumes ?

Yes there are limitations using docker volumes. Using docker volume may lead to security vulnerability and storage scalability issues .

How to secure docker volumes ?

Docker volumes can be secured using encryption , access control and other best security practices .


Article Tags :