Migrating data from MySQL to Postgresql

In this document we explain how to migrate from MySQL to Postgresql with different approaches.

Askbot is optimized for Postgresql as search functionality works better with this database engine.

Note

As a general advice, to reduce the database size - run the cleanup management command before starting the migration.

Simple Migration of small database

If your database is small with few users and questions you can follow this steps:

With MySQL as your database engine in your settings.py file run the following command:

python manage.py dumpdata > data.json

After that change your database engine to Postgresql in settings.py and do:

python manage.py migrate --noinput #create the database structure
python manage.py loaddata  data.json

Note

This won’t work with large datasets because django will load all your data into memory and you might run out of memory if the site data is too large.

This process can produce warnings that can be ignored.

Data migration with py-mysql2pgsql

If the database is large this tool will come handy, to install it run:

pip install py-mysql2pgsql

Create a configuration file called config.yml with the following contents:

mysql:
  hostname: localhost
  port: 3306
  username: your_user
  password: your_password
  database: your_database

destination:
  file:
  postgres:
    hostname: localhost
    port: 5432
    username: your_user
    password: your_password
    database: your_database

Then run:

py-mysql2pgsql -v -f config.yml

The script will start migrating the data and might take a while, depending on the database size.

After the process is finished there are a couple of things left to do.

Fixing data types

The py-mysql2pgsql translates datatype a bit different than Django ORM do, to keep the same datatypes do the following:

  1. Create a new postgresql database and run sync and migrate commands the following way:

    python manage.py migrate --noinput --no-initial-data
    
  2. Dump the converted database data with binary format:

    pg_dump --format=c -a database_name > dump_name
    
  3. Restore it into your current Django database:

    pg_restore -a --disable-triggers -d django_database dump_name