Skip to content

Deploy FastAPI with Postgres to your own server.

Ployz builds your FastAPI app from GitHub on a server you rent, runs it with Uvicorn behind HTTPS, adds Postgres in one click and runs alembic upgrade head before every deploy. Every pull request gets its own preview, your coding agent can run all of it, and deleting a database waits for you.

One FastAPI app with Postgres, five bills.

An always-on web app with Postgres costs about $14 a month on Railway when it's small, $62 at a medium size and $218 when it's busy. The same three apps run on one rented Hetzner server for $7, $11 and $19, and Ployz is free on it, custom domains included.

SmallMediumBusy
Railway (usage)$14$62$218
Your own server + Ployz$7 (CX23, 4 GB)$11 (CX33, 8 GB)$19 (CX43, 16 GB)

On the fixed-price platforms the floor is a web service plus a database: Heroku's always-on Basic dyno is $7 and a database adds $5 or more, Render's web service starts at $7 and Postgres at $6 plus storage, and Fly.io's Managed Postgres starts at $38 on its own. The apps and every source are in Railway pricing, Heroku pricing, Render pricing and Fly.io pricing. Server prices are Hetzner's in Europe; for the US, see Hetzner vs DigitalOcean.

How to deploy FastAPI with Postgres to your own server.

A FastAPI app in production is Uvicorn serving your app behind an HTTPS proxy, a Postgres database, and migrations that run once before the new code takes traffic. Ployz runs each of those on your server, built from your repository with no Dockerfile.

your fastapi project

$ curl -fsSL https://ployz.sh | sh

Installs the ployz CLI on macOS or Linux.

$ ployz login

Signs in to Ployz Cloud in your browser.

$ ployz up --server root@your-server

Installs Ployz on the server, builds this directory and gives it an HTTPS address.

Or paste Read ployz.dev/agents.md and deploy this repo to my server. into Claude Code, Codex or Cursor and it runs these steps for you. Then finish the app in the dashboard:

  1. Rent a server. Any provider works. Pick Ubuntu LTS with 2 GB of RAM or more, since builds run on the server too; 4 GB leaves room for Postgres.
  2. Connect GitHub so every push deploys: add the repository as a service (Deploy from GitHub).
  3. Check the start command. With main.py at the root and uvicorn in your dependencies, Railpack starts uvicorn main:app on the right port. For any other layout, set the start command to fastapi run app/main.py --port $PORT, with your own path.
  4. Add Postgres. Click Create, then Database, then PostgreSQL.
  5. Connect the app. Add a variable DATABASE_URL with the value ${{ postgres.DATABASE_URL }}. It points at Postgres over your servers' private network.
  6. Run migrations before each deploy. Set the pre-deploy command to alembic upgrade head.
  7. Set the health check to a route that returns 200, like a /health endpoint, so a new version must answer before the old one stops.
  8. Point your domain at the server, or keep the generated HTTPS address.

From then on, git push builds and deploys, and every pull request gets its own preview. Give previews their own Postgres and each one starts empty; a setup command can seed it.

What Railpack does with a FastAPI app.

Ployz builds with Railpack, so you don't need a Dockerfile:

  • It finds Python from requirements.txt, pyproject.toml or a Pipfile, and installs with pip, Poetry, PDM, uv or Pipenv, whichever your lockfile says.
  • It finds FastAPI when fastapi is a dependency. If main.py is at the root and uvicorn is listed too, it starts uvicorn main:app --host 0.0.0.0 --port ${PORT:-8000}. Ployz sets PORT to 8080, and your domain sends traffic there.
  • Otherwise, set the start command. fastapi[standard] brings Uvicorn without naming it in your dependency file, and many apps live in app/main.py. FastAPI's own docs start it with fastapi run app/main.py, which "uses Uvicorn underneath" and listens on 0.0.0.0. On a Railpack build the start command runs in a shell, so --port $PORT works.

Your own Dockerfile works too: set Build method to Dockerfile, and keep FastAPI's advice to use the exec form of CMD "to ensure that FastAPI can shutdown gracefully and lifespan events are triggered".

Workers: replicas, not --workers. FastAPI's deployment docs say that when something else handles replication, you "would want to have just a single Uvicorn process per container (but probably multiple containers)". On Ployz that's the replica count: run one Uvicorn process per replica and add replicas, up to 50 per service, spread across your servers. On one small server, --workers 2 is fine too, as long as the processes fit in memory.

Why migrations go in the pre-deploy command. FastAPI's docs say migrations belong in a separate step that runs once "before running the replicated worker containers", not in each one. The pre-deploy command is that step: it runs once with the new code before any replica is replaced, and if it fails, the deploy stops and the old version keeps serving.

Which Postgres driver? The DATABASE_URL Ployz gives you starts with postgresql://, which SQLAlchemy reads as psycopg2. For psycopg 3 or asyncpg, build the URL from the database's own variables: postgresql+asyncpg://${{ postgres.PGUSER }}:${{ postgres.PGPASSWORD }}@${{ postgres.PGHOST }}:${{ postgres.PGPORT }}/${{ postgres.PGDATABASE }}. Have Alembic's env.py read the same variable, so migrations and the app use one database.

Background jobs. FastAPI's BackgroundTasks run in the web process. For heavy or long jobs, add Redis in one click and a second service from the same repository whose start command runs your worker, like celery -A app.worker worker.

Ployz and the hosted platforms.

What you needHeroku, Render, Railway, Fly.ioPloyz on your server
BuildsBuildpacks, Railpack or your Dockerfile, depending on the platformNo Dockerfile needed for Python (pip, Poetry, PDM, uv and Pipenv), or bring your own Dockerfile
Web processA dyno, service or Machine, each with its own priceA service running Uvicorn, with as many replicas as you pick, sharing the server's RAM
PostgresManaged add-ons, from $5 to $38 for the smallestOne-click Postgres on your server, with a restore point
MigrationsRelease phase, pre-deploy or release commandA pre-deploy command, `alembic upgrade head`, runs once before each deploy
Previews per PRReview apps or PR environments, billed per service; Render needs Pro, Fly has none built inA preview environment for every pull request, with its own HTTPS address, on your servers. A Postgres it copies starts empty
Zero-downtime deploysYes, on most plansWith a health check set, the new version must pass it before the old one stops. Postgres has a short gap instead
PricePer process, per add-on, per GBYour server. Ployz is free on any number of servers, custom domains included

What comes with it.

  • Your coding agent runs it. Paste one line into Claude Code, Codex or Cursor and it installs Ployz, adds your server and ships the repo. Every deploy, restart or delete your agent asks for waits for you to approve, and no setting turns that off.
  • A restore point for Postgres. Restore it, or move the database to a bigger server with seconds of downtime (how, in the docs).
  • Monitoring and searchable logs for Uvicorn, your worker and every server, with an email when a service crashes.
  • Postgres from your laptop. ployz service port-forward opens the database locally for psql or a GUI, with no public port.

If you'd rather stay on a hosted platform, Railway alternatives, ranked by price compares the options.

Questions

How do I deploy a FastAPI app with Postgres to my own server?

Run your app with Uvicorn behind an HTTPS proxy, run Postgres next to it on a private network, and run alembic upgrade head once before each deploy. With Ployz, ployz up --server root@your-server installs everything on the server and builds FastAPI with no Dockerfile; you add Postgres in one click, set DATABASE_URL and add the pre-deploy command.

Where is the cheapest place to host FastAPI?

A server you rent: a 4 GB Hetzner CX23 runs a FastAPI app and Postgres for about $7 a month, and Ployz is free on it. The same small app with Postgres costs about $14 on Railway.

Should I use Uvicorn workers or replicas for FastAPI?

Replicas, when your platform runs several containers. FastAPI's docs recommend a single Uvicorn process per container when replication is handled at the cluster level. On Ployz you set the replica count, up to 50 per service across your servers.

How do I run Alembic migrations on deploy?

Set alembic upgrade head as the pre-deploy command. It runs once with the new code before any replica is replaced, and a failed migration stops the deploy while the old version keeps serving.

Do I need a Dockerfile to deploy FastAPI?

No. Railpack detects Python and FastAPI, installs your dependencies with pip, Poetry, PDM, uv or Pipenv, and starts Uvicorn. Set a start command like fastapi run app/main.py --port $PORT if your app isn't main.py at the root.

Can my coding agent deploy my FastAPI app?

Yes. Paste "Read ployz.dev/agents.md and deploy this repo to my server." into Claude Code, Codex or Cursor. It installs the CLI, registers ployz mcp and deploys. Every deploy, restart or delete your agent asks for waits for you to approve, and no setting turns that off.

What does Ployz cost?

Nothing, on any number of servers, with unlimited projects, preview environments, custom domains and members, and the dashboard is hosted for you. You pay your server provider for the servers.

Your servers. The whole platform. Free on your own servers.

Deploy your app

Facts checked 2026-11-15.