Video in progress

How do you plan a migration project from MySQL to PostgreSQL?

I work on three fronts: matching the schema and types, finding MySQL-specific SQL and behavior in the application code, and moving the data continuously through CDC and doing a short switchover. Most of the time goes not to moving the data but to finding and testing behavior differences.

📖 Answer

Schema. Match the types one by one: TINYINT(1) → boolean, DATETIME → timestamptz (after settling the time zone), AUTO_INCREMENT → identity, ENUM → text + CHECK, and the utf8 (3-byte) issues. Collation: by default MySQL is case-insensitive and PostgreSQL is case-sensitive, so WHERE email = ? and unique indexes start behaving differently. In practice the most bugs come from here; it's closed off with citext or lower() indexes.

Application. Native queries: backticks, LIMIT x, y, ON DUPLICATE KEY UPDATE → ON CONFLICT, strict GROUP BY (wrong queries MySQL allowed fail in PostgreSQL), IFNULL, date functions. The Hibernate dialect changes, and I re-examine the ID generation strategy. All the integration tests move to PostgreSQL in Testcontainers.

Transaction behavior. InnoDB's default is REPEATABLE READ with gap locks, while PostgreSQL's is READ COMMITTED. Some code may unknowingly rely on MySQL's locking behavior, so I check concurrent flows separately.

Data. An initial load + continuous synchronization with Debezium CDC. While the two databases run in parallel, I compare the row counts and checksums per table. Gradually routing read traffic to the new database and comparing the results (shadow reads) also helps.

Switchover. Stop writes briefly, CDC lag at zero, align the sequences, switch the application. The way back is reverse CDC, for at least a week or two.