The Short Answer
For a greenfield web application in 2026, choose PostgreSQL unless something specific in your situation argues otherwise. MySQL 8.0 closed most of the gap that used to justify the choice — window functions, common table expressions and expression indexes all landed in 8.0 — so the old argument about "Postgres is richer" is mostly settled. What remains is a set of narrower advantages, and they matter more than people expect: a JSON type you can index for containment queries, partial indexes, real row-level security, and a planner that will keep using an index correctly as your table grows.
The honest counterweight is operational. If your team already runs MySQL fluently, or you inherit a WordPress estate, or your provider's managed offering for MySQL is materially cheaper with better replication tooling, that is a legitimate reason to pick MySQL and it will cost you very little in practice.
Side by Side
| Concern | PostgreSQL | MySQL 8.0 / InnoDB |
|---|---|---|
| JSON storage | jsonb, binary, decomposed, containment-queryable | json, binary, extracted as text |
| Indexing JSON | Native GIN/GiST, operator classes | Expression index on a generated column |
| Partial indexes | Yes, WHERE clause on the index | No; emulate with a generated column |
| Expression indexes | Any immutable expression | Yes, since 8.0.13 |
| Generated columns | GENERATED ALWAYS AS, stored or virtual | GENERATED ALWAYS AS, stored or virtual |
| CTEs and window functions | Since 8.4; non-recursive CTEs inlined when referenced once | Since 8.0; materialised before 8.0.21 |
| Upsert | ON CONFLICT ... DO UPDATE, explicit target | ON DUPLICATE KEY UPDATE, any unique key |
| Row-level security | Native policies per table | Not available |
| Read isolation default | READ COMMITTED, no gap locks | REPEATABLE READ, next-key gap locks |
| Text search | tsvector/GIN, ranked, dictionaries | FULLTEXT, ranked, per-index parser |
| Replication ecosystem | Logical replication, pg_cron, third-party CDC | Group Replication, InnoDB Cluster, mature binlog tooling |
| Managed availability | RDS, Cloud SQL, Azure, Neon, Supabase | RDS, Cloud SQL, Azure, PlanetScale, Vitess |
The row-level security row is the one that decides more projects than the rest of the table combined. Postgres policies live in the database and apply to every client, including a misconfigured one; see the PostgreSQL documentation on row security for the exact semantics. MySQL has no equivalent, which is precisely why building a multi-tenant Next.js app is simpler on Postgres — tenant isolation becomes a policy instead of a clause you must remember on every query.
JSON, and Where Postgres Wins for Real
The JSON difference is not "one has JSON, the other does not". MySQL's JSON type has existed since 5.7 and stores documents in an internal binary format. The difference is how you query them.
jsonb stores a decomposed binary form and supports containment queries with the @ operator, plus a GIN index that makes them fast:
sqlalter table events enable row level security; create index events_payload_gin on events using gin (payload jsonb_path_ops); select id, created_at, payload->>'user_id' as user_id from events where payload @> '{"type": "purchase", "currency": "GBP"}' order by created_at desc limit 50;
jsonb_path_ops produces a smaller index than the default jsonb_ops and is much faster for containment queries, at the cost of not supporting key-existence operators. Choose it when your access pattern is "does this document contain this fragment", which for an events table is nearly always.
MySQL needs the expression to be extracted into an indexable string first. You can attach a virtual generated column to the JSON path and index that:
sqlalter table events add column event_type varchar(64) generated always as (payload->>'$.type') stored, add index idx_events_type_created (event_type, created_at desc); select id, created_at from events where event_type = 'purchase' order by created_at desc limit 50;
That works, and for equality filtering it is not much slower than the GIN index. It is strictly less capable: there is no equivalent of containment, so "events where the payload has a plan key" requires an expression the optimiser will not index usefully. jsonb semantics are described in the Postgres JSON types documentation; MySQL's are in its JSON documentation.
The trap on both sides is the same: JSON is not a substitute for a relational column. A column you filter, sort and aggregate on should be a column. We have migrated projects where jsonb was holding a boolean that three services read differently, and the fix was always to promote it back to a real column.
Indexing Differences That Change Query Plans
Partial indexes
Postgres can index a subset of a table. This is the single feature we miss most on MySQL, because it makes indexes dramatically smaller and keeps them hot:
sql-- Only 2% of orders are open, so this index stays tiny and cache-resident create index idx_orders_open_created on orders (created_at desc) where status in ('pending', 'paid'); explain (analyze, buffers) select id, total_cents from orders where status = 'paid' and created_at >= now() - interval '30 days' order by created_at desc limit 50;
textIndex Scan using idx_orders_open_created on orders (cost=8.45..1290.20 rows=812 time=0.412..1.884) Index Cond: (created_at >= (now() - '30 days'::interval)) Buffers: shared hit=61 Planning Time: 0.214 ms Execution Time: 2.031 ms
Buffers: shared hit=61 is the number that matters: the whole query was answered from cache. Without the partial index, the planner has two bad options — a sequential scan across all 8.4M rows, or a bitmap scan over idx_orders_status_created that touches a quarter of the heap. Partial indexes are documented in Postgres's index chapter.
MySQL has no partial index. The emulation below is correct and almost useless:
sqlalter table orders add column is_open tinyint generated always as (if(status in ('pending','paid'), 1, null)) stored, add index idx_orders_open_created (is_open, created_at desc); select id, total_cents from orders use index (idx_orders_open_created) where is_open = 1 and created_at >= now() - interval '30 days' order by created_at desc limit 50;
Generating NULL rather than 0 for closed orders does let MySQL exclude those entries from the index, which recovers most of the benefit. But it is a trick that a reviewer has to understand, and it silently stops working if someone later changes the column to not null. If partial indexes matter to your design, that is a real argument for Postgres.
The one index both engines agree on
A single-column index on a low-cardinality column is frequently a liability on either engine. Indexing status where there are five values leaves the planner choosing between a bitmap scan over millions of entries and a sequential scan, and it will sometimes make the wrong call. Composite indexes ordered equality-columns-first, then range or sort columns, are almost always the right answer:
sqlcreate index idx_orders_status_created on orders (status, created_at desc);
CTEs and Generated Columns
Both engines support recursive CTEs and window functions, so the old "you need Postgres for a recursive query" argument expired with MySQL 8.0. One difference remains. Postgres 12 and later will inline a non-recursive CTE that is referenced exactly once, letting the planner push down predicates; MySQL materialised every CTE before 8.0.21.
sqlwith monthly_revenue as ( select date_trunc('month', created_at) as month, sum(total_cents) / 100.0 as revenue from orders where status = 'paid' group by 1 ), with_churn as ( select customer_id, count(*) filter (where status = 'cancelled') as cancellations from orders group by 1 ) select m.month, m.revenue, m.revenue - lag(m.revenue) over (order by m.month) as delta from monthly_revenue m order by m.month;
count(*) filter (where ...) is Postgres-only — MySQL spells it sum(status = 'cancelled') — and it reads far better. That is the level of difference you are choosing between: not capability, but ergonomics.
Generated columns, meanwhile, are near-parity. Both support stored and virtual forms; Postgres virtual columns were added in 18, well behind MySQL's 5.7 support. If you rely on one heavily, note that MySQL requires STORED for any indexed generated column and enforces stricter determinism rules.
Upserts Are Not the Same Thing
This is where the two dialects diverge most in practice, because ON CONFLICT and ON DUPLICATE KEY are not the same operation wearing different syntax.
Postgres requires you to name a conflict target, and that target must be backed by a unique or primary key index:
sqlcreate table inventory ( sku text primary key, quantity integer not null default 0, updated_at timestamptz not null default now() ); insert into inventory (sku, quantity) values ('TSHIRT-M', 10) on conflict (sku) do update set quantity = inventory.quantity + excluded.quantity, updated_at = now();
excluded is the pseudo-table holding the row you tried to insert, and referencing the table by name gives you the current stored value. That combination is what makes a correct atomic increment readable. The cost of the explicitness is that Postgres cannot resolve an ON CONFLICT against a partial unique index unless your conflict target repeats the index predicate exactly — a detail that has cost us an afternoon more than once.
MySQL infers the target from any unique key:
sqlinsert into inventory (sku, quantity) values ('TSHIRT-M', 10) on duplicate key update quantity = inventory.quantity + new.quantity, updated_at = current_timestamp;
The AS new row alias is the current spelling and has been since 8.0.20; older code uses the values(quantity) function, which is deprecated and will eventually be removed. Being able to write on duplicate key update without naming a conflict is convenient, and it also means a typo in a column name silently triggers a duplicate rather than a constraint error.
Both behaviours are specified — Postgres in its INSERT reference and MySQL in its INSERT ... ON DUPLICATE KEY UPDATE page — and the difference will bite you if you move a database between them, so know which dialect your migrations are written in before you need to.
Reading the Query Plan
Neither planner can be trusted without evidence. EXPLAIN ANALYZE runs the query and reports real timings; the ANALYZE part is what separates it from EXPLAIN, which only estimates and routinely mis-estimates row counts by orders of magnitude on skewed data.
Two things to look for. A sequential scan on a large table usually means a missing or unusable index, though not always — Postgres will correctly choose a seq scan for a query touching a large fraction of a table, and forcing an index there makes things slower. And row estimates that are wildly off mean stale statistics, fixed with ANALYZE; on Postgres, EXPLAIN even shows the estimate alongside the actual row count when they disagree, which points straight at the missing correlation.
sqlexplain (analyze, buffers, verbose) select o.id, o.total_cents, count(li.id) as item_count from orders o join line_items li on li.order_id = o.id where o.created_at >= now() - interval '7 days' group by o.id, o.total_cents having count(li.id) > 3 order by o.total_cents desc;
MySQL's equivalent is EXPLAIN FORMAT=JSON, which additionally reports rows_examined_per_scan and a cost estimate for each node; the MySQL EXPLAIN format documentation is worth reading once because the JSON output shows far more than the tabular form. The Postgres side is covered in its EXPLAIN guide.
Set log_min_duration_statement in production — 500ms on Postgres, or long_query_time on MySQL — and treat the log as your real query inventory. It is a more honest list of what your application does than anything you will find in the source tree.
When MySQL Is the Right Answer
MySQL wins on operational grounds far more often than on technical ones, and that is fine:
- You already run it. Team fluency beats a feature table. A Postgres win that requires three people to learn
EXPLAINis not a win. - Content management estates. WordPress, Drupal and most PHP frameworks are tested against MySQL first. Matching the reference environment saves a category of bug.
- Replication tooling. If you need multi-primary or read-your-writes across regions, Vitess and the sharding ecosystem around MySQL are more mature than anything comparable on Postgres.
- Managed value. A managed MySQL offering that includes automated failover and point-in-time recovery at half the price changes the arithmetic. Our cloud and DevOps practice is largely about making exactly this comparison honestly, including the parts of it vendors do not put on the pricing page.
Whatever you pick, write the schema deliberately rather than letting an ORM default decide it — normalisation, key choice and index rationale are covered in how to design a database schema that holds up. If the decision is tied to a broader product build, what SaaS development involves covers where the database fits, and you can send us the specifics for a direct recommendation.