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