Summary
NotesAdding a relational database such as PostgreSQL or SQLite3 to a FastAPI project can significantly enhance the project’s functionality and performance. A FastAPI project can store and retrieve data efficiently by integrating a database, allowing users to interact with the application more effectively. PostgreSQL is a robust and reliable database management system offering a wide range of features, while SQLite3 is a lightweight and portable solution ideal for small-scale projects.
Regardless of which database is chosen, integrating it into a FastAPI project can be a straightforward process that involves defining the database schema and connecting the application to the database. Once the database is up and running, the FastAPI project can interact with it using SQL queries or an Object-Relational Mapping (ORM) library such as SQLAlchemy, the basis for SQLModel, providing a powerful and flexible foundation for building modern web applications.
For the example code and documentation, I’ll be continuing with SQLite because it needs nothing extra to run. If you want to continue with PostgreSQL, that’s awesome: set CONF_DATABASE_URL in .env and the code, and the Alembic migrations, stay exactly the same.
The example code at this point is tagged ch02-databases in the example repository. To get it:
git clone https://github.com/codewalkr/fastapi-example-for-you.git
cd fastapi-example-for-you
git checkout ch02-databases