Sub topic 2
Choosing between PostgreSQL and MySQL
This chapter assumes you are comfortable with the Linux command line, know basic SQL (CREATE TABLE, SELECT, INSERT, UPDATE, DELETE), and understand IP addresses, ports and systemd services. It helps you pick an engine before you install one. The rest of the tutorial covers both engines side by side, so either choice works. The point here is to make the choice on purpose and know what it commits you to.
Why the choice matters
Both PostgreSQL and MySQL are mature, open source relational database servers. Both run as a long-lived process on Linux, listen on a TCP port, and speak SQL. For a simple application, either will work.
The engine you choose is hard to change later. Your application code, SQL dialect, backup tooling, monitoring and the team's habits all grow around it. Moving a production database between engines means rewriting queries, converting data types and planning downtime. Spend an hour on the decision now.
The working model: what differs
The two engines differ in how they are built, and those differences show up in daily operations.
Process and connection model
PostgreSQL starts one operating system process per client connection. A main process (the postmaster) accepts connections and forks a backend process for each. Each connection costs real memory, so a few hundred connections is normal and thousands need a connection pooler.
MySQL uses one thread per connection inside a single server process (mysqld). Threads are cheaper than processes, so MySQL tolerates more simultaneous connections before you need a pooler. Chapter "Connection limits and connection pooling" covers both cases.
Storage engines
MySQL separates the SQL layer from the storage engine. The default engine, InnoDB, provides transactions, row-level locking and crash recovery. Older engines such as MyISAM exist but lack transactions, so avoid them for new work. The main memory setting is innodb_buffer_pool_size.
PostgreSQL has one built-in storage design. Updated rows are written as new row versions, and a background process called autovacuum removes the old versions. You need to watch that process on busy tables. The main memory settings are shared_buffers and work_mem.
Features and SQL behavior
PostgreSQL follows the SQL standard closely and has a wide feature set: rich data types (arrays, ranges, JSONB), partial and expression indexes, strong constraint support, and extensions such as PostGIS for geographic data. MySQL is simpler and has historically been tuned for fast, read-heavy web workloads. Its recent versions support JSON, window functions and common table expressions, so the gap is smaller than it once was.
Some behavior differences catch people out:
- MySQL's strictness depends on
sql_mode. A permissive mode can silently truncate data or accept invalid dates. Current defaults are stricter, but check them on a migrated server. - Identifier quoting differs. PostgreSQL uses double quotes, and MySQL uses backticks unless
ANSI_QUOTESis set. - PostgreSQL supports transactional DDL, so a failed schema change inside a transaction rolls back cleanly. In MySQL, most DDL statements commit implicitly.
- PostgreSQL has roles, and MySQL has user accounts tied to a host pattern such as
'appuser'@'10.0.0.%'.
Replication
Both engines can keep copies of the data on other servers. PostgreSQL's built-in streaming replication ships the write-ahead log to replicas, which makes a physical copy of the whole cluster. MySQL replication ships a binary log of changes, which gives more flexibility in what you replicate. Both are covered in a later chapter.
Comparison at a glance
| Aspect | PostgreSQL | MySQL (InnoDB) |
|---|---|---|
| Connection model | One process per connection | One thread per connection |
| Default port | 5432 | 3306 |
| Client access control | `pg_hba.conf` plus roles | Account with host pattern plus grants |
| Main memory setting | `shared_buffers` | `innodb_buffer_pool_size` |
| Logical backup tool | `pg_dump`, `pg_restore` | `mysqldump` |
| Transactional DDL | Yes | No, most DDL commits implicitly |
| Cleanup of old row versions | Autovacuum | InnoDB purge threads |
| Extension ecosystem | Large (PostGIS and others) | Smaller, plugin based |
Decision criteria
Use these questions in order. Stop when one gives a clear answer.
- Does the application or framework require one engine? Some products (many PHP CMS platforms, for example) are built for MySQL or MariaDB. Others require PostgreSQL features. If the vendor documents one engine, use it.
- Does the workload need advanced SQL or data types? Complex reporting queries, geospatial data, heavy JSONB use or strict constraints favor PostgreSQL.
- Is the workload simple, read-heavy and web-shaped? Many short queries on indexed lookups run well on MySQL, and operators often find it easy to run.
- What does your team already know? An administrator who has run MySQL for years will run it better than an unfamiliar PostgreSQL. Familiarity affects backups, tuning and incident response more than small feature differences.
- What tooling do you depend on? Check your backup, monitoring and managed-service plans for support of the engine.
Operational trade-offs
- Upgrades. PostgreSQL major versions change the on-disk format, so moving to a new major version needs a dump and restore or the
pg_upgradetool. Minor updates are drop-in. MySQL also has in-place upgrade paths between supported versions, and you should read the release notes first. - Maintenance. PostgreSQL needs attention to autovacuum and table bloat on write-heavy systems. MySQL needs attention to the buffer pool size, the redo log and the binary log retention.
- Security model. PostgreSQL controls who may connect in a dedicated file,
pg_hba.conf. MySQL combines the account name, host pattern and password in the user table. Both support TLS and strong password hashing. Later chapters configure each. - Licensing and ownership. PostgreSQL uses a permissive license and is governed by a community. MySQL is owned by Oracle, with a community edition under the GPL. If this matters to your organization, check it with your legal or procurement team.
Seeing the difference on a real host
You can check which engines your distribution offers before installing. On Debian or Ubuntu:
apt-cache policy postgresql mysql-server mariadb-serverEach entry shows the candidate version. Compare that to the version your application supports. Distribution packages are stable but may lag behind. Installation from vendor repositories, covered in the next chapter, gives newer versions with a different support arrangement.
The following diagram summarizes the choice.
Common mistakes
- Choosing by benchmark headlines. Results depend on schema, indexes and configuration. Test your own queries on representative data.
- Ignoring the team. A well-run server on the familiar engine beats a poorly run server on the "better" one.
- Assuming MariaDB and MySQL are interchangeable. Test your application against the exact one you will deploy.
- Running both without a reason. Two engines double your backup, patching and monitoring work.
- Skipping the version check. A distribution's default version may be too old for a feature you need.
Exercise
Your team must host the database for a new internal reporting application. It uses window functions, JSONB columns and a geospatial extension. Nobody on the team has strong experience with either engine. Which engine do you choose, and which two decision criteria support the choice?
Show answer
PostgreSQL. The workload needs advanced SQL features, rich data types and an extension ecosystem (JSONB and a geospatial extension such as PostGIS), which is criterion 2. Because the team has no strong preference, criterion 4 does not favor MySQL. You would still check that the application vendor does not require a specific engine (criterion 1) first.
Exercise
Why does PostgreSQL usually need a connection pooler at a lower connection count than MySQL?
Show answer
PostgreSQL starts a separate operating system process for each client connection, which uses more memory and has more overhead. MySQL uses a thread per connection inside one process, which is cheaper, so it tolerates more simultaneous connections before pooling becomes necessary.