The Short Answer
Start from the application. If it requires MySQL, as many PHP content management systems do, use MySQL. If it requires PostgreSQL, use PostgreSQL. If it supports both, either works for ordinary web workloads, and the team's experience matters more than the engine. The sections below show where the two differ when that difference counts.
What They Have in Common
- Relational model: tables, columns of fixed types, joins, and SQL.
- Transactions: both are ACID-compliant; in MySQL this comes from the InnoDB engine.
- Concurrency: both use multiversion concurrency control, so readers do not block writers.
- Packages: both are in the repositories of common Linux distributions, although Debian's default MySQL-compatible server is MariaDB.
For the basics of each, see what is MySQL and how to install PostgreSQL.
Side by Side
| PostgreSQL | MySQL | |
|---|---|---|
| Default port | 5432 | 3306 |
| Client | psql | mysql |
| Dump tool | pg_dump | mysqldump |
| Accounts | Roles | User and host |
| Engines | One | Pluggable |
| Per connection | A process | A thread |
| Schemas | Inside a database | Same as database |
| Extensions | Many | Plugins |
Data Types and SQL
PostgreSQL offers a wide set of built-in types: arrays, ranges, network addresses, UUIDs, and user-defined types, plus features such as partial indexes and transactional schema changes, where a failed ALTER TABLE inside a transaction rolls back cleanly. MySQL has a smaller set of types, and a schema change such as ALTER TABLE commits on its own instead of joining the surrounding transaction. Both support window functions and common table expressions in current versions.
JSON
Both store JSON. PostgreSQL has json and the binary jsonb, which can be indexed as a whole with a GIN index and queried with containment operators. MySQL has a binary JSON type with path functions; the column itself is not indexed, so you index chosen values through generated columns or functional indexes, and array members through multi-valued indexes. For mostly structured data with some flexible fields, either one works.
Replication
PostgreSQL ships streaming replication, which copies the write-ahead log to standby servers, and logical replication, which publishes changes of chosen tables. MySQL replicates through its binary log, with GTIDs to track position, and adds group replication for multi-server setups. Both need care to set up well; the MySQL side is outlined in how to set up MySQL replication.
Extensions and Tools
PostgreSQL loads extensions into a database with CREATE EXTENSION. Widely used ones add geographic data, trigram text search, and query statistics. MySQL is extended through plugins and components, and different storage engines can be chosen per table. On the tooling side, both have mature command-line clients, graphical tools, and drivers for every common language.
Connections on a Small Server
PostgreSQL starts one process per connection, so hundreds of idle connections cost real memory; a connection pooler in front of it is common. MySQL handles each connection with a thread inside one server process, which uses less memory per connection. On a small VPS, keep the connection limit of either one in line with your memory; see how to check memory usage in Linux.
When Each One Fits
- MySQL: applications built for it, such as WordPress and many other PHP projects; teams that already run it.
- PostgreSQL: applications built for it; data with complex types, geographic queries, or heavy use of constraints; projects that want extensions.
- Either: new applications whose framework supports both.
Moving Between Them
A dump from one does not load into the other: quoting, auto-increment columns versus identity columns, type names, and case rules differ. The usual path is to create the schema in the target with the application's own migrations, then copy the data: pgloader moves data from MySQL into PostgreSQL (it loads into PostgreSQL only), and for the other direction the framework's own export and import tools are the usual route. Test the copy, compare row counts, and switch the application only after a trial run.