Import MySQL dump to PostgreSQL database

后端 未结 17 846
北海茫月
北海茫月 2020-12-04 10:05

How can I import an \"xxxx.sql\" dump from MySQL to a PostgreSQL database?

17条回答
  •  抹茶落季
    2020-12-04 10:22

    As with most database migrations, there isn't really a cut and dried solution.

    These are some ideas to keep in mind when doing a migration:

    1. Data types aren't going to match. Some will, some won't. For example, SQL Server bits (boolean) don't have an equivalent in Oracle.
    2. Primary key sequences will be generated differently in each database.
    3. Foreign keys will be pointing to your new sequences.
    4. Indexes will be different and probably will need tweaked.
    5. Any stored procedures will have to be rewritten
    6. Schemas. Mysql doesn't use them (at least not since I have used it), Postgresql does. Don't put everything in the public schema. It is a bad practice, but most apps (Django comes to mind) that support Mysql and Postgresql will try to make you use the public schema.
    7. Data migration. You are going to have to insert everything from the old database into the new one. This means disabling primary and foreign keys, inserting the data, then enabling them. Also, all of your new sequences will have to be reset to the highest id in each table. If not, the next record that is inserted will fail with a primary key violation.
    8. Rewriting your code to work with the new database. It should work but probably won't.
    9. Don't forget the triggers. I use create and update date triggers on most of my tables. Each db sites them a little different.

    Keep these in mind. The best way is probably to write a conversion utility. Have a happy conversion!

提交回复
热议问题