Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Relational Databases  ›  Lesson

PostgreSQL, MySQL or SQLite?

SQLite 12 minServer databases such as PostgreSQL and MySQL serve many connections over the network, while SQLite is a library that keeps the whole database in one local file.

PostgreSQL, MySQL or SQLite?

Every relational database speaks SQL, so why are there so many? Because they are built for different situations. The question that matters most is not which is "best", but where the database runs and who connects to it.

The rule

Many app servers share a database server; one app can carry a file.

There are two shapes of relational database.

Client–server databases run as their own program, often on their own machine. Your application connects to them over the network, and many application servers can connect at the same time.

  • PostgreSQL: open source, strict about data, with rich types (JSON, arrays, ranges) and extensions. A common default for new projects.
  • MySQL: open source, very widely deployed, especially in web hosting. MariaDB began as a fork of MySQL and stays largely compatible with it.
  • Microsoft SQL Server and Oracle Database: commercial products, common in large companies that already use those vendors' tools.

Embedded databases run inside your program as a library. SQLite is the famous one: the whole database is a single file, there is no server to install, and no network connection. It is used in phones, browsers, desktop apps, tests and small sites. Many processes can read it at once, but only one can write at any moment, and it cannot be shared over the network by several servers the way a database server can.

import sqlite3

conn = sqlite3.connect("bookshop.db")   # a file on disk; created if missing
rows = conn.execute("SELECT title FROM books").fetchall()

Most SQL carries across engines unchanged, but the details differ, which is why dialects matter. An auto-numbering id, for example:

-- PostgreSQL
id INTEGER GENERATED ALWAYS AS IDENTITY PRIMARY KEY
-- MySQL / MariaDB
id INT AUTO_INCREMENT PRIMARY KEY
-- SQLite
id INTEGER PRIMARY KEY   -- automatically fills in new ids

The shortcut

Ask two questions, in order:

  1. Will more than one machine need to write to this data? Yes → a client–server database.
  2. If not, is it one app keeping its own data? Yes → SQLite is often enough, and much simpler.

Then prefer what your team already runs well. Operating a database you know beats a "better" one you do not.

Choosing for three bookshop jobsStep 1 of 3
  1. The public API

    Three app servers must write orders to the same data at once. Question 1 is yes: PostgreSQL.

Drills

Answer fast — trust the rule.

quick drill

How is SQLite different from PostgreSQL?

quick drill

Four app servers behind a load balancer must all write orders. Which fits best?

quick drill

How is MariaDB related to MySQL?

quick drill

A table definition using AUTO_INCREMENT fails in PostgreSQL. Why?

Remember

  • Many app servers share a database server; one app can carry a file.
  • Ask: will more than one machine write? Then prefer what your team operates well.
  • SQL is shared, but dialects differ; test on the engine you run in production.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainWhich database for which job?

Recommend a database for (a) the bookshop's production API running on three app servers and (b) a desktop app that keeps a reader's notes offline. Justify each choice and mention one thing that changes when you move SQL between engines.