Skip to content

A minimal Python script for transferring PostgreSQL databases from Docker containers to host systems with automatic container detection and secure data migration

ceviixx/psql-docker-to-host

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 

Repository files navigation

PostgreSQL Docker-to-Host Transfer Script

A minimal script for transferring PostgreSQL databases from Docker containers to host systems.

Features

  • Smart Container Detection: Automatically finds PostgreSQL database servers
  • Secure Data Transfer: Uses pg_dump for lossless database migration
  • Zero Data Loss: Original container data remains unchanged
  • Auto-Detection: Detects container environment variables
  • Configuration File: Host settings managed via config.ini

Warning

  • Test First: Always test with non-critical databases first
  • Backup: Create backups before any database operations
  • Configuration: Edit config.ini for your local PostgreSQL setup

Quick Start

Requirements: Python 3.6+, Docker, PostgreSQL client tools

# Clone repository
git clone https://github.com/ceviixx/psql-docker-to-host.git
cd psql-docker-to-host

# Install PostgreSQL client (if needed)
brew install postgresql  # macOS
# sudo apt-get install postgresql-client  # Ubuntu

# Run script
python3 postgres_transfer.py

Configuration

Edit config.ini for your local PostgreSQL setup:

[host]
host = localhost
port = 5432
username = postgres

How It Works

  1. Detects running PostgreSQL containers
  2. Connects to selected container database
  3. Exports database using pg_dump
  4. Imports to host PostgreSQL
  5. Cleans up temporary files

Supported Images

  • postgres (official)
  • postgis/postgis
  • timescale/timescaledb
  • bitnami/postgresql

Example Output

PostgreSQL Docker-to-Host Transfer

PostgreSQL containers:
1) my-postgres-db (postgres:15)
2) project-db (postgres:14)
Select a container (1-2) or 'q' to quit: 1

Connection details for my-postgres-db:
Host (default: localhost): 
Port (default: 5432): 
Username (default: postgres): 
Password: 

Available databases:
1) myapp
2) users
Select a database (1-2) or 'q' to quit: 1

Transfer: myapp from my-postgres-db
Start transfer? (y/n): y
Host PostgreSQL password for postgres@localhost: 
Exporting...
Preparing target...

WARNING: Database 'myapp' already exists!
This will PERMANENTLY DELETE all existing data!
Overwrite 'myapp'? (y/n): y
Importing...

Transfer completed: myapp

Troubleshooting

No containers found? → Check docker ps
Connection failed? → Verify credentials and container access
Host connection failed? → Check config.ini and host PostgreSQL
Missing psql? → Install with brew install postgresql (macOS)

Contributing

Bug reports and feature requests are welcome! Please create an issue or submit a pull request.

About

A minimal Python script for transferring PostgreSQL databases from Docker containers to host systems with automatic container detection and secure data migration

Topics

Resources

Stars

Watchers

Forks

Languages