How To Persist Data In A Dockerized Postgres Database Using Volumes ?
Last Updated :
16 Feb, 2024
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 .
Similar Reads
How to Use Docker For Stateful Applications with Persistent Volumes? Data persistence is provided through a controlled directory called a Docker Volume, which may be mounted inside Docker containers. When containers are stopped or deleted, it enables data to continue to exist. Volumes can be shared across containers and are unaffected by container lifecycles. They ma
3 min read
How to Seed a MongoDB Database Using Docker Compose Seeding a MongoDB database is a common task in many development and testing scenarios. It involves populating the database with initial data to ensure consistent and predictable behavior during the application development and testing phases. Docker Compose is a powerful tool that simplifies the proc
4 min read
Save a image file on a Postgres database - Python In this article, we are going to see how to save image files on a postgresql database using Python. Psycopg2 is a driver, that is used, for interacting, with Postgres data, using the Python scripting language. It is, used to perform, CRUD operations on Postgres data. Data handled in applications c
4 min read
Storing a BLOB in a PostgreSQL Database using Python This article focuses on, Storing BLOB in a PostgreSQL database. BLOB is a Binary large object (BLOB) is a data type that can store any binary data.To Store Blob data in a Postgres database Table, we will use psycopg2.The table for storing BLOB data in PostgreSQL is called a Large Object table and th
3 min read
Run PostgreSQL on Docker and Setting Up pgAdmin PostgreSQL, an effective tool, is a freeÂ-to-use relational database management system. Docker can quickly construct and orcheÂstrate its instances without bothering about the complexity of setup or depeÂndencies. This step-by-step simple guide will show you how to get PostgreÂSQL on Docker, and the
7 min read
Docker Data Volume vs Mounted Host Directory Docker can be defined as an open-source tool that allows the software to be deployed within containers which are basic, yet extremely efficient and flexible entities on the system level, and how this tool has transformed the process. Some of the significant aspects that help make Docker so powerful
10 min read