SkybuDocs

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.

skill://databases/use-the-databaseAgenter leser den med resources/read eller skills_read. Den er skrevet på engelsk, for agentene.

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

  1. list_projects to see whether the project already has a database. If not, create_database, named main.
  2. Wait until its status is ready (about a minute). The project's apps restart with DATABASE_URL set.
  3. Have the app connect with DATABASE_URL and a small pool.
  4. 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.

StatusMeaning
provisioningBeing created, or applying new passwords.
readyIn use.
restoringBeing restored to an earlier time.
suspendedThe organisation is suspended; the cluster is hibernated and its data kept.
recovery_incompleteA 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.

VariableRoleRightsProductionPreviews
DATABASE_URLapp_rwRead and write rows; cannot create, change or drop tablesyesyes (own database)
DATABASE_URL_READONLYapp_roRead onlyyesno
DATABASE_URL_DIRECTapp_ownerOwns the schema; for migrationsnoyes

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

RoleConnectionsstatement_timeout
app_owner105 min
app_rw3030 s
app_ro1030 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 use

    Then point the migration tool (prisma migrate deploy, drizzle-kit migrate, …) at the printed URL. If you have a shell where the user's skybu is logged in, you can do this yourself; otherwise give the user the commands.

  • In previews, DATABASE_URL_DIRECT is set, so the app can migrate itself at start. Write that code so it runs only when DATABASE_URL_DIRECT is 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 with skybu 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).