A minimal script for transferring PostgreSQL databases from Docker containers to host systems.
- 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
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.pyEdit config.ini for your local PostgreSQL setup:
[host]
host = localhost
port = 5432
username = postgres- Detects running PostgreSQL containers
- Connects to selected container database
- Exports database using pg_dump
- Imports to host PostgreSQL
- Cleans up temporary files
postgres(official)postgis/postgistimescale/timescaledbbitnami/postgresql
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
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)
Bug reports and feature requests are welcome! Please create an issue or submit a pull request.