Migrating from MySQL to PostgreSQL [closed] Migrating from MySQL to PostgreSQL [closed] postgresql postgresql

Migrating from MySQL to PostgreSQL [closed]


Steve, I had to migrate my old application the way around, that is PgSQL->MySQL. I must say, you should consider yourself lucky ;-)Common gotchas are:

  • SQL is actually pretty close to language standard, so you may suffer from MySQL's dialect you already know
  • MySQL quietly truncates varchars that exceed max length, whereas Pg complains - quick workaround is to have these columns as 'text' instead of 'varchar' and use triggers to truncate long lines
  • double quotes are used instead of reverse apostrophes
  • boolean fields are compared using IS and IS NOT operators, however MySQL-compatible INT(1) with = and <> is still possible
  • there is no REPLACE, use DELETE/INSERT combo
  • Pg is pretty strict on enforcing foreign keys integrity, so don't forget to use ON DELETE CASCADE on references
  • if you use PHP with PDO, remember to pass a parameter to lastInsertId() method - it should be sequence name, which is created usually this way: [tablename]_[primarykeyname]_seq

I hope that helps at least a bit. Have lots of fun playing with Postgres!


I have done a similar conversion, but for different reasons. It was because we needed better ACID support, and the ability to have web users see the same data they could via other DB tools (one ID for both).

Here are the things that bit us:

  1. MySQL does not enforce constraintsas strictly as PostgreSQL.
  2. There are different date handling routines. These will need to be manually converted.
  3. Any code that does not expect ACIDcompliance may be an issue.

That said, once it was in place and tested, it was much nicer. With correct locking for safety reasons and heavy concurrent use, PostgreSQL performed better than MySQL. On the things where locking was not needed (read only) the performance was not quite as good, but it was still faster than the network card, so it was not an issue.

Tips:

  • The automated scripts in the contribdirectory are a good starting pointfor your conversion, but will needto be touched a little usually.
  • I would highly recommend that youuse the serializable isolationlevel as a default.
  • The pg_autodoc tool is good toreally see your data structures andhelp find any relationships youforgot to define and enforce.


We did a move from a MySQL3 to PostgreSQL 8.2 then 8.3. PostgreSQL has the basic of SQL and a lot more so if your MYSQL do not use fancy MySQL stuff you will be OK.

From my experience, our MySQL database (version 3) doesn't have Foreign Key... PostgreSQL lets you have them, so we had to change that... and it was a good thing and we found some mistake.

The other thing that we had to change was the coding (C#) connector that wasn't the same in MySQL. The MySQL one was more stable than the PostgreSQL one. We still have few problems with the PostgreSQL one.