Back to Home
Tutorials · Databases

Database Instructions

Three databases, three very different ideas of what a database is. SQLite is a single file your program opens directly. PostgreSQL is a server that many programs talk to at once, in SQL. MongoDB is a server too, but it stores JSON-like documents instead of rows. Each guide below takes you from nothing installed to your own data stored and queried, on Linux, macOS, or Windows, and every command block has a copy button.

The finish line — the three prompts you'll be typing at
SQLitesqlite> SELECT 1 + 1; 2
PostgreSQLyou=> SELECT 1 + 1; ?column? ---------- 2
MongoDBtest> 1 + 1 2
Guides 3
THE GUIDES

Pick a database, or do all three in order.

WHICH ONE

Three tools, three different jobs.

SQLitePostgreSQLMongoDB
Where the data livesOne file on diskA server process that owns a data directoryA server process that owns a data directory
Shape of the dataTables of rowsTables of rows, plus JSON columnsCollections of JSON-like documents
Query languageSQLSQLJavaScript-style method calls
Shellsqlite3psqlmongosh
Default portnone, no network543227017
Good first useA desktop app, a small website, a script's scratch dataA web app with many users writing at onceRecords whose fields vary from one to the next

How much each one can hold

All three can store far more than most projects will ever need. The limits that matter in practice are the size of a single record, the size of a single stored file, and how many programs can write at the same moment. These figures are from each project's official limits page.

SQLitePostgreSQLMongoDB
Files on diskOne file per database; a connection can attach up to 10 more (125 at most)A data directory; each table is split into 1 GB segment filesA data directory; one file per collection and one per index
Largest databaseAbout 281 TBUnlimitedNo hard limit; bounded by the filesystem, then by sharding across servers
Largest table or collectionBounded by the database size32 TB per tableNo hard limit
Largest single row or documentAbout 1 GB (the default maximum string or BLOB length)1 GB per field; large values are moved out of the row automatically16 MB per document
Storing files (images, PDFs)As a BLOB, up to about 1 GB eachAs bytea, up to 1 GB eachUp to 16 MB inside a document; bigger files go in GridFS, which splits them into chunks
Columns or fields2,000 columns per table (default)1,600 columns per tableNo column limit; documents can nest 100 levels deep
Writers at the same momentOne at a time; readers carry on meanwhileMany, with row-level lockingMany, with document-level locking
ConnectionsNo server; each program opens the file itself100 by default (max_connections), raised with a setting or a connection poolerThousands; limited mainly by the operating system
Size is rarely the reason to switch

A single SQLite file handles many gigabytes comfortably. What pushes a project to PostgreSQL or MongoDB is usually concurrency, when many programs or users need to write at the same moment, or the need to reach the database over a network from several machines.

Pros and cons

SQLite
  • Nothing to install or run: the database is one file you can copy, email, or back up.
  • Very fast for one program reading and writing on the same machine.
  • Built into Python, PHP, Android, iOS, and every major browser.
  • Ideal for learning SQL, prototypes, desktop and mobile apps, and small websites.
  • Only one writer at a time, so busy multi-user apps queue up.
  • No network access or user accounts; programs must be on the same machine as the file.
  • Flexible typing accepts wrong-typed values unless the table is declared STRICT.
  • Fewer built-in features for large-scale reporting and replication.
PostgreSQL
  • Many users can read and write at once, safely.
  • Strict types and constraints keep bad data out.
  • Rich features: transactions, JSONB, full-text search, window functions, extensions such as PostGIS.
  • Free and open source, with a very large community and hosting on every cloud.
  • A server to install, configure, secure, and back up.
  • Roles and login rules take some learning, as the guide's first steps show.
  • Schema changes need planning on very large tables.
  • Scaling writes beyond one server needs extra tools or services.
MongoDB
  • Flexible documents: records can differ, and fields can be added without a migration.
  • Arrays and nested objects map directly onto the objects in your code.
  • Built-in replication, and sharding to spread data across many servers.
  • A powerful aggregation pipeline for reports.
  • No schema by default, so typos and inconsistent fields slip in unless you add validation.
  • Joins with $lookup are clumsier than SQL joins.
  • Transactions need a replica set, and each document is capped at 16 MB.
  • The Community edition's license (SSPL) is not considered open source by the OSI, so some Linux distributions don't package it.

A file, a server that speaks SQL, and a server that stores documents. Choosing between them is choosing a shape for your data.