A command-line utility developed in Python to facilitate the migration of database schemas and data from MySQL to PostgreSQL.
- Schema Comparison: Compares the source (MySQL) and target (PostgreSQL) databases and lists which tables exist or are missing in the target.
- Table Creation: Automatically generates PostgreSQL
CREATE TABLEstatements based on the MySQL table structure, mapping data types. - Flexible Migration Control:
--recreate: Drop and recreate tables in the target database even if they already exist.--truncate: Truncate (empty) existing tables in the target database before migrating data.
- Chunk-based Data Migration: Transfers data in configurable chunks (
--chunk-size) to handle large tables efficiently. - Live Progress Display: Shows real-time progress of data migration for each table, including the number of records transferred.
- Configuration File: Database credentials and connection details are managed externally in a
config.inifile, not hardcoded. - Python
uvEnvironment: Usesuvfor fast and straightforward Python environment and package management.
- Python 3.8+
- uv (for environment management, though
pipcan also be used)
-
Clone the Repository (or use the files already created):
# git clone <repository_url> cd mysql2postgres
-
Create a Virtual Environment: Use
uvto create an isolated Python environment.uv venv
-
Activate the Virtual Environment:
- On macOS/Linux:
source .venv/bin/activate - On Windows:
.venv\Scripts\activate
- On macOS/Linux:
-
Install Dependencies: Install the required Python packages from
requirements.txt.uv pip install -r requirements.txt
Before running the tool, you must provide your database connection details.
-
Rename or copy
config.ini.templatetoconfig.ini. -
Edit
config.iniwith your specific database credentials:[mysql] host = localhost user = your_mysql_user password = your_mysql_password database = source_database_name port = 3306 [postgresql] host = localhost user = your_postgres_user password = your_postgres_password database = target_database_name port = 5432
Ensure your virtual environment is activated before running any commands. All commands should be run from the mysql2postgres directory.
To see all available commands and options:
python main.py --helpTo compare the databases without performing any migration. This is the recommended first step to understand the current state.
python main.pyThis will output a list of tables found in the source and indicate whether they exist in the target.
This command will migrate only the tables that are missing in the target database. It will not affect existing tables.
python main.pyThis will empty any existing tables in the target database before migrating new data into them.
python main.py --truncateThis will DROP all corresponding tables in the target database and recreate them from the MySQL schema before migration.
Warning: Use this command with caution, as it will destroy any existing data and schema in the target tables.
python main.py --recreateTo control memory usage and migration speed, you can set the number of records to process in each batch.
python main.py --chunk-size 5000The script includes a simplified function (map_mysql_to_postgres_type) to convert MySQL data types to their PostgreSQL equivalents. This mapping covers common types but may not handle all edge cases or custom data types perfectly. If you have a complex schema, you may need to adjust this function in main.py.
This project is licensed under the MIT License.