Documentation Store data

Run Postgres migrations without giving your app schema permissions

Verify separate release and runtime roles with a real PostgreSQL migration and privilege check.

~3 min read
On this page

Use two database credentials: a migration role for the release command and a runtime role for the serving app. On Gregale, the release step gets MIGRATION_DATABASE_URL; running instances only use DATABASE_URL. The runtime role can read and write rows, but it does not own the tables or have schema-changing privileges.

WhereCredentialWhat the example checks
Release commandMIGRATION_DATABASE_URLCreate a table, add a column, and seed one row
App serving trafficDATABASE_URLInsert and read a new row
App serving trafficDATABASE_URLALTER TABLE fails with PostgreSQL SQLSTATE 42501

A migration role changes the schema. A runtime role handles requests. Don't put the migration URL in the serving app just because an ORM reports permission denied at startup. Move the migration to the release step.

Set up a disposable app and database

Download the role-separation starter and unpack it. Use a workspace with room for a test app and managed Postgres. Keep request authentication on. Never point this demo at a production database.

sh
APP=postgres-role-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"

Choose an available region and wait for the database to report ready. Attach it twice, in the same app scope, with different access:

sh
gregale postgres attach "$DB" "$APP" --env DATABASE_URL --access read_write
gregale postgres attach "$DB" "$APP" --env MIGRATION_DATABASE_URL --access migration
gregale env set --app "$APP" PG_ROLE_DEMO=1
gregale deploy --name "$APP" --require-authn --wait

The checked-in gregale.yaml runs python /app/migrate.py as the release command. It reads MIGRATION_DATABASE_URL and applies the migration before the new version serves. The server reads only DATABASE_URL. See Connect a Gregale app to Postgres.

Check the boundary in the running app

Use the platform URL and a Gregale key from the test app's owning account. Provide the key as GREGALE_TOKEN outside the repository. Run:

sh
python3 smoke.py "https://YOUR-APP.gregale.app"

The smoke test first confirms an anonymous request cannot read the app. With authentication it reads the release seed through the new column, writes and reads its own marker, and asks the fixture to run one fixed ALTER TABLE with the runtime credential. PostgreSQL should reject it with insufficient privilege (42501). The probe also confirms that the serving process cannot see MIGRATION_DATABASE_URL. If the SQL unexpectedly succeeds, the fixture rolls back the transaction and fails the test.

Verified on Gregale: The release command added the column on managed PostgreSQL 17. The running app wrote and read a separate row. PostgreSQL returned 42501 for the runtime ALTER TABLE, and the serving process had no migration credential.

The schema probe is deliberately enabled only in the disposable demo. Do not put an HTTP endpoint that changes schema in a production app.

Use the same pattern with your migration tool

Point your release command at MIGRATION_DATABASE_URL and keep runtime queries on DATABASE_URL. Use the migration tool's usual history table and keep schema changes compatible with the version that's still serving. Rolling back application code does not automatically reverse a committed migration. See Rollback code or a database migration before choosing a recovery path.

A runtime role without schema privileges can still alter rows it may write. Use custom PostgreSQL roles or database policies if you need narrower table or row access.

When finished, remove the disposable app and database through the console.

Spotted a problem on this page? Tell us