Self-host FastAPI + PostgreSQL with Docker
A minimal Python HTTP service wired to a Postgres database, the reference pairing for a FastAPI backend.
This page describes the Levelrail template for FastAPI + PostgreSQL (Starter Kits). It deploys the services below as one Docker Compose app on your own server.
Project site: https://fastapi.tiangolo.com/deployment/docker/.Recommended memory: about 256 MiB.
Services, ports and volumes
| Service | Image | Container ports | Volumes |
|---|---|---|---|
api | python:3.12-alpine | 8000 | none |
db | postgres:16-alpine | none | fastapi_postgres_db_data -> /var/lib/postgresql/data |
Environment variables
| Service | Variable | Value |
|---|---|---|
api | DATABASE_URL | Generated at deploy |
db | POSTGRES_DB | Preset in the template |
db | POSTGRES_PASSWORD | Generated at deploy |
db | POSTGRES_USER | Preset in the template |
Passwords and keys marked as generated are created for you when the app is deployed and stored as secrets. Values are not shown here.
Deploy FastAPI + PostgreSQL with Levelrail
In the dashboard, open Apps, choose New app, then Browse templates, and select FastAPI + PostgreSQL. Review the Compose body and deploy.
With the CLI:
levelrail-cli templates deploy fastapi-postgres-starter --name my-fastapi-postgres-starterSee Service template catalog for how templates work, and Getting started if you have not installed Levelrail yet.
More Starter Kits templates
- Next.js + PostgreSQL
- Nginx Reverse Proxy (Multi-Backend)
- Node.js API + PostgreSQL
- Redis Cache Starter
- Static Site + API Backend
- Background Worker + Redis Queue
All templates are listed in the self-host gallery.