The Wild

MySQL to PostgreSQL Migration: Zero Downtime Guide

· 5 min read · Mammals

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

This guide walks you through migrating a live MySQL database to PostgreSQL without taking your application offline. The core strategy is to run both databases in parallel, continuously sync changes, then switch traffic atomically. You'll need a working PostgreSQL instance, a tool for logical replication (like pg_chameleon or Debezium), and the ability to modify your application's connection settings briefly.

What You'll Need

  • MySQL instance (source) with binary logging enabled (binlog_format=ROW).
  • PostgreSQL instance (target) with sufficient disk and memory.
  • A migration tool: pg_chameleon (simplest for full schema + data + replication) or Debezium (more flexible, stream-based).
  • Schema conversion tool: pgloader or manual conversion for complex types.
  • Access to application configuration files (to change DB connection).
  • A maintenance window of a few minutes for the final cutover (not for the whole migration).

Step-by-Step

  1. Prepare the source MySQL: Ensure binary logging is enabled and set to ROW format. Verify with SHOW VARIABLES LIKE 'binlog_format';. If not, enable it and restart MySQL (this is the only downtime, done weeks before).
  2. Create a MySQL user for replication: Grant REPLICATION SLAVE, SELECT, and ALL on the database to a dedicated user (e.g., migrator).
  3. Set up PostgreSQL: Create the target database and user. Tune max_replication_slots and max_wal_senders if using Debezium.
  4. Convert the schema: Use pgloader to convert the MySQL schema to PostgreSQL. Run: pgloader mysql://user:pass@host/dbname pgsql://user:pass@pg_host/dbname. Review the generated schema for data type differences (e.g., TINYINT to SMALLINT, DATETIME to TIMESTAMP). Adjust manually if needed.
  5. Initial data load: Use pgloader to copy all existing data. This will take time, but the application continues running on MySQL. Ensure no writes are missed by noting the binlog position before the load.
  6. Start continuous replication: Configure pg_chameleon or Debezium to read MySQL binlogs and apply changes to PostgreSQL. For pg_chameleon, run chameleon init, then chameleon start_replica. For Debezium, set up a connector via Kafka Connect.
  7. Validate data consistency: Compare row counts and checksums on key tables. Use tools like pt-table-checksum (for MySQL) and custom queries on PostgreSQL. Also verify that new writes to MySQL appear in PostgreSQL within seconds.
  8. Prepare application for cutover: Update your application configuration to point to PostgreSQL, but keep the MySQL connection string as a fallback. Test the new connection in a staging environment.
  9. Stop writes to MySQL (briefly): Put your application in read-only mode or stop it for a minute. This is the only downtime. Wait for replication to catch up (lag should be zero).
  10. Switch traffic: Change the application connection to PostgreSQL. Enable writes. Verify that the application works correctly against PostgreSQL.
  11. Stop replication: Once stable, stop the replication process. Keep MySQL as a backup for a few days.

Common Mistakes

  • Not enabling binlog_format=ROW before starting, causing incomplete replication.
  • Ignoring data type differences (e.g., MySQL's ENUM vs PostgreSQL's VARCHAR with CHECK constraint).
  • Forgetting to convert stored procedures, triggers, and views manually—they don't port automatically.
  • Testing the cutover only once; you should practice the full procedure multiple times.
  • Not monitoring replication lag during the initial load; if it falls too far behind, you'll have a longer cutover.

Tips

  • Start with a small, non-critical database to practice the entire process.
  • Use a tool like pgloader for the initial schema and data conversion—it handles many edge cases automatically.
  • Before cutover, run a full application test suite against PostgreSQL in a staging environment.
  • Keep the MySQL instance running for at least a week after cutover to allow rollback if issues arise.
  • If using Debezium, consider using a schema registry to handle schema changes during replication.

Troubleshooting

  • Replication lag increases: Check network bandwidth and disk I/O. Large transactions on MySQL can cause lag. Consider batching writes or using a faster network.
  • Data type conversion errors: pgloader may fail on unsupported types. Manually edit the schema before loading, or use a custom type mapping.
  • Replication stops with an error: Check logs. Common issues: missing tables, incompatible column definitions, or permission errors. Fix the issue and restart the replication from the last known good position.
  • Application fails after switch: Check for SQL syntax differences (e.g., MySQL's LIMIT vs PostgreSQL's LIMIT is fine, but GROUP BY is stricter). Use a query logger to find problematic queries.
  • Connection pool exhaustion: After switching, your connection pool may need tuning because PostgreSQL uses more connections per query. Adjust pool size accordingly.

Frequently Asked Questions

Can I do this without any downtime at all?

Technically, you can avoid downtime entirely by using a proxy that routes reads to both databases and writes only to MySQL, then switch writes after replication catches up. However, this is complex and risky. The brief pause (a few seconds) during cutover is usually acceptable.

What if my application uses MySQL-specific features like ON DUPLICATE KEY UPDATE?

You'll need to rewrite those queries to PostgreSQL's INSERT ... ON CONFLICT DO UPDATE. This is a common migration task and should be done before the cutover.

How long does the initial data load take?

It depends on data size and network speed. For a 100GB database, it can take several hours. The key is that the application remains live on MySQL during this time.

Can I roll back if something goes wrong after cutover?

Yes, keep MySQL running and stop replication. Then switch the application back to MySQL. You may lose any writes made to PostgreSQL after cutover, but you can replay them from PostgreSQL's logs if needed.

Do I need to convert stored procedures manually?

Yes, stored procedures and triggers are not automatically converted. You'll need to rewrite them in PL/pgSQL. This is often the most time-consuming part.

Is it safe to use pg_chameleon for production?

pg_chameleon is mature and used in production, but it's less flexible than Debezium. For complex environments, Debezium is more robust. Test thoroughly before committing.