How to Backup and Restore Database in Postgres Docker
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
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
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!