Logo

dev-resources.site

for different kinds of informations.

How to prevent data loss when a Postgres container is killed or shut down.

Published at
3/26/2023
Categories
docker
pods
kubernetes
devops
Author
ndohjapan
Categories
4 categories in total
docker
open
pods
open
kubernetes
open
devops
open
Author
9 person written this
ndohjapan
open
How to prevent data loss when a Postgres container is killed or shut down.

I do not like to make my blog contents so long when I can easily go straight to the point.

The simple answer to this problem is simply docker volume

Why Docker Volume

The main concept behind using Docker volumes for backups is to create a copy of the data outside the container. Each time the container is restarted, the data is copied from the local system into the Docker container file system. This ensures that the data is always up-to-date and consistent with the latest changes.

How To Create Docker Volume for PostgreSQL

  1. To create a Docker volume for PostgreSQL, use the following command:
 docker volume create pgdata
Enter fullscreen mode Exit fullscreen mode

This will create a new Docker volume called pgdata that you can use to store the data for your PostgreSQL container.

  1. Start a new PostgreSQL container using the following command:
docker run -d --name postgresql -v pgdata:/var/lib/postgresql/data postgres:latest
Enter fullscreen mode Exit fullscreen mode

This command starts a new PostgreSQL container called postgresql and maps the pgdata volume to the /var/lib/postgresql/data directory in the container. This means that the data stored in the container will be stored in the pgdata volume, which is separate from the container and will persist even if the container is deleted or restarted.

If the container is deleted or restarted, you can simply start a new container and map the pgdata volume to the /var/lib/postgresql/data directory again, and your data will be restored. You do not need to create backups of your data or copy the data back into the container manually.

Also you can check the docs for more information on docker volumes.

I usually use docker volume for development, but I have not really considered its importance when it comes to production if I won't be using a postgres container. I don't know about you? Tell me what you think in the comment section.

Also, if you found this post helpful, please consider giving it a like or sharing it with others who may be facing a similar issue. Thanks for reading!

pods Article's
24 articles in total
Favicon
Exploring Kubernetes: A Step Ahead of Basics
Favicon
Understanding Kubernetes Pods and Controllers: ReplicaSets, Deployments, DaemonSets, and StatefulSets
Favicon
Understanding Kubernetes Pods with Docker: The Heart of Containerized Applications
Favicon
Understanding Kubernetes Basics: A Beginnerโ€™s Guide
Favicon
Gardening Overalls, Seeds, Pods, and Herbs
Favicon
Navigating Horizontal Pod Autoscaler (HPA) in Kubernetes
Favicon
Copying local file/directory to the k8s pod
Favicon
Kubernetes ConfigMaps and Secrets: A Developer's Guide
Favicon
SQS & Kubernetes Pods: The Quick and Dirty Guide to Read/Write Permissions
Favicon
Pod Disruption Budget in Kubernetes
Favicon
Kubernetes For Beginners : 3
Favicon
How to prevent data loss when a Postgres container is killed or shut down.
Favicon
Create You Own Pod
Favicon
Getting Started: Pod, Replicaset and Deployment in Kubernetes
Favicon
AKS Creation - Step by Step Approach
Favicon
Kubernetes - Part 1 - What are Pods in Kubernetes ?
Favicon
Understanding Kubernetes Evicted Pods
Favicon
Recommender algorithms for distributed social networks
Favicon
What is Kubernetes CrashLoopBackOff? And how to fix it
Favicon
How to restart Pods in Kubernetes : a complete guide
Favicon
K8s Pods: Image tags vs. Digest
Favicon
K8s Objects - Part 1 [Pods and Replicasets]
Favicon
React Native ํ”„๋กœ์ ํŠธ๋ฅผ git์— push ํ•˜๊ธฐ ์ „์— ํ•  ๊ฒƒ
Favicon
What are Kubernetes Pods Anyway?

Featured ones: