Small FastAPI example that stores a Person (id, first_name, last_name, email) into Postgres using async SQLAlchemy.
This is a project I used to learn more about writing a service using FastAPI. I'm using it to make sure I understand how to use a database, run CI/CD, and use the builtin tools for exercising and documenting the JSON API. I used an AI agent to assist.
Assumes you have a development environment on your system with the following installed:
- postgresql database (I used 14.19 installed with Homebrew)
- python3 (I used 3.9 installed with Homebrew)
- Create and activate a virtual environment (recommended):
python3 -m venv .venv
source .venv/bin/activate- Install dependencies:
pip install -r requirements.txt- Copy and set your database URL in
.env(see.env.example):
cp .env.example .env
# edit .env to set your DATABASE_URLuvicorn app.main:app --reloadOpen http://127.0.0.1:8000/ to see the form and insert a Person into the person table.
Notes
- The app uses an async SQLAlchemy engine and will create the
persontable at startup if it doesn't exist. - The default DATABASE_URL in
.env.examplepoints to a local Postgres with userpostgresand databasepostgres— change it for your environment.