Deploy an ASP.NET Core API with PostgreSQL
Deploy a .NET 10 Docker image with managed PostgreSQL and run EF Core migrations before traffic moves.
On this page
Deploy your .NET 10 API with a Dockerfile and bind it to managed PostgreSQL. Run EF Core migrations as a release command before the new version serves traffic. Give that command a separate database login so the running API can read and write rows without owning the schema.
Download the ASP.NET Core starter. It contains a minimal Notes API, a Dockerfile, a checked-in EF Core migration, and a smoke test. You need the Gregale CLI, Python 3 for the smoke test, and a workspace with capacity for a disposable app and managed Postgres. You don't need .NET installed locally to deploy the Dockerfile.
Bind Postgres before the first deployment
Unpack the starter and run these from its root. Pick a unique app name and an available database region.
APP=your-unique-notes-demo
DB="$APP-db"
gregale deploy --name "$APP" --create-only --require-authn
gregale postgres create "$DB" --region us-east-2 --postgres-major 17 --class development --scale-to-zero
gregale postgres get "$DB"
When the database is ready, bind it with two credentials:
gregale postgres attach "$DB" "$APP" --env DATABASE_URL --access read_write
gregale postgres attach "$DB" "$APP" --env MIGRATION_DATABASE_URL --access migration
gregale deploy --name "$APP" --require-authn --rollout immediate --wait
Immediate routing here is for the disposable sample. Use your normal rollout policy for production. Keep authentication on; the Notes API in the starter has no user accounts.
Run EF Core migrations before the new version serves
The Dockerfile uses separate SDK and ASP.NET Core runtime images. Kestrel listens on Gregale's PORT; the sample defaults to 8080 locally. Gregale calls the same binary in two modes:
| Phase | Command | Database credential |
|---|---|---|
| Release | dotnet /app/Notes.dll --migrate | MIGRATION_DATABASE_URL |
| Serve | dotnet /app/Notes.dll | DATABASE_URL |
The release path invokes EF Core and exits. The serving path never calls MigrateAsync at startup. The sample converts the injected postgres:// or postgresql:// URL into an Npgsql connection string, preserving its TLS setting and keeping credentials out of the repository. See managed PostgreSQL for the binding types and separate migration roles for the database permission boundary.
Before Kestrel listens, the app validates DATABASE_URL and makes one bounded read of the Notes table using the serving role. That probe uses a separate, unpooled connection so it closes before the process starts serving. Missing credentials, missing tables, or missing read permission prevent the process from starting. Serial health probes do not repeatedly query the database, so this check does not keep a parked database awake.
When you add another migration, generate and review it locally before committing it. Keep changes backward-compatible with the release that is already serving. Moving schema changes to a release step does not make a destructive migration safe.
Write a note, then read it after redeploy
Use the HTTPS platform URL from the deploy receipt and export a request key from the disposable app's owning account as FAAS_TOKEN. The smoke test sends it only to the Gregale platform URL you supply.
python3 smoke.py "https://YOUR-APP.gregale.app" v1
This checks the app's /version response, verifies anonymous requests are blocked, and writes and reads a note. It saves the note ID and payload in marker.json. It refuses to post a second note if the receipt already exists. If a failed request leaves an incomplete receipt, use a fresh disposable setup instead of assuming the write did not happen.
Change the revision literal in the /version handler of Program.cs from v1 to v2, then rerun:
gregale deploy --name "$APP" --require-authn --rollout immediate --wait
python3 smoke.py "https://YOUR-APP.gregale.app" v2
The second check expects the new revision, reads the exact same note, and verifies the running API still has no migration credential. It checks the app's response, rather than relying on a platform health response. When finished, remove only the resources you created for this test:
gregale app delete "$APP" --delete-bindings
gregale postgres delete "$DB"
Verified on Gregale: The release command succeeded on managed PostgreSQL 17. The protected API wrote and read a note without receiving the migration credential. After the next deployment took all traffic, the new revision read back the same note. Both versions refused anonymous requests.
Spotted a problem on this page? Tell us