Skip to content

[FAQ] Docker: How to Back Up and Restore PostgreSQL Data Stored in Docker Volumes #64

@kkh1902

Description

@kkh1902

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.sql

Method 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_backup

Checklist

  • 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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions