Back to Blog
Tutorial6 min read2026-05-01

Deploy FastAPI to PandaStack with Docker and PostgreSQL

FastAPI is blazing fast. Pair it with PandaStack's managed PostgreSQL and container hosting for a production-ready API in minutes.

# Deploy FastAPI to PandaStack with Docker and PostgreSQL

FastAPI is the fastest-growing Python API framework — high performance, automatic OpenAPI docs, and excellent type safety via Pydantic. This tutorial shows you how to containerize a FastAPI app and deploy it to PandaStack with a managed PostgreSQL database.

Prerequisites

  • A FastAPI project pushed to a GitHub repository
  • A free PandaStack account at [dashboard.pandastack.io](https://dashboard.pandastack.io)

Step 1: Prepare Your FastAPI App

A minimal FastAPI app with database support (main.py):

from fastapi import FastAPI
from contextlib import asynccontextmanager
import databases
import os

DATABASE_URL = os.environ["DATABASE_URL"]
database = databases.Database(DATABASE_URL)

@asynccontextmanager
async def lifespan(app: FastAPI):
    await database.connect()
    yield
    await database.disconnect()

app = FastAPI(lifespan=lifespan)

@app.get("/health")
async def health():
    return {"status": "ok"}

@app.get("/")
async def root():
    return {"message": "Hello from FastAPI on PandaStack"}

Step 2: Create requirements.txt

fastapi>=0.110.0
uvicorn[standard]>=0.29.0
databases[asyncpg]>=0.9.0
sqlalchemy>=2.0.0
alembic>=1.13.0
pydantic-settings>=2.0.0

Step 3: Write the Dockerfile

FROM python:3.12-slim

WORKDIR /app

COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt

COPY . .

EXPOSE 8000

CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]

For production, add --workers 2 and consider gunicorn with uvicorn workers:

CMD ["gunicorn", "main:app", "-k", "uvicorn.workers.UvicornWorker", "--bind", "0.0.0.0:8000", "--workers", "2"]

Step 4: Create a Managed PostgreSQL Database on PandaStack

  1. 1Log in to [dashboard.pandastack.io](https://dashboard.pandastack.io)
  2. 2Navigate to DatabasesNew DatabasePostgreSQL
  3. 3Copy the connection string

Connection string format:

postgresql+asyncpg://user:password@host:5432/dbname

Note the +asyncpg driver suffix for async FastAPI applications.

Step 5: Deploy the Container

  1. 1Go to New DeploymentContainer App
  2. 2Connect your GitHub repository
  3. 3Set environment variables:
VariableValue
DATABASE_URLYour PostgreSQL connection string
SECRET_KEYA strong random secret
ENVIRONMENTproduction
  1. 1Click Deploy

Step 6: Run Alembic Migrations

If using Alembic for database migrations:

# Using PandaStack CLI
panda exec --command "alembic upgrade head"

Or add migrations to your startup script:

CMD ["sh", "-c", "alembic upgrade head && uvicorn main:app --host 0.0.0.0 --port 8000"]

Step 7: Verify the Deployment

Once deployed, your FastAPI app will be available at https://your-app.pandastack.io.

FastAPI automatically generates interactive API documentation:

  • Swagger UI: https://your-app.pandastack.io/docs
  • ReDoc: https://your-app.pandastack.io/redoc

Test your health endpoint:

curl https://your-app.pandastack.io/health
# {"status": "ok"}

Setting Up Automatic Deploys

Every push to your configured GitHub branch triggers an automatic rebuild. Configure branch settings in your deployment dashboard. For production, use a dedicated production branch and merge to it when ready to release.

Performance Considerations

  • Use uvicorn with --workers matching available CPU cores
  • Enable connection pooling for PostgreSQL (asyncpg handles this automatically)
  • Cache heavy queries with Redis (PandaStack managed Redis available from the Databases section)
  • Add a /health endpoint for PandaStack's health checks

Full documentation: [docs.pandastack.io](https://docs.pandastack.io).

Ready to deploy?

Start free on PandaStack — no credit card required.

Start free on PandaStack

More in Tutorial

Browse all Tutorial articles →

See also