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 List Databases and Tables in PostgreSQL using PSQL
PostgreSQL is a powerful, open-source object-relational database system. It provides a wide array of tools and features to manage databases, tables, and other database objects. In this article, we will explain how to list databases and tables in PostgreSQL using the psql command-line interface. We w
3 min read
How to Persist Data in Distributed Storage?
Do you know how your files stay safe and accessible in the digital world? It's all because of distributed storage systems. But what keeps your data from disappearing into thin air? That's where data persistence comes in. In this article, we'll break down the basics of how your data sticks around in
8 min read
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
How To Create EBS Volume In AWS Using Terraform
EBS Stands for Elastic Block Storage is a block-level storage service provided by Amazon web services to use with Amazon's Elastic Compute Cloud (EC2) instances.It provides persistent, high-performance storage volumes that can be attached to Amazon EC2 instances. it acts as an attached external hard
6 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
Sending data from a Flask app to PostgreSQL Database
A database is used to store and maintain persistent data that can be retrieved and manipulated efficiently. we usually need a database, an organized collection of data for example in an e-commerce web app where we need to store the data about the users, orders, carts, shipping, etc. In a database, d
6 min read
Microsoft Azure - Using Flexible Server in Azure Database for PostrgeSQL
In this article, we will look into the process of using the Flexible Server in the Azure Database for Postgres SQL. You can create an Azure Database for Postgres SQL with a Flexible Server which allows for configuration and control of the database server. Let's take a look at the below implementatio
3 min read
How to Push a Container Image to a Docker Repository?
In this article we will look into how you can push a container image to a Docker Repo. We're going to use Docker Hub as a container registry, that we're going to push our Docker image to. Follow the below steps to push container Image to Docker repository: Step 1: The first thing you need to do is m
2 min read