Starting a new project, you have to pick a database, and four names come up again and again: PostgreSQL, MySQL, SQLite and MongoDB. All four are mature, widely used and free to start with. They are not interchangeable, though. They differ in data model, in how they run, and in what they are good at. Here is a side-by-side view.
The four in one paragraph each
PostgreSQL is a relational database server known for standards compliance and extensibility. It has a rich type system (arrays, JSONB, ranges, custom types), many index types, strong transactional guarantees, and an extension ecosystem: PostGIS for geospatial data, pgvector for embeddings, and more. It is a common default for new backend projects.
MySQL is a relational database server and one of the most deployed databases on the web. With the InnoDB storage engine it supports ACID transactions and row-level locking. It is simple to operate, has a large ecosystem of hosting, tooling and replication setups, and is the classic choice for web stacks like LAMP and many CMSs. MariaDB is a community fork with a very similar interface.
SQLite is not a server at all. It is a library that your program links, and the whole database is a single file on disk. There is no process to run, no port, no users to configure. It supports most of SQL and full transactions. It is everywhere: phones, browsers, desktop apps, embedded devices, tests and small websites.
MongoDB is a document database. Instead of tables and rows, it stores documents (JSON-like, stored as BSON) in collections. Documents in one collection do not need the same fields. It supports secondary indexes, an aggregation pipeline, replica sets and built-in sharding, and multi-document transactions in modern versions.
Side by side
| PostgreSQL | MySQL | SQLite | MongoDB | |
|---|---|---|---|---|
| Data model | Relational + JSONB | Relational | Relational | Documents (BSON) |
| Runs as | Server | Server | Library, one file | Server / cluster |
| Query language | SQL | SQL | SQL | MQL + aggregation |
| Schema | Strict, with JSONB escape | Strict | Flexible typing | Flexible per document |
| Concurrent writes | Many (MVCC) | Many (InnoDB) | One writer at a time | Many |
| Horizontal scale | Replicas; sharding via extensions | Replicas; sharding via tools | Not its goal | Built-in sharding |
| Sweet spot | General backend, complex queries | Web apps, read-heavy sites | Embedded, local, small sites | Flexible documents, scale-out |
A few notes on the table:
-
JSON in PostgreSQL.
JSONBcolumns can be indexed (with GIN) and queried, so one table can mix strict columns with a flexible document part. This covers many cases that once pushed teams toward a document store. - Writes in SQLite. SQLite allows many readers but only one writer at a time. With WAL mode, readers and the writer do not block each other, which is enough for many small and medium apps. It is not built for many machines writing over a network.
-
Joins. The three SQL databases are built around joins. MongoDB has
$lookup, but its design favours storing data that is read together in one document. - Transactions. All four support transactions. In MongoDB, single-document writes are always atomic, and multi-document transactions exist but are generally used sparingly.
Strengths and limits
PostgreSQL
- Strengths: advanced SQL (window functions, CTEs, rich indexes), strong consistency, extensions, careful handling of data correctness.
- Limits: each connection is a separate process, so many connections need a pooler such as PgBouncer. Tuning
VACUUMand memory settings matters as data grows.
MySQL
- Strengths: simple to run, well known by hosts and admins, mature replication, good performance for common web queries.
- Limits: historically fewer advanced SQL features than PostgreSQL (recent versions closed much of the gap), and some behaviours depend on settings such as SQL mode, so check them.
SQLite
- Strengths: zero setup, one file to copy or back up, very fast for local reads, ideal for tests and embedded use.
- Limits: a single writer at a time, no user management, and no network access by itself. Not meant to be shared by many application servers.
MongoDB
- Strengths: flexible documents that map directly to objects in code, horizontal scaling with sharding, convenient for data whose shape varies.
- Limits: the data model needs planning around access patterns; heavy relational queries with many joins are awkward; a flexible schema still needs validation, or bad data creeps in.
Common mistakes
- Picking MongoDB to "skip the schema". The schema still exists, it just moves into your code. Use schema validation if you go this way.
- Picking a server database for a single-user desktop or mobile app. SQLite is the better fit there.
- Using SQLite behind several web servers on a network file system. File locking over network shares is a known source of trouble.
- Choosing for a scale you do not have. A single well-indexed PostgreSQL or MySQL server handles a lot. Plan for sharding when you have numbers that call for it.
When to pick which
- Pick PostgreSQL as the default for a new backend: relational data, complex queries, reporting, mixed structured and JSON data, or when you want extensions like PostGIS or pgvector.
- Pick MySQL when your stack, host or team already uses it, for classic web apps and CMSs, or when you want simple, well-understood replication for read-heavy traffic.
- Pick SQLite for mobile and desktop apps, CLI tools, embedded devices, tests, prototypes and small websites with modest write traffic.
- Pick MongoDB when your data is naturally document-shaped and read as a whole (catalogs with varying attributes, content, event payloads), and when you expect to scale writes across many machines.
If you are unsure, start with PostgreSQL or SQLite. Both are easy to move away from later, and both handle more than most projects need at the start.
Watch the animated version: https://youtu.be/f1IZvNkKjs4
Originally published at abdeldjalilchougui.biz.
Top comments (0)