-
Notifications
You must be signed in to change notification settings - Fork 14
Closed
Labels
Description
Course
data-engineering-zoomcamp
Question
How can I back up and restore PostgreSQL data that is stored in a Docker volume?
Answer
Method 1: Docker volume backup
# List Docker volumes
docker volume ls
# Backup while the container is running
docker run --rm \
-v ny_taxi_postgres_data:/data \
-v $(pwd):/backup \
ubuntu tar czf /backup/postgres_backup.tar.gz /data
# Restore
docker run --rm \
-v ny_taxi_postgres_data:/data \
-v $(pwd):/backup \
ubuntu tar xzf /backup/postgres_backup.tar.gz -C /Method 2: Using pg_dump
# Backup
docker exec -t postgres_container pg_dump -U root -d ny_taxi > ny_taxi_backup.sql
# Restore
docker exec -i postgres_container psql -U root -d ny_taxi < ny_taxi_backup.sqlMethod 3: Copying the host directory
# When using a host-mounted directory in docker-compose.yaml
cp -r ./ny_taxi_postgres_data ./ny_taxi_postgres_data_backupChecklist
- I have searched existing FAQs and this question is not already answered
- The answer provides accurate, helpful information
- I have included any relevant code examples or links