Add a database and run migrations
Declare a database with your service. The platform creates it on a managed PostgreSQL and connects your service to it. It applies your migrations before the new revision starts, so a revision never runs against a schema it was not built for.
What happens in the cloud
- You declare the database in
infra/requirements.json. The release set carries the declaration and your migration bundle. - At deploy, the platform creates the database on first use and gives the service access to it. Your code carries no database credentials.
- The platform runs the migrations of the bundle that it has not applied yet. The old revision keeps serving.
- The platform creates the new revision, waits until it is ready, then moves 100 % of traffic to it.
- If a migration fails, the deploy of that service stops. The old revision keeps serving.
Read How your service runs for the order of the whole deploy.
Do it
Declare the database
{
"$schema": "https://putnami.dev/schemas/putnami-infra.json",
"protocolVersion": 2,
"databases": [
{ "name": "orders", "engine": "postgres", "schemas": ["orders"] }
]
}Publish migrations
putnami build produces the migration bundle. A workload with a bundle declares a @putnami/cloud:publish-migration namespace in its putnami.json. A merge to main publishes the bundle in the release set. You run nothing else.
To publish a bundle yourself, name the namespace:
putnami cloud db migrations publish --namespace <namespace> --dry-runRemove --dry-run to publish.
Connect for a bounded time
Request a grant, then open a local bridge. The platform bounds the lifetime of the grant and records your reason.
putnami cloud db grant request --database <name> --level read --reason "investigate incident"
putnami cloud db connect --database <name>The grant request prints the connection string once. The levels are read, write and maintain. Add --ttl <seconds> to ask for a lifetime; the platform clamps it to its maximum. Connect your client to postgres://<user>:<password>@127.0.0.1:5432/<name> with the user and password of the grant. Use --port to change the local port.
| Want to… | Do this |
|---|---|
| List databases | putnami cloud db list |
| Show one database | putnami cloud db info <name> |
| Compare databases with the workloads that bind them | putnami cloud db status [<name>] |
| List your grants | putnami cloud db grant list --database <name> |
| End a grant | putnami cloud db grant revoke <id> --database <name> |
Use the console
Databases in putnami.app lists the databases of every workspace you belong to. Opening one gives you a read grant for one hour. The grant renews while the page stays open. You browse tables by schema, filter and sort rows, and read columns, types, defaults, keys and indexes.
Connect, on a database page, asks for a read grant (1 to 8 hours) or a write grant (15 to 60 minutes) and a reason. It shows the connection string once, with the tunnel command. Copy the password at that moment: the page never shows it again. Revoke now ends the grant and changes the password.
Each new grant changes your database user's password. A session that is already open keeps running. A new connection needs the connection string of the newest grant.
What you see
| Where | What |
|---|---|
putnami cloud deploy status <cm_id> |
One check per workload. A failed migration shows the service as failing, with a detail. |
putnami cloud db status <name> |
The databases, the workloads that bind them, and each binding that is not ready. |
| putnami.app, Databases | Tables, rows and schema of each database. |
What does not go back
Migrations apply forward only. The platform does not roll an applied migration back. Write a new migration to undo a schema change.
When it fails
| You see | Cause | Fix |
|---|---|---|
A service is failing after a deploy, and deploy status names a migration |
A migration failed. The platform retries a transient error 5 times. The old revision keeps serving. Other services continue. The deploy ends failed. |
Fix the migration. Merge again. |
env doctor reads failing on database-ownership |
Objects in the database belong to another owner than the platform expects. | Read Database ownership on the first deploy. |
db grant request asks for a reason |
The reason is mandatory, because grants are audited. | Pass --reason "<why>". |
db migrations publish asks for a namespace |
You gave no --namespace. |
Pass --namespace <namespace>. |
env doctor reads failing on publish-namespaces |
A workload with a migration bundle declares no @putnami/cloud:publish-migration namespace. |
Declare it in the putnami.json of the workload. |