Ferdigheter · Databaser
Use the database
Create a project's Postgres database, connect an app to it, run schema changes with the right role, 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_URLand a small pool. - Make schema changes as the owner role, which production apps do not get: the user runs them through
skybu db connect … --role owner. Make a restore point first (skill://databases/restore-a-database).
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 | Read and write rows; cannot create, change or drop tables | yes | yes (own database) |
DATABASE_URL_READONLY | app_ro | Read only | yes | no |
DATABASE_URL_DIRECT | app_owner | Owns the schema; for migrations | no | 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. There is no connection pooler (PgBouncer).
Step 3: schema changes
Skybu has no release step that runs migrations for you, and a production app's DATABASE_URL role cannot create tables. Run migrations as app_owner:
From the user's machine. The CLI opens a tunnel through Skybu (a CLI token 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, so the app can migrate itself at start. Write that code so it runs only whenDATABASE_URL_DIRECTis present.
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.
- 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).