# Hosted Postgres

> A Postgres database Osevel will run for you, with the same change journal, approvals and undo for every AI agent. A working prototype, not open yet.

Source: https://osevel.com/docs/hosted/overview/

Osevel can host a Postgres database for you, with the same journal, approvals and undo for
every agent as a database you connect. This page is the user guide.

> **Not open yet.** Hosting is a working prototype, not a service you can sign up for. The data
> plane runs only as a development setup on one machine (`spike/neon/`, Docker); there are no
> servers, no public regions (EU or US) and no payments yet. The panel's hosted screens are a
> preview on sample data. The plan for the beta: production use by invitation, no uptime
> guarantee, prices may change before general availability.

**What is ready and what arrives with the beta**

| Part                                                                                | State                                                                                                            |
| ----------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------- |
| Data plane: hosting, sleep/wake, branches, point-in-time restore, encrypted backups | prototype on one machine (`spike/neon/`); no servers, no R2, no customer restore action, retention not enforced  |
| Auth, instant API, Realtime, Vector, Cron, Queues                                   | prototype, tested end to end on the development data plane                                                       |
| Panel: Schema, Data and Modules tabs                                                | built (beta)                                                                                                     |
| `osevel database import`                                                            | built, in the CLI                                                                                                |
| Plans, spend cap, metering, invoices                                                | library built (`packages/billing`), not wired into the cloud; no payment provider yet                            |
| Access cards, freezes, staff grants, closing an organization                        | built (signed records, `packages/gate`); the gate is not deployed in front of a data plane yet                   |
| Panel form to create a hosted database, plan page, SQL console with an access card  | with the beta (the data-plane side of each is built)                                                             |
| `osevel db url` / `osevel db api` †                                                 | with the beta; today the development data plane's stand-in is `spike/neon/connector/connector.mjs url` and `api` |

- [Create a database](#create-a-database)
- [Connect your app](#connect-your-app)
- [Auth and the instant API](#auth-and-the-instant-api)
- [Realtime](#realtime)
- [Vector, Cron and Queues](#vector-cron-and-queues)
- [Tables, rows and SQL in the panel](#tables-rows-and-sql-in-the-panel)
- [Move in from Supabase, Neon or any Postgres](#move-in-from-supabase-neon-or-any-postgres)
- [Plans, the spend cap and invoices](#plans-the-spend-cap-and-invoices)
- [Safety: access cards, freezes, staff access](#safety-access-cards-freezes-staff-access)
- [Regions, deletion and your data](#regions-deletion-and-your-data)
- [Limits of the beta](#limits-of-the-beta)

## Create a database

Owners and admins create hosted databases from the panel. Creating one is a record signed with
your passkey: it names the region (EU or US), the plan (free or paid) and your bridge. The data
plane provisions it from that record in about ten seconds; the cloud only relays it.

- **Free**: two databases per organization, 500 MB each. At 80 % you are warned; at 500 MB the
  database turns read only (reads keep working; `DROP TABLE` frees space, `TRUNCATE` and
  `VACUUM FULL` are refused until then). It sleeps when idle and wakes on the next connection.
- **Paid** databases need the Pro plan (see [plans](#plans-the-spend-cap-and-invoices)).

The owner password is sealed to your bridge's key: only your bridge can open it, not our cloud.

## Connect your app

```bash
osevel db url --project app --database shop     # † a connection URL with its own app role
```

The app gets its own role, never the owner's and never an agent's. The URL uses TLS
(`sslmode=require`).

## Auth and the instant API

Every hosted database comes with **Supabase Auth** (email + password, email confirmation, magic
link, password reset) and **PostgREST** (every table in `public` as a REST API). Existing
Supabase client libraries work: give them your project's URL and anon key.

```bash
osevel db api --project app --database shop            # † OSEVEL_URL and OSEVEL_ANON_KEY
osevel db api --project app --database shop --service  # † adds the service key (server only)
```

```js
import { createClient } from '@supabase/supabase-js';
const db = createClient(process.env.OSEVEL_URL, process.env.OSEVEL_ANON_KEY);
await db.auth.signUp({ email, password });
const { data } = await db.from('todos').select('*');
```

- **New tables are closed.** Every table created in `public` has row-level security on from the
  start, so the API returns nothing until you add a policy, for example:

  ```sql
  create policy "own todos" on todos for all to authenticated
    using (user_id = auth.uid()) with check (user_id = auth.uid());
  ```

- The **anon key** is public (it goes in your app). The **service key** bypasses row-level
  security: keep it on a server.
- Requests without a valid key of your project never reach the database; tokens signed any other
  way than with your project's key (including `alg: none`) are refused; each client address is
  rate limited; queries time out after 3 s (anonymous) or 8 s (signed in).
- Passwords need at least 10 characters with a lower-case letter, an upper-case letter and a
  digit; nobody signs in before confirming their email; refresh tokens rotate.

## Realtime

Tables you add to the `supabase_realtime` publication (the panel's **Modules** tab: "Propose
streaming") send their inserts, updates and deletes over a WebSocket. Each subscriber gets only
the rows its row-level security allows:

```js
db.channel('todos')
  .on('postgres_changes', { event: '*', schema: 'public', table: 'todos' }, (p) => console.log(p))
  .subscribe();
```

## Vector, Cron and Queues

Enable them in the **Modules** tab; each change is a proposal you sign and a person applies at
the terminal through the approval gate, in the schema history.

- **Vector** (pgvector): vector columns and HNSW indexes (cosine, L2 or inner product).
- **Cron** (pg_cron): SQL on a schedule (`0 3 * * *`, or `30 seconds`). A job's SQL runs with the
  database owner's rights, so it is always approved by a person, and it may only read or write
  data: no schema changes, nothing in osevel's own schemas.
- **Queues** (pgmq): durable queues; your app sends and reads with `pgmq.send` / `pgmq.read`.

## Tables, rows and SQL in the panel

The **Schema**, **Data** and **Modules** tabs (beta) work for hosted and connected databases:
design tables, browse rows (every read is logged), propose row edits and SQL. Writes are never
run from the browser: they are proposals signed with your passkey and applied at the terminal
with `osevel schema apply --proposal <id>`, journaled and undoable like an agent's write.

On a hosted database you will also read with SQL directly, with a 15-minute **access card**
signed by your passkey (renewed with one touch, ended when the panel locks). The data plane
checks cards today; the panel's console for them opens with the beta.

## Move in from Supabase, Neon or any Postgres

```bash
export OSEVEL_IMPORT_FROM='postgresql://...source...'
export OSEVEL_IMPORT_TO='postgresql://...the hosted database (owner role)...'
osevel database import --dry-run           # the plan, nothing written
osevel database import --with-auth-users   # from Supabase: also your signed-up users
```

It runs on your machine: the URLs and the rows never pass through our cloud, and no URL is
printed. It shows a plan and stops before writing if a table already exists on the target, a
foreign key points into a schema it does not copy, or a role or extension is missing. It copies
in one transaction (`pg_dump` into `psql`; from Docker if the tools are not installed) and then
compares every table's row count. Supabase and Neon platform schemas are skipped; with
`--with-auth-users` your users keep their passwords.

## Plans, the spend cap and invoices

|                       | Free                | Pro                                                                      |
| --------------------- | ------------------- | ------------------------------------------------------------------------ |
| Price                 | $0                  | $25 / month per organization                                             |
| Databases             | 2 free, 500 MB each | paid databases                                                           |
| Included              |                     | 8 GiB-month storage, 300 compute hours                                   |
| Beyond the plan       | read only           | $0.13 / GiB-month, $0.10 / compute hour, **only with the spend cap off** |
| Point-in-time restore | 1 day               | 7 days                                                                   |

Only an owner changes the plan (a signed record). The **spend cap is on by default**: usage
beyond the plan is never billed; at the included storage your paid databases turn read only and
you are told. Turn it off to keep writing and pay for the overage. Invoices are monthly and
name the usage records they were computed from. (Beta prices; the payment provider opens with
the beta.)

## Safety: access cards, freezes, staff access

- **Freeze** (owners and admins): "agents" stops agent and panel access while your app keeps
  running; "everything" stops every connection. Lifting a freeze needs two owners.
- **Our staff** reach your database only with a grant your owner signs, time limited; in an
  emergency two of our staff must sign, and you are told at once and get a report.
- The proxy refuses superusers and the internal service logins (API, Auth, Realtime), even with a
  password.

## Regions, deletion and your data

- A database lives in the region chosen at creation; it moves only by importing it into a new
  database in another region.
- **Deleting a database** removes it and destroys its backup key at once: every backup becomes
  unreadable.
- **Closing your organization** needs two owners (or one owner on two devices): every hosted
  database and its backups are deleted, access stops, nothing more can be signed.
- Legal papers (drafts): [Data Processing Agreement](https://osevel.com/docs/hosted/dpa/),
  [subprocessors](https://osevel.com/docs/hosted/subprocessors/).

## Limits of the beta

- One region per database; no read replicas yet.
- Realtime streams database changes (`postgres_changes`); broadcast and presence are not offered.
- Extensions: vector, pg_cron and pgmq through the panel; common ones (pgcrypto, pg_trgm, citext,
  uuid-ossp, ...) are installed by the import; others on request.
- Storage (files) and edge functions are not part of Osevel: we do the database.
- The panel's plan page and the card payment flow open with the beta.
