A comprehensive PostgreSQL database migration suite for migrating data between databases while handling foreign key dependencies and identity columns.
- Complete Database Migration: Migrate all tables with proper foreign key dependency ordering
- Identity Column Handling: Proper handling of PostgreSQL identity/serial columns
- Foreign Key Management: Automatic dependency resolution and constraint handling
- Progress Tracking: Real-time migration progress with detailed statistics
├── main.go # Main database migration tool
├── drop/
│ └── drop.go # Drop all the tables in the target database
├── go.mod # Go module dependencies
├── go.sum # Go module checksums
├── .env # Environment configuration
├── env.example # Environment configuration example
└── README.md # This file
- Go 1.21 or higher
- PostgreSQL databases (source and target)
- Clone the repository:
git clone https://github.com/bograh/db_migrate.git
cd db_migrate- Install dependencies:
go mod tidy- Create a
.envfile with your configuration:
# Source Database Configuration
SOURCE_DB_HOST=
SOURCE_DB_PORT=
SOURCE_DB_USER=
SOURCE_DB_PASSWORD=
SOURCE_DB_NAME=
SOURCE_DB_SSL=disable
# Target Database Configuration
TARGET_DB_HOST=
TARGET_DB_PORT=
TARGET_DB_USER=
TARGET_DB_PASSWORD=
TARGET_DB_NAME=
TARGET_DB_SSL=disable
Migrate complete database from source to target:
go run main.goFeatures:
- Migrates tables in dependency order
- Handles foreign key constraints
- Preserves identity column values
- Provides detailed migration statistics
- Continues on errors with other tables
| Variable | Description | Default |
|---|---|---|
SOURCE_DB_HOST |
Source database host | localhost |
SOURCE_DB_PORT |
Source database port | 5432 |
SOURCE_DB_USER |
Source database user | postgres |
SOURCE_DB_PASSWORD |
Source database password | `` |
SOURCE_DB_NAME |
Source database name | source_db |
SOURCE_DB_SSL |
Source SSL mode | disable |
TARGET_DB_HOST |
Target database host | localhost |
TARGET_DB_PORT |
Target database port | 5432 |
TARGET_DB_USER |
Target database user | postgres |
TARGET_DB_PASSWORD |
Target database password | `` |
TARGET_DB_NAME |
Target database name | target_db |
TARGET_DB_SSL |
Target SSL mode | disable |
The tool provides detailed statistics including:
- Tables migrated
- Rows migrated per table
- Migration duration per table
- Total migration time
- Error summary
Example output:
================================================================================
MIGRATION SUMMARY
================================================================================
Table Rows Duration Status
--------------------------------------------------------------------------------
tbl_1 5 125ms SUCCESS
tbl_2 5 125ms SUCCESS
tbl_3 5 125ms SUCCESS
tbl_4 5 125ms SUCCESS
tbl_5 5 125ms SUCCESS
...
--------------------------------------------------------------------------------
TOTAL 25 650ms
================================================================================
- Backup your target database before running migrations
- Foreign key constraints are temporarily disabled during migration
- Identity columns are handled with
OVERRIDING SYSTEM VALUE - Sequences are reset to match migrated data
- The tool continues on errors but reports them in the summary
- Connection failures: Check database credentials and network connectivity
- Permission errors: Ensure database user has appropriate privileges
- Foreign key violations: Tables are migrated in dependency order, but check for circular references
- Identity column issues: The tool uses
OVERRIDING SYSTEM VALUEto handle these automatically
- The migration continues even if individual tables fail
- All errors are logged and reported in the final summary
- Check the console output for detailed error messages
This project is licensed under the MIT License - see the LICENSE file for details.
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests if applicable
- Submit a pull request
For issues and questions:
- Open an issue in the repository
- Review the error logs