Migrate from MySQL to PostgreSQL on Linux (Kubuntu)

前端 未结 4 1417
孤独总比滥情好
孤独总比滥情好 2020-12-29 07:43

A long time ago on a system far, far away...

Trying to migrate a database from MySQL to PostgreSQL. All the documentation I have read covers, in great detail, how

4条回答
  •  失恋的感觉
    2020-12-29 08:31

    If you've converted a schema then migrating data would be the easy part:

    • dump schema from PostgreSQL (you said that you've converted schema to postgres, so we will dump it for now, as we will be deleting and recreating target database, to have it cleaned):

      pg_dump dbname > /tmp/dbname-schema.sql
      
    • split schema to 2 parts — /tmp/dbname-schema-1.sql containing create table statements, /tmp/dbname-schema-2.sql — the rest. PostgreSQL needs to import data before foreign keys, triggers etc. are imported, but after table definitions are imported.

    • recreate database with only 1 part of schema:

      drop database dbname
      create database dbname
      \i /tmp/dbname-schema-1.sql
      -- now we have tables without data, triggers, foreign keys etc.
      
    • import data:

      (
         echo 'start transaction';
         mysqldump --skip-quote-names dbname | grep ^INSERT;
         echo 'commit'
      ) | psql dbname
      -- now we have tables with data, but without triggers, foreign keys etc.
      

      A --skip-quote-names option is added in MySQL 5.1.3, so if you have older version, then install newer mysql temporarily in /tmp/mysql (configure --prefix=/tmp/mysql && make install should do) and use /tmp/mysql/bin/mysqldump.

    • import the rest of schema:

      psql dbname
      start transaction
      \i /tmp/dbname-schema-2.sql
      commit
      -- we're done
      

提交回复
热议问题