PyDB
An educational database engine built from scratch in Python.
PyDB is a fully functional relational database that stores data in tables, supports SQL queries, and manages transactions -- all written from the ground up as a learning project. Built incrementally using TDD, every concept is explained with real-world analogies a 12-year-old can follow.
Features
These are wired into the database engine -- run them through SQL and the executor uses them automatically:
- Full SQL support -- SELECT, INSERT, UPDATE, DELETE, CREATE/DROP TABLE
- JOINs -- cross-reference rows from two tables (aggregates supported)
- Aggregations -- COUNT, SUM, AVG, MIN, MAX with GROUP BY and HAVING
- Subqueries -- scalar and IN subqueries in WHERE clauses
- Views -- saved queries as virtual tables (in-memory; not persisted)
- B-Tree indexes -- CREATE/DROP INDEX; equality lookups use the index automatically; index definitions are saved and rebuilt on load
- Query planner -- EXPLAIN shows scan vs. index lookup decisions
- Constraints -- PRIMARY KEY, NOT NULL, UNIQUE (duplicates are found with one look-up in a register of values, not by reading every row)
- Dates and times --
DATEandTIMESTAMPcolumns (stored in UTC), withDATE '…'/TIMESTAMP '…'literals in SQL - NULL -- nullable columns,
IS NULL/IS NOT NULL, and three-valued logic (TRUE / FALSE / UNKNOWN) in WHERE, ON and HAVING - Transactions -- commit/rollback via an undo log (each change notes
how to undo itself, so no table is ever copied), also as
with Transaction(db): - Durable databases --
Database(path, journal=True)writes every commit to a checksummed journal before it counts, recovers after a crash, and checkpoints to keep the journal short - Locks -- threads take turns on one database lock; a
LOCKfile stops a second program opening the same journalled folder - Storage engine -- JSON persistence for table schemas and rows
- Interactive REPL -- type SQL and see pretty-printed results
Standalone teaching modules
These live in their own modules and work with a Database, but they
are not yet wired into the Database/executor pipeline -- import and
drive them directly to see how each idea works:
- Write-Ahead Log (
pydb.wal) -- crash recovery via operation logging (the real database uses its own journal: see "Durable databases" below) - MVCC (
pydb.mvcc) -- multi-version concurrency for snapshot isolation - CSV import/export (
pydb.csv_io) -- load and save spreadsheet files - Outbox pattern (
pydb.outbox) -- atomic DB writes + message queue delivery
Note on persistence:
save()/load()persist table schemas, rows and index definitions. Views live in memory only, so they are recreated rather than restored on restart. Withoutjournal=True, nothing is safe on disk until you callsave().
Note on updates:
Table.update()is copy-on-write: it returns a newRecord, and aRecordyou fetched earlier keeps its old values, so rows you are reading never change under you. Fetch the row again (get()orselect()) to see the new values. See docs/concepts/journal.md for the other ways every database now differs from before.
Example
pydb> CREATE TABLE cards (name TEXT NOT NULL, type TEXT, power INTEGER)
Table 'cards' created
pydb> INSERT INTO cards VALUES ('Pikachu', 'Electric', 55)
1 row inserted
pydb> SELECT type, COUNT(*), AVG(power) FROM cards GROUP BY type
┌──────────┬──────────┬────────────┐
│ type │ COUNT(*) │ AVG(power) │
├──────────┼──────────┼────────────┤
│ Electric │ 1 │ 55.0 │
└──────────┴──────────┴────────────┘
pydb> EXPLAIN SELECT * FROM cards WHERE name = 'Pikachu'
Full table scan on cards
Durable databases
Open a database with journal=True and every commit is written to
journal.log -- and forced onto the disk -- before it returns. Reopening the
folder (even after a crash) replays the journal, so committed work is
never lost and unfinished transactions leave no trace:
import tempfile
from datetime import UTC, date, datetime
from pathlib import Path
from pydb.database import Database
from pydb.schema import Column, Schema
from pydb.transaction import Transaction
from pydb.types import DataType
with tempfile.TemporaryDirectory() as tmp:
folder = Path(tmp) / "jobs"
schema = Schema(columns=[
Column("job", DataType.TEXT),
Column("run_date", DataType.DATE),
Column("started", DataType.TIMESTAMP),
])
with Database(folder, journal=True) as db: # every commit is fsynced
runs = db.create_table("runs", schema)
runs.insert({"job": "backup", "run_date": date(2026, 10, 17),
"started": datetime(2026, 10, 17, 2, 0, tzinfo=UTC)})
with Transaction(db) as txn: # all-or-nothing, one commit
txn.get_table("runs").insert({"job": "report", "run_date": date(2026, 10, 17),
"started": datetime(2026, 10, 17, 7, 30, tzinfo=UTC)})
with Database(folder, journal=True) as db: # reopen: recovered from disk
print([row["job"] for row in db.get_table("runs").select()])
Output:
['backup', 'report']
Options: sync=False skips the fsync (fast, but a power cut can lose the
last commits), checkpoint_bytes sets when the journal is checkpointed
automatically (4 MiB by default), and repair=True opens a journal damaged
in the middle by keeping the commits before the damage. save() and
close() take a checkpoint. See it survive a real crash with
uv run python examples/crash_and_recover.py, and read the
"Durable Databases" learning path in docs/index.md.
Quick Start
# Install dependencies
uv sync --all-extras
# Launch the interactive REPL
uv run pydb
# Run tests
uv run pytest
# Lint and type check
uv run ruff check .
uv run pyright src tests
Documentation
Full docs (with kid-friendly explanations of every idea): https://robcox2910.github.io/pydb/
Developing alongside a sibling
PyDB has no sibling dependencies of its own, but
PyStack pins PyDB to a release tag
(in its [tool.uv.sources]), and its uv.lock records the exact commit. A
change merged here can't affect PyStack until PyStack chooses to upgrade.
To try an unreleased PyDB change from a PyStack checkout next to this one, install it over the pinned release:
cd ../pystack
uv pip install -e ../pydb
uv run --no-sync pytest
Use uv run --no-sync while you do this: a plain uv run (like uv sync)
puts the pinned release back, which is how you undo the override.
Releasing
Other projects in the series (PyStack) use a pinned release of this one,
not whatever is on main. To publish a change for them:
- Merge the change to
main. - Tag it:
git tag -a vX.Y.Z -m vX.Y.Z && git push origin vX.Y.Z. A change to the public API bumps the minor version (v0.1.0→v0.2.0); anything else bumps the patch (v0.1.0→v0.1.1). - In each project that uses it, edit the tag in
[tool.uv.sources], runuv lock --upgrade-package <name>, and open a pull request so CI checks the pair together.
Related Projects
This project is one of a series that builds real systems "from scratch in Python" to teach how they work. See the series home for the full story.
| Project | What it teaches | Repository | Docs |
|---|---|---|---|
| PyOS | Operating systems | py-os | Docs |
| Pebble | Compilers and programming languages | pebble-lang | Docs |
| PyDB | Relational databases | pydb | Docs |
| PyKV | Key-value stores | pykv | Docs |
| PyDocDB | Document databases | pydoc-db | Docs |
| PyGraphDB | Graph databases | pygraphdb | Docs |
| PyTSDB | Time-series databases | pytsdb | Docs |
| PyVecDB | Vector databases | pyvecdb | Docs |
| PyColDB | Column-family databases | pycoldb | Docs |
| PyCrypt | Cryptography | pycrypt | Docs |
| PyNet | Computer networking | pynet | Docs |
| PyWeb | HTTP web servers | pyweb | Docs |
| PyGit | Version control | pygit | Docs |
| PySearch | Search engines | pysearch | Docs |
| PyMQ | Message queues | pymq | Docs |
| PyLLM | Language models (the brain) | pyllm | Docs |
| PyScheduler | Job scheduling | pyscheduler | Docs |
| PyStack | Full-stack integration | pystack | Docs |
License
MIT
Metadata
Release files for fromscratch-pydb 0.5.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| fromscratch_pydb-0.5.0.tar.gz | 298.1 kB | With provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| fromscratch_pydb-0.5.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 398.5 kB
Release files / fromscratch_pydb-0.5.0.tar.gz
| Download URL | fromscratch_pydb-0.5.0.tar.gz |
|---|---|
| Size | 298.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
0d6394312cea576e2b97288cfc3b7ce085eee353c1cd7e47e7f8b03cce0012bd
|
|
BLAKE2b-256 checksum How to use checksums |
1e3b4a4c6bdc3b435ea904efd64379c9f601c248e2bd65b7fcc04b5317d2188b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 26, 2026.
Transparency logRelease files / fromscratch_pydb-0.5.0-py3-none-any.whl
| Download URL | fromscratch_pydb-0.5.0-py3-none-any.whl |
|---|---|
| Size | 100.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
01a3b6529380bb620d0ad387393f3ac784fc9c85dc5dede7033624b027e10059
|
|
BLAKE2b-256 checksum How to use checksums |
00afb36b3335e5e82a5aa236a4712de42f7cc8b80257d8d0fc20cb08c687ecf0
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 26, 2026.
Transparency logSecurity for fromscratch-pydb 0.5.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
All 2 files in this release have been uploaded with provenance metadata.
Attestation for fromscratch_pydb-0.5.0.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 26, 2026.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.