Skip to content

Instantly share code, notes, and snippets.

@krisleech
Last active December 12, 2023 23:36
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save krisleech/b7ceb46e4f98c223224001f351f60300 to your computer and use it in GitHub Desktop.
Save krisleech/b7ceb46e4f98c223224001f351f60300 to your computer and use it in GitHub Desktop.
Convert Postgres to MySQL

Software:

Steps

  • dump and gzip postgres (source)
pg_dump -Ox -T sessions -U mos mos_staging > ~/mos_staging.sql
gzip mos_staging.sql
  • scp down and decompress to dev machine
scp USER@DOMAIN:mos_staging.sql.gz .
  • migrate MySQL database (target) [rails only]
rake db:drop db:create db:migrate

Ensure to add a new migration first which changes the encoding, such as:

# db/migrate/20000000000000_change_encoding.rb

class ChangeEncoding < ActiveRecord::Migration
  def change
    execute("ALTER DATABASE mos_staging CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;")
  end
end

Truncate the schema_migrations table.

TRUNCATE `mos_staging`.`schema_migrations`;

Now we have the source data dump and the target schema.

  • Create source database and import dump
createdb -U postgres mos_staging
psql -U postgres mos_staging < mos_staging.sql
  • Create target database (MySQL)
create DATABASE mos_staging

I recommend [mycli] as a client.

  • Migrate the data from source (postgres) to target (mysql)

    • Open the "MySQL Workbench" application
    • Database > Migration Wizard > Start Migration
    • Enter source details and remove Driver entry, leaving it blank (important!)
    • Fill in the default databases for both source and target
    • Press next a few times
    • Tick "Create schema in target RDBMS"
    • Tick "Keep schemas if they already exist"
    • Tick "Truncate target tables before copying"
  • Wait.

  • dump and gzip MySQL database

  • scp up and decompress dump

  • Import dump into MySQL (on server)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment