Skip to content

Instantly share code, notes, and snippets.

@mrsweaters
Forked from tristanm/README.md
Last active July 24, 2017 19:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrsweaters/b988e646cf2d9fb1091f399512a77307 to your computer and use it in GitHub Desktop.
Save mrsweaters/b988e646cf2d9fb1091f399512a77307 to your computer and use it in GitHub Desktop.
Migrating a Rails project from MySQL to PostgreSQL

Migrating a Rails project from MySQL to PostgreSQL

This brief guide is written from my own experience with migrating a large (~5GB) MySQL database to PostgreSQL for a Rails project.

No warranties, guarantees, support etc. Use at your own risk and, as always, ENSURE YOU MAKE BACKUPS FIRST!

I chose pgloader because it's extremely fast. YMMV.

  1. Replace mysql2 gem with pg in Gemfile.
  2. Update config/database.yml for PostgreSQL. I used Rails' template as a starting point.
  3. Run bin/rails db:create to generate your shiney new PostgreSQL DB.
  4. Remove options: "ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci" from schema.
  5. Remove id: integer from create_table commands in schema.
  6. Run bin/rails db:schema:load to setup the schema.
  7. Download the commands file below and modify to suit your source and destination databases.
  8. Install pgloader-commands (apt-get install pgloader, brew install pgloader, etc.).
  9. Run pgloader --verbose pgloader-commands, sit back and enjoy the show.
-- See https://github.com/dimitri/pgloader/blob/master/pgloader.1.md for
-- connection string options.
LOAD DATABASE
FROM mysql://root@localhost/source_database
INTO postgresql:///destination_database
-- data-only: We don't need pgloader to touch the schema as Rails does a better
-- job using rake db:schema:load.
-- truncate: Ensure all tables are empty first (especially schema_migrations).
-- WARNING: THIS WILL SMOKE YOUR DATABASE!
WITH data only, truncate;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment