Ferdigheter · Databaser
Use the database
Create a project's Postgres database, connect an app to it through the pooler, run migrations as a release command, and reach it from a laptop.
A Skybu database is its own Postgres 17 cluster (CloudNativePG) with pgvector, inside a project. Apps in the same project get its address as environment variables; nothing outside the project can reach it over the network. The database inside the cluster is called app, and three roles with different rights connect to it.
In short
list_projectsto see whether the project already has a database. If not,create_database, namedmain.- Wait until its status is
ready(about a minute). The project's apps restart withDATABASE_URLset. - Have the app connect with
DATABASE_URL(through PgBouncer) and a small pool. - Run schema changes as the app's release command (
set_app_hostingwithreleaseCommand, such asnpx prisma migrate deploy): it runs as the owner role before each new version gets traffic, after a restore point.
Step 1: create it
{ "name": "create_database", "arguments": { "projectId": "<project id>", "name": "main" } }
The name is 2–31 characters: a lowercase letter, then lowercase letters, digits and hyphens. The answer has the id and "status": "provisioning". Follow it with list_projects, which lists each project's databases with their status and the project's wiredDatabaseId.
| Status | Meaning |
|---|---|
provisioning | Being created, or applying new passwords. |
ready | In use. |
restoring | Being restored to an earlier time. |
suspended | The organisation is suspended; the cluster is hibernated and its data kept. |
recovery_incomplete | A recovery stopped early. Apps get no DATABASE_URL. Tell the user to contact Skybu. |
An organisation may have 2 databases unless Skybu has given it more (db_quota_reached). Each has 1 GB of storage. For an import from Supabase, create it with the supabase argument instead (skill://playbooks/leave-supabase).
Step 2: what apps get
Only one database per project is wired to apps: the ready one named main, otherwise the oldest ready one.
| Variable | Role | Rights | Production | Previews |
|---|---|---|---|---|
DATABASE_URL | app_rw, through PgBouncer | Read and write rows; cannot create, change or drop tables | yes | yes (own database) |
DATABASE_URL_UNPOOLED | app_rw, straight to Postgres | As above, for what a pooler cannot do (below) | yes | yes |
DATABASE_URL_READONLY | app_ro | Read only | yes | no |
DATABASE_URL_DIRECT | app_owner | Owns the schema; for migrations | only in the release command | yes |
DATABASE_URL* names are reserved, so set_env_vars refuses them. Each pull request preview gets its own empty database, never production's (skill://apps/previews-and-approval).
TLS
Postgres accepts only TLS connections with SCRAM passwords. The URLs carry sslmode=require. The app also gets the database's CA certificate at /etc/skybu/postgres/ca.crt, with NODE_EXTRA_CA_CERTS and PGSSLROOTCERT pointing at it, so node-postgres and libpq verify the server without extra code. Pass the URL as is; do not turn verification off.
import pg from "pg";
export const pool = new pg.Pool({ connectionString: process.env.DATABASE_URL, max: 5 });
Limits per role
| Role | Connections | statement_timeout |
|---|---|---|
app_owner | 10 | 5 min |
app_rw | 30 | 30 s |
app_ro | 10 | 30 s |
A role's limit is shared by every app and pod in the project, and old and new versions overlap during a deploy, so keep pools small.
The pooler
Each database has a PgBouncer in front of it, in transaction mode, and DATABASE_URL goes through it: many short connections from apps, workers, jobs and previews share a few to Postgres. Prepared statements work (node-postgres, postgres.js, Prisma and Drizzle need nothing extra). What does not work through a pooler in transaction mode: LISTEN/NOTIFY, session advisory locks, SET without LOCAL, temporary tables kept between transactions, and WITH HOLD cursors. Use DATABASE_URL_UNPOOLED for those. For the first minute of a new database, until its pooler answers, DATABASE_URL goes straight to Postgres; the apps restart once it switches.
Step 3: schema changes
A production app's DATABASE_URL role cannot create tables. Run migrations as the app's release command: a command run once from each new version's image, after the build and before the version gets traffic.
{ "name": "set_app_hosting", "arguments": { "appId": "<app id>", "releaseCommand": "npx prisma migrate deploy" } }
- It gets the app's variables plus
DATABASE_URL_DIRECT(app_owner, straight to Postgres). Point the migration tool at that: Prisma'sdirectUrl = env("DATABASE_URL_DIRECT"), Drizzle'sdbCredentials.urlprocess.env.DATABASE_URL_DIRECT. The tool must be in the built image (a dependency, not only a dev dependency, when the image is pruned). - In production, Skybu marks a restore point named
release #<number>on the database first, so a bad migration can be undone (skill://databases/restore-a-database). - If it exits with anything but 0, runs out of memory or takes more than 10 minutes, the new version is not started and the live one keeps serving. The deployment fails with "The release command failed" and its log has the command's output (
get_deployment). - Previews run it too, against their own database. A rollback does not run it again.
- Write migrations that the version still running can live with for a moment: add columns before code uses them, drop them a version after code stops using them.
Other ways, when a release command does not fit:
From the user's machine. The CLI opens a tunnel through Skybu (a CLI key is needed; agents connected over OAuth cannot open it):
skybu db connect shop/main --role owner # tunnel on 127.0.0.1:6543 and a psql URL to useThen point the migration tool (
prisma migrate deploy,drizzle-kit migrate, …) at the printed URL. If you have a shell where the user'sskybuis logged in, you can do this yourself; otherwise give the user the commands.- In previews,
DATABASE_URL_DIRECTis set in the app too, so an app can migrate itself at start. Prefer the release command, which does the same in production.
Create tables as app_owner in the public schema. Default privileges then give app_rw read and write and app_ro read on them. In another schema, grant USAGE and the table rights to app_rw and app_ro yourself.
Extensions
vector (pgvector) is installed in public. The owner can create trusted extensions such as pgcrypto. Extensions that need a superuser are refused (file_fdw, for example); there is no superuser login.
Credentials and local work
{ "name": "get_database_credentials", "arguments": { "databaseId": "<database id>" } }
Returns the three roles and their URLs. It needs secrets:read, is audited, and the organisation's owners get an email when an agent reads it. The host is inside the cluster, so the URLs work only from the project's apps; from a laptop, use skybu db connect <db> [--role app|owner|readonly]. Never put the passwords in chat or code.
{ "name": "rotate_database_passwords", "arguments": { "databaseId": "<database id>" } }
New passwords for all three roles; the old ones stop working. The status is provisioning until they are applied, then the apps restart with the new URLs. Use it when a password may have leaked.
Things to know
- Do not copy the owner URL into an app variable of another name to migrate in production. The app would run with schema rights, and the next rotation breaks it. Use the release command.
- Agents cannot delete a database. The user does it in the dashboard (
https://platform.skybu.no) or withskybu db delete; its backups are kept for 30 days. - To look at the data without seeing who it is about, use
query_database(skill://data/query-data-safely).