Skip to main content

VillageSQL is a drop-in replacement for MySQL with extensions.

All examples in this guide work on VillageSQL. Install Now →
PostgreSQL and MySQL share ANSI SQL syntax but diverge in enough ways that a migration requires careful attention to data types, default behaviors, and SQL dialect differences. Most of the work is schema translation and fixing queries that rely on PostgreSQL-specific features.

Data Type Mapping

Sequences vs AUTO_INCREMENT

PostgreSQL uses sequences (independent objects) for auto-increment values. MySQL uses AUTO_INCREMENT as a column attribute: PostgreSQL:
MySQL equivalent:
To get the last inserted ID in MySQL: SELECT LAST_INSERT_ID(); (equivalent to PostgreSQL’s currval() or RETURNING id).

SQL Dialect Differences

String concatenation:
String quoting:
MySQL accepts double quotes for identifiers only when ANSI_QUOTES SQL mode is enabled. ILIKE (case-insensitive LIKE):
MySQL string comparisons are case-insensitive by default with utf8mb4_general_ci or similar collations. If you need case-sensitive matching, use a _bin collation. RETURNING clause:
LIMIT / OFFSET:
Schemas vs databases: PostgreSQL uses schemas within a database (mydb.public.orders). MySQL uses “schemas” and “databases” interchangeably — there’s no concept of a schema within a database. What PostgreSQL calls a schema, MySQL calls a database. NULL handling in UNIQUE indexes: PostgreSQL allows multiple NULL values in a unique column (NULLs are not equal to each other). MySQL (InnoDB) also allows multiple NULLs in unique indexes — behavior is the same. CTEs: MySQL supports CTEs including recursive CTEs. See CTEs in MySQL. Window functions: MySQL supports window functions. See Window Functions in MySQL.

Features Without MySQL Equivalents

Some PostgreSQL features have no direct equivalent:

Migration Approach

  1. Export the schema from PostgreSQL (pg_dump --schema-only) and translate each table manually, using the type mapping above.
  2. Export the data from PostgreSQL as CSV (COPY table TO '/tmp/table.csv' CSV HEADER).
  3. Load the data into MySQL with LOAD DATA INFILE or mysqlimport.
  4. Test queries — find all PostgreSQL-specific syntax and rewrite it.
  5. Verify counts and checksumsSELECT COUNT(*) on every table; spot-check key rows.

Frequently Asked Questions

Does MySQL support UPSERT like PostgreSQL’s ON CONFLICT?

Yes, using different syntax. See UPSERT in MySQL. MySQL’s INSERT ... ON DUPLICATE KEY UPDATE and REPLACE INTO cover the same use case as PostgreSQL’s ON CONFLICT DO UPDATE and ON CONFLICT DO NOTHING.

How do I handle PostgreSQL’s BOOLEAN columns in MySQL?

Use TINYINT(1). Store 1 for true and 0 for false. Most MySQL client libraries and ORMs handle this automatically and present TINYINT(1) columns as booleans. You can also use BIT(1) but TINYINT(1) has broader tooling support.

Troubleshooting

See also