Migrating from MySQL to PostgreSQL is not as straight forward as it sounds. As you may or may not know, MySQL’s ‘mysqldump’ utility comes with a “compatible” option where you can specific “postgresql” but the catch is this: the resulting SQL is not properly escaped and will need it’s data types augmented.
In other words… or in my words, the SQL generated from mysqldump that’s meant to be compatible with postgresql is basically worthless in it’s exported state.
How I Migrated
Here is what I did to migrate my MySQL data – a few million rows clocking in just under 1 gig.
Install the mysql2postgres gem.
Before you just go and clone the gem to your local system, take a look @ the forks as they might have something interesting you want. At the time of this writing, maxlapshin’s repo had like 5 or 6 outstanding pull requests. My example code will just use his repo as the clone source.
After installation, the ‘mysql2psql’ command should be available. If it’s not, just close your terminal, open a new one and navigate back to where you cloned the gem.
By default you won’t have a configuration file. Run the ‘mysql2psql’ command and it will generate a default one for you. Once you do that, just open it up and configure it to your heart’s desires.
Once you open the configuration file, just read it top to bottom. Everything is commented and it’s very straight forward stuff. Fill in your MySQL details, then fill in your postgres details (or use the file: option).
Last part is to run the migration.
Run the Migration
This part will *probably* be as simple as doing this:
If that worked for you then enjoy the sweet sweet glory of being migrated. Go check your data, utf8 encodings and all that fun stuff.
If that did not work out and you got an error like the following… then you might be using a mac, along with homebrew and can easily fix it!
The Fix (aka How to Install mysql-connector-c on a mac)
- FIRST get rid of homebrew’s mysql-connector-c with: ‘brew remove mysql-connector-c’
- Go to MySQL’s download page and grab the Connector/C for your flavor of OSX
- Extract it and do the following:
- Copy the lib/ files to /usr/local/lib
- Copy the include/ files to /usr/local/include
- Copy the bin/ files to /usr/local/bin
Note: I’m still not fully sure why this fixed it for me. The version i install from MySQL is the same version Homebrew uses. :/