Migrate from MySQL to PostgreSQL on Linux (Kubuntu)

前端 未结 4 1431
孤独总比滥情好
孤独总比滥情好 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:10

    Convert the mysqldump file to a PostgreSQL-friendly format

    Convert the data as follows (do not use mysql2pgsql.perl):

    1. Escape the quotes.

      sed "s/\\\'/\'\'/g" climate-my.sql | sed "s/\\\r/\r/g" | sed "s/\\\n/\n/g" > escaped-my.sql

    2. Replace the USE "climate"; with a search path and comment the comments:

      sed "s/USE \"climate\";/SET search_path TO climate;/g" escaped-my.sql | sed "s/^\/\*/--/" > climate-pg.sql

    3. Connect to the database.

      sudo su - postgres
      psql climate

    4. Set the encoding (mysqldump ignores its encoding parameter) and then execute the script.

      \encoding iso-8859-1
      \i climate-pg.sql

    This series of steps will probably not work for complex databases with many mixed types. However, it works for integers, varchars, and floats.

    Indexes, primary keys, and sequences

    Since mysqldump included the primary keys when generating the INSERT statements, they will trump the table's automatic sequence. The sequences for all tables remained 1 upon inspection.

    Set the sequence after import

    Using the ALTER SEQUENCE command will set them to whatever value is needed.

    Schema Prefix

    There is no need to prefix tables with the schema name. Use:

    SET search_path TO climate;
    

提交回复
热议问题