Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save barseghyanartur/56876ab3acbd3d5d6ab7dcc477c29238 to your computer and use it in GitHub Desktop.
Save barseghyanartur/56876ab3acbd3d5d6ab7dcc477c29238 to your computer and use it in GitHub Desktop.
Convert MySQL dump to PostgreSQL dump

Prepare database dump using mysqldump:

mysqldump -u [username] -p [databaseName] --compatible=postgresql --default-character-set=utf8 > database.sql

Clone mysql-postgresql-converter project:

git clone git@github.com:lanyrd/mysql-postgresql-converter.git
cd mysql-postgresql-converter

Assuming that your mysql/mariadb dump is named database.sql, place that file into the mysql-postgresql-converter directory.

Convert mysql dump to postgresql dump:

python db_converter.py database.mysql database.psql

Replace datetime field with timestamp field.

sed -i 's/datetime(6)/timestamp with time zone/' database.psql

Import dump into postgres:

psql -U dbname -f database.pgsql
@sajid-nltsf
Copy link

Line 25 (of 2738: 0.91%) [0 tables] [0 inserts] [ETA: 0 min 0 sec]Traceback (most recent call last):
File "db_converter.py", line 225, in
parse(sys.argv[1], sys.argv[2])
File "db_converter.py", line 81, in parse
current_table = line.split('"')[1]
IndexError: list index out of range

@barseghyanartur
Copy link
Author

barseghyanartur commented Jan 26, 2022

@sajid-nltsf:

It used to work back then. Raise an issue in the lanyrd/mysql-postgresql-converter.

@feyton
Copy link

feyton commented Jul 19, 2023

This work but a point to note is that I faced the issue when working with python3. It only worked when using python2 which I verified multiple times in a docker container

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment