FastAPI and PostgreSQL template

Source: setcloud1/template-fastapi-postgres. The SetCloud-authored repository is MIT licensed and carries dependency license attribution in its NOTICE file.

Runtime contract

  • Python 3.13 production container
  • FastAPI served by Uvicorn
  • GET /health returns the deployment health response without requiring a database
  • Optional pooled PostgreSQL through the injected DATABASE_URL

Run and verify locally

python -m venv .venv
. .venv/bin/activate
pip install -r requirements-dev.txt
pytest
uvicorn app.main:app --reload --port 3000

Build the production image with:

docker build -t my-fastapi-app .
docker run --rm -p 3000:3000 my-fastapi-app

Attach PostgreSQL

Clone FastAPI + PostgreSQL from Projects → New Project → Templates and grant the SetCloud GitHub App access to the generated repository. You can deploy the API without a database. To add one, open Databases, select the project, and create Managed PostgreSQL. That action starts a deployment which injects DATABASE_URL into the app.

GET /database-time demonstrates a pooled connection and returns 503 until a database is attached. The code converts SetCloud’s PostgreSQL URL to SQLAlchemy’s Psycopg driver form without exposing or rewriting credentials.