Deploy FastAPI
Deploy a FastAPI application on Out Plane, with Uvicorn, a managed database, and migrations.
This guide deploys a FastAPI application with Uvicorn and connects it to a managed PostgreSQL database. For the shared deploy flow, see Deploy from GitHub.
Prepare Your App
Run with Uvicorn
Uvicorn defaults to 127.0.0.1, which is unreachable in a container, so bind 0.0.0.0
and the port you expose. Because the start command runs as a direct exec, wrap it in
sh -c so $PORT expands:
sh -c "uvicorn main:app --host 0.0.0.0 --port $PORT"Replace main:app with the path to your app object. Add uvicorn (or fastapi[standard])
to requirements.txt.
For more concurrency in production, run Uvicorn workers under Gunicorn:
sh -c "gunicorn main:app --workers 2 --worker-class uvicorn.workers.UvicornWorker --bind 0.0.0.0:$PORT"Set the worker count to the CPU cores of your instance size.
Deploy
Create an application from your repository. Buildpacks runs pip install -r requirements.txt, or use a Dockerfile. Set the start command above, and expose the port.
Connect a Database
Create a managed PostgreSQL database and copy its connection
string into a DATABASE_URL environment variable.
Connections require SSL, so ensure the string ends with ?sslmode=require.
import os
from sqlalchemy import create_engine
engine = create_engine(os.environ["DATABASE_URL"])Map environment variables to typed settings with pydantic-settings.
Run Migrations
Out Plane has no pre deploy hook. Run Alembic once from the
browser terminal with alembic upgrade head, or fold it into the
start command:
sh -c "alembic upgrade head && uvicorn main:app --host 0.0.0.0 --port $PORT"If you run more than one instance, prefer running migrations once manually so they do not race.
Production Notes
- Bind
0.0.0.0and use$PORT. Hardcoding8000means no traffic reaches the app. - Avoid blocking, CPU bound work inside
asyncroutes with a single worker. It stalls the event loop. Add workers or offload the work.