Is there a simple tool to convert mysql to postgresql syntax?
-
01-07-2019 - |
Question
I've tried the tools listed here, some with more success than others, but none gave me valid postgres syntax I could use (tinyint errors etc.)
Solution
There's a mysqldump
option which makes it output PostgreSQL code:
mysqldump --compatible=postgresql ...
OTHER TIPS
After some time on Google I found this post.
- Install the mysql2psql gem using
[sudo] gem install mysql2psql
. - Create a config file by running
mysql2psql
. You'll see an error but amysql2psql.yml
file should have been created. - Edit
mysql2psql.yml
- Run
mysql2psql
again to migrate you data.
Tip: Set force_truncate
to true
in your mysql2psql.yml
config file if you want the postgresql database to be cleared before migrating your data.
I've used py-mysql2pgsql. After installation it needs only simple configuration file in yml format (source, destination), e.g.:
# if a socket is specified we will use that
# if tcp is chosen you can use compression
mysql:
hostname: localhost
port: 3306
socket: /tmp/mysql.sock
username: mysql2psql
password:
database: mysql2psql_test
compress: false
destination:
# if file is given, output goes to file, else postgres
file:
postgres:
hostname: localhost
port: 5432
username: mysql2psql
password:
database: mysql2psql_test
Usage:
> py-mysql2pgsql -h
usage: py-mysql2pgsql [-h] [-v] [-f FILE]
Tool for migrating/converting data from mysql to postgresql.
optional arguments:
-h, --help show this help message and exit
-v, --verbose Show progress of data migration.
-f FILE, --file FILE Location of configuration file (default:
mysql2pgsql.yml). If none exists at that path,
one will be created for you.
More on its home page https://github.com/philipsoutham/py-mysql2pgsql.
There is one piece of pay software listed on this postgresql page: http://www.postgresql.org/download/products/1
and this is on pgFoundry: http://pgfoundry.org/projects/mysql2pgsql/
This page lists the syntax differences, but a simple working query converter i haven't found yet. Using an ORM package instead of raw SQL could prevent these issues.
I'm currently hacking up a converter for a legacy codebase:
function mysql2pgsql($mysql){
return preg_replace("/limit (\d+), *(\d+)/i", "limit $1 offset $2", preg_replace("/as '([^']+)'/i", 'as "$1"', $mysql)); // Note: limit needs order
}
For CREATE
statements, SQLines converts most of them online. I still had to edit the mysqldump afterwards, though:
"mediumtext" -> "text", "^LOCK.*" -> "", "^UNLOCK.*" -> "", "`" -> '"', "'" -> "''" in 'data', "0000-00-00" -> "2000-01-01", deduplicate constraint names, " CHARACTER SET utf8 " -> " ".
"int(10)" -> "int" was missed in the last table, so pass that part of the mysqldump through http://www.sqlines.com/online again.
Try this one , it works like charm !!
http://www.sqlines.com/online
Have a look at PG Foundry, extra utilities for Postgres tend to live there. I believe that the tool you're looking for does exist though.
you will most likely never get a tool for such task which would do all of your job for you. be prepared to do some refactoring work yourself.