Back-Up and Restore PostgreSQL DB in Docker Container

2024-01-10 Antonio Canzanella

Overview

Learn how to perform backup and restore of your PostgreSQL database in Docker containers using shell commands.

Solution


$containerName = "my_postgres_container";
$dbUser = "postgres_user";
$database = "my_database";

docker exec -t $containerName pg_dumpall -c -U $dbUser > database-dump.sql

# stop the container and remove the data volume
# start the container normally

cat database-dump.sql | docker exec -i $containerName psql -U $dbUser -d $database