Skip to content

Instantly share code, notes, and snippets.

@tristanm
Last active November 22, 2024 15:47
Show Gist options
  • Save tristanm/a2afa29ac6f37bf92b46 to your computer and use it in GitHub Desktop.
Save tristanm/a2afa29ac6f37bf92b46 to your computer and use it in GitHub Desktop.

Revisions

  1. tristanm revised this gist May 8, 2015. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions pgloader-commands
    Original file line number Diff line number Diff line change
    @@ -9,4 +9,5 @@ LOAD DATABASE
    -- 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;
  2. tristanm revised this gist May 8, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -2,7 +2,7 @@

    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_!
    No warranties, guarantees, support etc. Use at your own risk and, as always, __ENSURE YOU MAKE BACKUPS FIRST__!

    I chose [`pgloader`][1] because it's extremely fast. YMMV.

  3. tristanm created this gist May 8, 2015.
    22 changes: 22 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,22 @@
    # 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`][1] 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][2] as a starting point.
    3. Run `rake db:create` to generate your shiney new PostgreSQL DB.
    4. Go through `db/schema.rb` and ensure no `limit` options exist on boolean columns (a bug which has been [fixed][3] but not released).
    5. Run `rake db:schema:load` to setup the schema.
    6. Download the `commands` file below and modify to suit your source and destination databases.
    7. Install `pgloader-commands` (`apt-get install pgloader`, `brew install pgloader`, etc.).
    8. Run `pgloader --verbose commands`, sit back and enjoy the show.

    Please leave comments with your own experiences!

    [1]: https://github.com/dimitri/pgloader
    [2]: https://github.com/rails/rails/blob/master/railties/lib/rails/generators/rails/app/templates/config/databases/postgresql.yml
    [3]: https://github.com/rails/rails/pull/19066
    12 changes: 12 additions & 0 deletions pgloader-commands
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,12 @@
    -- 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;