MysqlMigratorPostgreSQL
pip install mysqlmigratorpostgresql
Introduction
MysqlMigratorPostgreSQL is a Python library designed to simplify the migration of entire databases from MySQL to PostgreSQL. This tool is ideal for software engineers seeking an automated solution to transfer data and table structures between these two relational database management systems.
The library offers:
- Easy connection to MySQL and PostgreSQL servers.
- Automated table migration, including columns and data types.
- Efficient connection and error handling.
- A modular and extensible design.
Reserved Word Dictionary
During migration, certain data types in MySQL do not have an exact equivalent in PostgreSQL. Below is a summary of the conversions performed by the library:
| MySQL Type | PostgreSQL Type | Description |
|---|---|---|
INT |
INTEGER |
Fixed-size integers. |
VARCHAR(n) |
TEXT |
PostgreSQL does not require strict text limits. |
TEXT |
TEXT |
Maintains the same type for long text fields. |
FLOAT |
REAL |
Floating-point values. |
DOUBLE |
DOUBLE PRECISION |
Higher precision floating-point values in PostgreSQL. |
DATE |
DATE |
Standard date in YYYY-MM-DD format. |
DATETIME |
TIMESTAMP |
Date and time with timezone. |
TINYINT(1) |
BOOLEAN |
Interpreted as a logical value (TRUE or FALSE). |
ENUM |
TEXT |
Converted to text since PostgreSQL does not directly support the ENUM type. |
Code Details
General Structure
The library follows a modular approach. Each functionality is defined in a specific file:
connect_mysql.py: Handles connection to a MySQL server.connect_postgresql.py: Handles connection to a PostgreSQL server.migrator.py: Orchestrates the migration of tables and data.
Changes in Data Types
The logic to convert MySQL data types to PostgreSQL is located in migrator.py. Here is the key code fragment with explanation:
# Data type mapping
if "int" in column_type:
postgres_type = "INTEGER"
elif "varchar" in column_type or "text" in column_type:
postgres_type = "TEXT"
elif "float" in column_type or "double" in column_type:
postgres_type = "REAL"
elif "date" in column_type:
postgres_type = "DATE"
elif "tinyint(1)" in column_type:
postgres_type = "BOOLEAN"
else:
postgres_type = "TEXT" # Default type if no specific mapping is found
Example Code
Here is a functional example demonstrating how to use the library to migrate all tables from a MySQL database to PostgreSQL:
from mysqlmigratorpostgresql import MysqlMigratorPostgreSQL
# Instantiate the migrator
migrator = MysqlMigratorPostgreSQL()
# Connect to MySQL
migrator.connect_mysql(
host="localhost",
port=3306, # Default MySQL port
user="root",
password="password", # Replace with your password
database="databases_name"
)
# Connect to PostgreSQL
migrator.connect_postgresql(
host="localhost",
port=5432, # Default PostgreSQL port
user="postgres",
password="password", # Replace with your password
database="databases_name"
)
# Migrate all tables
migrator.migrate_all()
# Close connections
migrator.close_connections()
Release files for mysqlmigratorpostgresql 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| mysqlmigratorpostgresql-0.1.0.tar.gz | 5.5 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| mysqlmigratorpostgresql-0.1.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 12.0 kB
Release files / mysqlmigratorpostgresql-0.1.0.tar.gz
| Download URL | mysqlmigratorpostgresql-0.1.0.tar.gz |
|---|---|
| Size | 5.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
22d055f8b0e430e591147116207ef0173a6a81938d8d6c6acdbf8a3b8ce491e7
|
|
BLAKE2b-256 checksum How to use checksums |
40373ab2e3f2ca4d22c358620d8ad1945ec9cc8226b307b55908d139f91767c2
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.0.1 CPython/3.11.9
|
Release files / mysqlmigratorpostgresql-0.1.0-py3-none-any.whl
| Download URL | mysqlmigratorpostgresql-0.1.0-py3-none-any.whl |
|---|---|
| Size | 6.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
1d659d877d87129dac69289c414718c421d09f85ce0dbbfe515a78a156b1c955
|
|
BLAKE2b-256 checksum How to use checksums |
2472c5d7689c8246877cb69d88fadfb3e72b4bf3c6bd4917bec82e1bcd119d9f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.0.1 CPython/3.11.9
|