TinyDB is a Python implementation of a NoSQL, document-oriented database. Unlike a traditional relational database, which stores records across multiple linked tables, a document-oriented database stores its information as separate documents in a key-value structure. The keys are similar to the field headings, or attributes, in a relational database table, while the values are similar to the tableâs attribute values.
TinyDB uses the familiar Python dictionary for its document structure and stores its documents in a JSON file.
TinyDB is written in Python, making it easily extensible and customizable, with no external dependencies or server setup needed. Despite its small footprint, it still fully supports the familiar database CRUD features of creating, reading, updating, and deleting documents using an API thatâs logical to use.
The table below will help you decide whether TinyDB is a good fit for your use case:
| Use Case | TinyDB | Possible Alternatives |
|---|---|---|
| Local, small dataset, single-process use (scripts, CLIs, prototypes) | â | simpleJDB, Pythonâs json module, SQLite |
| Local use that requires SQL, constraints, joins, or stronger durability | â | SQLite, PostgreSQL |
| Multi-user, multi-process, distributed, or production-scale systems | â | PostgreSQL, MySQL, MongoDB |
Whether youâre looking to use a small NoSQL database in one of your projects or youâre just curious how a lightweight database like TinyDB works, this tutorial is for you. By the end, youâll have a clear sense of when TinyDB shines, and when itâs better to reach for something else.
Get Your Code: Click here to download the free sample code youâll use in this tutorial to explore TinyDB.
Take the Quiz: Test your knowledge with our interactive âTinyDB: A Lightweight JSON Database for Small Projectsâ quiz. Youâll receive a score upon completion to help you track your learning progress:
Interactive Quiz
TinyDB: A Lightweight JSON Database for Small ProjectsIf you're looking for a JSON document-oriented database that requires no configuration for your Python project, TinyDB could be what you need.
Get Ready to Explore TinyDB
TinyDB is a standalone library, meaning it doesnât rely on any other libraries to work. Youâll need to install it, though.
Youâll also use the pprint module to format dictionary documents for easier reading, and Pythonâs csv module to work with CSV files. You donât need to install either of these because theyâre included in Pythonâs standard library.
So to follow along, you only need to install the TinyDB library in your environment. First, create and activate a virtual environment, then install the library using pip:
(venv) $ python -m pip install tinydb
Alternatively, you could set up a small pyproject.toml file and manage your dependencies using uv.
When you add documents to your database, you often do so manually by creating Python dictionaries. In this tutorial, youâll do this, and also learn how to work with documents already stored in a JSON file. Youâll even learn how to add documents from data stored in a CSV file.
These files will be highlighted as needed and are available in this tutorialâs downloads. You might want to download them to your program folder before you start to keep them handy:
Get Your Code: Click here to download the free sample code youâll use in this tutorial to explore TinyDB.
Regardless of the files you use or the documents you create manually, they all rely on the same world population data. Each document will contain up to six fields, which become the dictionary keys used when the associated values are added to your database:
| Field | Description |
|---|---|
continent |
The continent the country belongs to |
location |
Country |
date |
Date population count made |
% of world |
Percentage of the worldâs population |
population |
Population |
source |
Source of population |
As mentioned earlier, the four primary database operations are Create, Read, Update, and Deleteâcollectively known as the CRUD operations. In the next section, youâll learn how you can perform each of them.
To begin with, youâll explore the C in CRUD. Itâs time to get creative.
Create Your Database and Documents
The first thing youâll do is create a new database and add some documents to it. To do this, you create a TinyDB() object that includes the name of a JSON file to store your data. Any documents you add to the database are then saved in that file.
Documents in TinyDB are stored in tables. Although itâs not necessary to create a table manually, doing so can help you organize your documents, especially when working with multiple tables.
To start, you create a script named create_db.py that initializes your first database and adds documents in several different ways. The first part of your script looks like this:



