How to Backup and Restore Database in Postgres Docker

Thu Sep 03 2020

Anurag Kumar

So, what is docker and Postgres ?

Docker : Docker is a set of platform as a service products that use OS-level virtualization to deliver software in packages called containers.

PostgreSQL:  PostgreSQL is a free and open-source relational database management system emphasizing extensibility and SQL compliance. 

1. Run Postgres in docker 

docker run --name some-postgres -p 5432:5432 -e POSTGRES_PASSWORD=mysecretpassword -d postgres

2. Check docker container status

docker ps 

Now you can see your docker container id like this :

lets go inside docker as a bash shell

docker exec -it <CONTAINER ID> bash
docker exec -it d909cca76479 bash

3. Connect to Postgres and create database with user

Lets first, create database  

create database newdb;

create user with password

create user newuser with encrypted password 'mynewpassword';

add user to database 

 grant all privileges on database newdb to newuser;

4. Backup and Restore are same you just need to change  "  " direction for backup and " < " for restore. 

pg_dump -C -h [Hostname] -d [dbname] -p [port] -U [username] -W > backup.sql
OR

Restore your databases

cat backup.sql | docker exec -i <CONTAINER ID> psql -U newuser newdb

 

That's how simple it is to manage postgres in docker!

SHARE THIS ARTICLE

post-thumbnail
Today everyone knows the importance of a lightning-fast website and how the speed impacts the conversion rate of a business. Today, everyone wants the site to be a PWA so that the mobile users can have an app-like experience with the website because, for the majority of the merchants, the customers come through mobile devices.
Tue Apr 20 2021
post-thumbnail
Here we are going to see how you can manage backup and restore of Postgres database with docker.
Thu Sep 03 2020
post-thumbnail
Image sliders or carousels always have increased the UI attraction of websites and they are pretty useful for reflecting the major roles/products too. In case, I am having a website that sells tee-shirts,
Mon Apr 30 2018

About VoidCanvas

This blog was created out of hobby and talks mostly about technology, web development, JavaScript, NodeJS and related topics. Thank you for reading my blog.

Copyright 2022 - www.voidcanvas.com

Popular Articles

Authentication using Google's oAuth api with node.js

Thu Mar 10 2016

OAuth authentications are pretty popular now a days and another thing which is popular is JavaScript. This article shows how to plugin google’s oAuth api for authentication in your own node application.

CSS3 Loader Snippet Collection: (Part 2 - Squares)

Sat Mar 01 2014

This is a continuation of my CSS3 loader snippet collection series. I've provided spinning css3 animation loader in the part 1 of this series and here in part 2, I'm providing various square type loading