FastAPI SQLAlchemy PostgreSQL Template
Minimal starter template for a CRUD API with FastAPI + SQLAlchemy + PostgreSQL.
Features
- FastAPI app with CORS enabled
- SQLAlchemy ORM integration
- Postgres-ready configuration via
DATABASE_URL - CRUD endpoints for
posts - Pytest suite (DB mocked for local test runs)
- Docker + Docker Compose local setup
Requirements
- Python 3.10+
pip- PostgreSQL (only needed when running app without Docker)
Local Setup (Without Docker)
- Create and activate venv:
python3 -m venv .venv
source .venv/bin/activate- Install dependencies:
pip install -r requirements.txt
- Add env vars:
DATABASE_URL=postgresql://postgres:postgres@localhost:5432/fastapi_template
- Run API:
uvicorn main:app --reload
API Docs
When the server is running:
- Swagger UI: http://127.0.0.1:8000/docs
- ReDoc: http://127.0.0.1:8000/redoc
- OpenAPI JSON: http://127.0.0.1:8000/openapi.json
Tests
Formatting
Docker (Recommended for Local Run)
- Create
.env:
- Build and start:
docker compose up --build
- Access:
- API: http://127.0.0.1:8000
- Swagger: http://127.0.0.1:8000/docs
- ReDoc: http://127.0.0.1:8000/redoc
DATABASE_URL is passed to the API container from docker-compose.yml and can be overridden via .env.