# PocketBase — database, auth & files

PocketBase is a managed SQLite-backed backend with a REST API, user auth, file storage, and realtime subscriptions. Each app gets its own sidecar.

Use it when people need shared records, accounts, uploads, or data that follows them across devices. A static site or browser-only timer may not need a database. Keep an existing Supabase or Convex backend when your app depends on it; see [external databases](https://percher.app/docs/external-databases). Enabling PocketBase does not convert localStorage, replace another database, or create your app's collections automatically.

## Set up PocketBase
Three ways to start:
1. Ask your connected assistant: "Add Percher PocketBase to this app. Create the collections and per-user access rules it needs, connect the app through the PocketBase SDK, publish, and verify that one user cannot read another user's private data."
2. Start a new app with the [Capsule template](https://percher.app/docs/capsule): `bunx percher create my-app --template capsule`, then `cd my-app` and `bunx percher publish`. It includes sign-in, a client, and a schema applied during publish.
3. Add it to an existing app: merge this section into percher.toml, keep the existing app name and runtime, then publish from that project's directory:
```toml
[data]
mode = "pocketbase"
```
```sh
bunx percher publish
bunx percher data
```
The Data tab now shows the database and its admin link. Sign in with your PocketBase credentials. If you don't have them, use the Data tab's reset-password control or run `bunx percher data reset-superuser --reveal` from the project directory. Save the returned email and password for future sign-ins; the password is shown once. Redeploy if running server code uses the rotated superuser password.

## The PocketBase host is public
`pb-<app>.percher.run` serves PocketBase's whole API and its admin UI to the internet, also for private and password-protected apps: your app's gate does not cover it. Your collection rules are the only access control.
- The built-in `users` collection lets anyone sign up: its Create rule is empty. If your app does not need self-registration, lock that rule.
- `@request.auth.id != ""` means any signed-in account, including one a stranger registered a minute ago. It does not mean "my server". For data only your server reads, lock the rule; the server signs in as superuser with `POCKETBASE_ADMIN_EMAIL` / `POCKETBASE_ADMIN_PASSWORD` over the internal `POCKETBASE_URL`. These admin credentials are omitted when `[data] superuser_env = false`; keep them enabled for this server pattern.
- Percher limits login, one-time-password and account-email requests per IP on this host, and answers HTTP 429 over the limit; show that to the user rather than retrying. `[web].rate_limit` applies only to your app's own host.

## Create collections
In the PocketBase admin UI, create a Base collection named tasks. Add title (Text), done (Bool), and user (required single Relation to users). Set API rules before connecting browser code: locked rules are superuser-only; empty rules allow anyone. For private tasks, use these rules:
- List / View / Delete: `@request.auth.id != "" && user = @request.auth.id`
- Create: `@request.auth.id != "" && @request.body.user = @request.auth.id`
- Update: `@request.auth.id != "" && user = @request.auth.id && @request.body.user:changed = false`
Avoid hard-coded email addresses or personal IDs in rule text. Use an owner relation such as `user = @request.auth.id`. Use a signed-in user's ID when creating a task. Auth users belong in an Auth collection; a Base collection stores ordinary records. A File field stores uploads. Alternatively keep your schema in the project's `pb/schema.json`, as the [Capsule guide](https://percher.app/docs/capsule) describes, so supported additive changes apply during publish. Changing only data.mode does not create these fields or rules. [PocketBase rule reference](https://pocketbase.io/docs/api-rules-and-filters/).

Install the SDK in your project with `npm install pocketbase` (or your project's package manager).

Three env vars are injected automatically: `POCKETBASE_URL` (internal Docker URL for server-side calls), `POCKETBASE_PUBLIC_URL` (public URL with SSL), and `VITE_POCKETBASE_URL` (public URL, Vite convention). Connect with the official SDK:
```js
import PocketBase from "pocketbase";
const pb = new PocketBase(process.env.POCKETBASE_URL);            // server-side
```
Browser alternative: `const pb = new PocketBase(import.meta.env.VITE_POCKETBASE_URL)`. VITE_* is supplied at build time for supported builds; a custom Dockerfile needs an ARG. Next.js browser code needs your own NEXT_PUBLIC_POCKETBASE_URL set to the public PocketBase URL before publishing. Never expose POCKETBASE_ADMIN_PASSWORD in browser code. For server-side user sessions, create a separate SDK client for each request rather than sharing mutable auth state.
The SDK covers CRUD (`pb.collection("tasks").create/getList/update/delete`), auth (`authWithPassword`, `pb.authStore`), file uploads via FormData + `pb.files.getURL`, and realtime (`pb.collection("messages").subscribe`).

File fields in `pb/schema.json` use flat settings, for example `{"name":"attachment","type":"file","protected":true,"maxSelect":1,"maxSize":1048576,"mimeTypes":["application/pdf"],"thumbs":[]}`. Do not nest settings under `options`; omit optional arrays or use `[]`, not `null`. Protected files require a PocketBase file token and suitable collection rules. Changing an existing field's protection is not an additive schema change; a corrected descriptor does not automatically repair an earlier unprotected field.

Admin UI is at `pb-yourapp.percher.run/_/` (create collections, set API rules, manage data). Its login is separate from your Percher login and your app's user accounts. When PocketBase is first provisioned during a deploy, Percher uses the owner's email at that time and generates a password. It stores `POCKETBASE_ADMIN_EMAIL` and `POCKETBASE_ADMIN_PASSWORD` encrypted and, by default, injects them into your server; the initial password is never shown, and `percher env list` masks it. Sign in with your PocketBase credentials. If you don't have them, use **Reset admin password** beside the Data tab's admin link or run `bunx percher data reset-superuser <app> --reveal`. Save the returned email and new password for future sign-ins; the previous password for that login stops working immediately. If your server uses these credentials, redeploy to apply the new password. Don't use PocketBase's "forgot password" flow for superuser recovery; it needs SMTP, which isn't configured by default.

Email (SMTP) is bring-your-own: Percher doesn't provide SMTP for the sidecar, so every PB feature that sends mail (signup verification, password reset, OAuth notifications) is a silent no-op until you configure a provider in the PB admin UI under Settings, Mail settings (Resend, Postmark, SES, Mailgun, SendGrid, or your own). Until then PocketBase's password-reset endpoint still returns HTTP 204 even though no mail leaves the box.

PocketBase keeps its own request log: every API request, including superuser requests with their full URL and query filter, for 5 days by default. Requests from Percher's database tools, when they are enabled for the app, appear there too. Percher never changes this setting. To stop logging, open the admin UI, go to Logs, open the log settings and set Max days retention to 0.

## Keep admin credentials out of app env

By default, managed PocketBase injects `POCKETBASE_ADMIN_EMAIL` and
`POCKETBASE_ADMIN_PASSWORD` into your server. Only opt out if your server code
does not use them:

```toml
[data]
mode = "pocketbase"
superuser_env = false
```

False persists when later files omit the setting, including old-version
redeploys. To undo it, set `superuser_env = true` in a new live publish. A preview
cannot re-enable it. Publish output, app context and the dashboard show the
effective policy. User env values and `build.pass_env` cannot restore either key
while it is off.

Percher keeps the encrypted credentials for schema, OAuth and data operations.
Owner data tools still work; `percher data reset-superuser <app> --reveal` rotates
and reveals a password for the PocketBase admin panel. Without `--reveal`, reset
does not print it. Reset does not re-enable app injection.

Opt-out controls future delivery; it does not erase copied credentials, old
running processes or in-flight access. If you suspect exposure, also rotate with
`percher data reset-superuser <app>` after opting out. Rotation makes the old
admin password stop working; it does not revoke other secrets your app holds.

Rollback to a version deployed while admin env was on is refused. Run
`percher redeploy <deployId>` to create a new deployment under the current policy;
if its source was evicted, publish that source version again.

## Verify the deployed app
Sign up and sign in, create and read a record, then check that signed-out visitors and a second user cannot read private records. Reload and publish again to confirm persistence. Test verification and reset email delivery if your app uses them. See [backup and restore](https://percher.app/docs/backup-and-restore) and [export data](https://percher.app/docs/export-data).

Full guide: https://percher.app/docs/pocketbase
