MySQL vs PostgreSQL: Which One Should You Use?
MySQL vs PostgreSQL for beginners, run side by side on the same data at their default settings: why MySQL calls You@Example.com a duplicate and PostgreSQL saves it, why 7 / 2 is 3.5 in one and 3 in the other, what a rolled-back migration leaves behind in each, RETURNING and extensions, a process per connection vs a thread, what 120 connections do to each, how fast each one is, and a table for picking between them.

Same SQL, two answers
Your app needs a database: MySQL or PostgreSQL? One email signs up twice, once in capitals. MySQL says: duplicate. PostgreSQL saves both.
mysql> INSERT INTO users (email) VALUES ('you@example.com');Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO users (email) VALUES ('You@Example.com');ERROR 1062 (23000): Duplicate entry 'You@Example.com' for key 'users.email'
shop=> INSERT INTO users (email) VALUES ('you@example.com');INSERT 0 1
shop=> INSERT INTO users (email) VALUES ('You@Example.com');INSERT 0 1
shop=> SELECT * FROM users;id | email ----+----------------- 1 | you@example.com 2 | You@Example.com (2 rows)
In PostgreSQL, one person now has two accounts. We found more surprises: in math, in migrations, and in speed. By the end, you'll know which one to pick.
Text: case and accents
Both tables have one rule: each email can only be used once. So why did only MySQL call it a duplicate?

Ask each one: are these two emails equal? MySQL says one, meaning true. PostgreSQL says f, for false.
mysql> SELECT 'You@Example.com' = 'you@example.com' AS same;+------+ | same | +------+ | 1 | +------+ 1 row in set (0.00 sec)
shop=> SELECT 'You@Example.com' = 'you@example.com' AS same;same ------ f (1 row)
Now cafe, with and without its accent. MySQL: equal again. PostgreSQL: not equal. MySQL is ignoring capitals, and accents too.
mysql> SELECT 'café' = 'cafe' AS same;+------+ | same | +------+ | 1 | +------+ 1 row in set (0.00 sec)
shop=> SELECT 'café' = 'cafe' AS same;same ------ f (1 row)
The reason is a setting called a collation: the rules for comparing and sorting text. MySQL's default collation is what ignored the capitals and the accent. PostgreSQL compares text exactly, letter by letter. MySQL can be strict too: ask it for a case-sensitive collation, and the same two emails are not equal.

Here's how to stop duplicate accounts in PostgreSQL. Add a rule: turn every email into small letters, and no two may match. In SQL, that kind of rule is called a unique index. But PostgreSQL won't create it yet: the duplicate from earlier is already in the table. Delete it, add the rule, then try the capital version again: refused. In PostgreSQL, you add that rule. In MySQL, its collation already did.
shop=> CREATE UNIQUE INDEX users_email_lower ON users (lower(email));ERROR: could not create unique index "users_email_lower" DETAIL: Key (lower(email::text))=(you@example.com) is duplicated.
shop=> DELETE FROM users WHERE email = 'You@Example.com';DELETE 1
shop=> CREATE UNIQUE INDEX users_email_lower ON users (lower(email));CREATE INDEX
shop=> INSERT INTO users (email) VALUES ('You@Example.com');ERROR: duplicate key value violates unique constraint "users_email_lower" DETAIL: Key (lower(email::text))=(you@example.com) already exists.
Bad data and math
Older guides warn that MySQL quietly saves bad data. Not any more. Try saving the word free as an order's total, which has to be a number. Both refuse.
mysql> INSERT INTO orders (email, total) VALUES ('e@example.com', 'free');ERROR 1366 (HY000): Incorrect decimal value: 'free' for column 'total' at row 1
shop=> INSERT INTO orders (email, total) VALUES ('e@example.com', 'free');ERROR: invalid input syntax for type numeric: "free" LINE 1: ...INTO orders (email, total) VALUES ('e@example.com', 'free'); ^
But plain arithmetic is different. Seven divided by two. MySQL: three point five. PostgreSQL: three. Seven and two are both whole numbers, so PostgreSQL gives a whole-number answer, and drops the half. Write two point zero, and you get three point five. So if you work out an average this way, like total stars divided by the number of reviews, PostgreSQL can quietly drop the fraction.
mysql> SELECT 7 / 2;+--------+ | 7 / 2 | +--------+ | 3.5000 | +--------+ 1 row in set (0.00 sec)
shop=> SELECT 7 / 2;?column? ---------- 3 (1 row)
shop=> SELECT 7 / 2.0;?column? -------------------- 3.5000000000000000 (1 row)
A migration that fails halfway
What happens when a change fails halfway? A migration is a script that changes your tables. Ours adds a coupon column, then a rule that every total is above zero. But one order is a free sample, with a total of zero, so the rule will fail. It runs inside a transaction: all the changes happen, or none.

MySQL first. Start the transaction. Add the column: it works. Add the rule: it fails, because of that zero. So roll back: undo the whole group. Now list the columns of orders. Guess first: is the coupon column still there? It is. MySQL saved it the moment that line ran. A change to a table's structure ends the transaction on its own, so the rollback had nothing left to undo. That's not a bug. The MySQL manual says so: alter table always ends the transaction first.
mysql> START TRANSACTION;Query OK, 0 rows affected (0.00 sec)
mysql> ALTER TABLE orders ADD COLUMN coupon VARCHAR(20);Query OK, 0 rows affected (0.00 sec) Records: 0 Duplicates: 0 Warnings: 0
mysql> ALTER TABLE orders ADD CONSTRAINT total_positive CHECK (total > 0);ERROR 3819 (HY000): Check constraint 'total_positive' is violated.
mysql> ROLLBACK;Query OK, 0 rows affected (0.00 sec)
mysql> SELECT column_name FROM information_schema.columns WHERE table_name = 'orders' ORDER BY ordinal_position;+-------------+ | COLUMN_NAME | +-------------+ | id | | email | | total | | coupon | +-------------+ 4 rows in set (0.00 sec)
Now PostgreSQL, the same steps. The column is added. The rule fails on the same zero. Roll back, and list the columns: id, email, total. No coupon. Nothing is left half done. In PostgreSQL, changes to a table's structure are part of the transaction, like any other change. So if a real migration has ten changes and the third one fails, you fix it and run the whole thing again, instead of cleaning up by hand.
shop=> BEGIN;BEGIN
shop=> ALTER TABLE orders ADD COLUMN coupon VARCHAR(20);ALTER TABLE
shop=> ALTER TABLE orders ADD CONSTRAINT total_positive CHECK (total > 0);ERROR: check constraint "total_positive" of relation "orders" is violated by some row
shop=> ROLLBACK;ROLLBACK
shop=> SELECT column_name FROM information_schema.columns WHERE table_name = 'orders' ORDER BY ordinal_position;column_name ------------- id email total (3 rows)
Everyday SQL and extensions
Each database speaks its own version of SQL, and your app gets written in that version, so switching later means rewriting those parts. Two everyday examples. You add a row, and the database picks its id number. You want that id. PostgreSQL hands it straight back: returning id. MySQL doesn't understand the word returning, so it's a syntax error. Instead, you insert, then ask a second question: what was the last id you made?
shop=> INSERT INTO users (email) VALUES ('new@example.com') RETURNING id;id ---- 4 (1 row) INSERT 0 1
mysql> INSERT INTO users (email) VALUES ('new@example.com') RETURNING id;ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'RETURNING id' at line 1
mysql> INSERT INTO users (email) VALUES ('new@example.com');Query OK, 1 row affected (0.00 sec)
mysql> SELECT LAST_INSERT_ID();+------------------+ | LAST_INSERT_ID() | +------------------+ | 3 | +------------------+ 1 row in set (0.00 sec)
Same for an upsert: add a row, or update it if it's already there. MySQL says: on duplicate key update. PostgreSQL says: on conflict, do update. Type MySQL's words into PostgreSQL, and it's a syntax error.
mysql> INSERT INTO products (id, name, price) VALUES (1, 'product 1', 9.99) ON DUPLICATE KEY UPDATE price = 9.99;Query OK, 2 rows affected (0.00 sec)
shop=> INSERT INTO products (id, name, price) VALUES (1, 'product 1', 9.99) ON CONFLICT (id) DO UPDATE SET price = 9.99;INSERT 0 1
shop=> INSERT INTO products (id, name, price) VALUES (1, 'product 1', 9.99) ON DUPLICATE KEY UPDATE price = 9.99;ERROR: syntax error at or near "DUPLICATE" LINE 1: ...id, name, price) VALUES (1, 'product 1', 9.99) ON DUPLICATE ... ^
PostgreSQL also takes add-ons, called extensions, that teach it new tricks. This server comes with forty-seven ready to switch on. One is citext, a text type that ignores capitals: another way to fix our email problem. Another is fuzzy text search, which finds words even when they're misspelled. Bigger ones install separately: PostGIS for maps and distances, and pgvector for AI search by meaning.
shop=> SELECT count(*) FROM pg_available_extensions;count ------- 47 (1 row)
shop=> SELECT name FROM pg_available_extensions WHERE name IN ('citext', 'pg_trgm', 'hstore', 'postgis', 'vector') ORDER BY name;name --------- citext hstore pg_trgm (3 rows)
Connections: processes or threads
In 2016, Uber moved from PostgreSQL to MySQL, and one reason was what happens every time an app connects. PostgreSQL starts a new process for every connection: a separate running program, with its own memory. MySQL runs one program, and gives each connection a thread inside it. A thread is lighter: it shares the program's memory.
Here it is for real. Our app opens fifty connections to PostgreSQL, and holds them. On the server, ps lists the running processes: fifty-one, one for each connection, plus one for psql, the window we've been typing our queries into. Now fifty to MySQL. It's still one program. It was already running thirty-seven threads of its own, and now it has eighty-seven: one more for each connection.
python3 connections.py postgres 50 20postgres: 50 of 50 connections open
ps -e -o pid,cmd | grep '[p]ostgres: app' | head -481 postgres: app shop 172.18.0.5(46664) idle 91 postgres: app shop 172.18.0.5(51098) idle 92 postgres: app shop 172.18.0.5(51110) idle 93 postgres: app shop 172.18.0.5(51120) idle
ps -e -o pid,cmd | grep -c '[p]ostgres: app'51
python3 connections.py mysql 50 20mysql: 50 of 50 connections open
ps -o pid,nlwp,cmd -C mysqldPID NLWP CMD 1 87 mysqld
Each server also has a limit. MySQL allows a hundred and fifty-one connections by default. PostgreSQL, a hundred. So open a hundred and twenty to each. Guess first: which one says no first? PostgreSQL. Our app got ninety-six. One of the other four was our psql window, and the last three are kept spare for administrators. MySQL opened all hundred and twenty. You can raise PostgreSQL's limit, but every extra connection is another process.
mysql> SELECT @@max_connections;+-------------------+ | @@max_connections | +-------------------+ | 151 | +-------------------+ 1 row in set (0.00 sec)
shop=> SHOW max_connections;max_connections ----------------- 100 (1 row)
python3 connections.py postgres 120postgres: 96 of 120 connections open connection 97: FATAL: remaining connection slots are reserved for roles with the SUPERUSER attribute
python3 connections.py mysql 120mysql: 120 of 120 connections open
Picture visitors to a building. PostgreSQL builds every visitor a small house of their own. MySQL gives every visitor a desk in one shared office. Houses cost more, so busy PostgreSQL apps put a front desk at the door: a connection pooler, like PgBouncer. Hundreds of app connections share a few real ones.
Speed
Last, the question everyone asks: is one of them faster? The same small work from the laptop, three runs each. Ten thousand lookups by id: a tie, zero point zero four milliseconds each. Two thousand inserts, each saved on its own: PostgreSQL took 0.19 seconds. MySQL took 0.82, more than four times as long.
python3 speed.py10,000 lookups by id (middle of 3 runs) MySQL 0.40 s 0.04 ms each PostgreSQL 0.40 s 0.04 ms each MySQL, no binlog 0.40 s 0.04 ms each 2,000 inserts, one commit each (middle of 3 runs) MySQL 0.82 s 0.41 ms each PostgreSQL 0.19 s 0.09 ms each MySQL, no binlog 0.23 s 0.12 ms each
That gap is mostly one default. Saving a change for good is called a commit. Both servers write each commit to a log on disk before they say done: MySQL calls it the redo log. MySQL also writes it to a second log, the binary log. Copies of your database, called replicas, read that log to stay in sync. That's one more write to disk on every commit. PostgreSQL's replicas read its one log, so it writes once.

Here's the same MySQL with the binary log switched off: twenty-three hundredths of a second, close to PostgreSQL. But without it you can't have replicas, so leave it on. With it on, MySQL's inserts stay slower, but at under half a millisecond each, a new app won't feel it. And lookups were a tie. Speed won't pick for you.

Which one to pick
So, which one should you use? PostgreSQL does exactly what you typed. That's why it saved both emails, and why it dropped the half. You get fewer hidden surprises, but you write the rules yourself. Its migrations undo cleanly, and it hands back new ids. Pick MySQL when your tools expect it, or when you need lots of connections: desks, not houses.

WordPress, for example, needs MySQL or its close cousin, MariaDB. But starting fresh, with no reason either way? Start with PostgreSQL. Just decide on day one how emails should compare, and add the rule, before any duplicates get in.

If you now know which database to install, subscribe for more side-by-side tests like this one.
The lab, the scripts, and every command are linked in the description, so you can run every test yourself, on your own laptop. Thanks for watching Cloud Explainers.
Sources
- MySQL 8.4 Reference Manual, History of MySQL
- MySQL 8.4 Reference Manual, Statements That Cause an Implicit Commit
- MySQL 8.4 Reference Manual, Server Character Set and Collation
- MySQL 8.4 Reference Manual, Arithmetic Operators
- MySQL Community Edition (GPL)
- PostgreSQL License
- PostgreSQL docs, How Connections Are Established
- PostgreSQL docs, Mathematical Functions and Operators
- WordPress requirements
- Uber Engineering, Why Uber Engineering Switched from Postgres to MySQL (2016)
Demos: the official MySQL and PostgreSQL Docker images on a laptop. The shop data is made up; every command, output and number was real. Watch the full video on YouTube.