# Hosting, local development

> The whole Hosting stack on your own machine with one command, from a project file, with the same RLS guard and secure defaults as production.

Source: https://docs.infaris.com/en/pulseguard/hosting-local-dev

`infaris-guard hosting dev` starts everything your app uses from Hosting on your own machine: Postgres with the same extensions and the same RLS guard as production, Infaris Auth with a local mail inbox, the Data API, Realtime, object storage (S3 compatible), Valkey and your functions. What your app needs is in one file, `infaris.toml`. Later, `hosting dev push` puts the same file on a real project.

Local is no excuse for open tables. The stack runs the statements of a real host: new tables get RLS at once, exposing a table without RLS is a hard error, default privileges stay closed, buckets and functions are private and sign-up is by invitation only. You open things in `infaris.toml`, with a word you type on purpose.

## What you need

- Docker with compose: Docker Desktop, OrbStack or Docker Engine.
- The CLI: `bun add -g @infaris/guard-cli`. The local stack needs no login.

## Starting

```bash
cd examples/hosting-quickstart   # or your own app; infaris-guard hosting dev init writes an infaris.toml
infaris-guard hosting dev
```

The first time, Docker pulls the images. Then you see the addresses:

```text
Auth:                http://localhost:54321/auth/v1/ap_...
Data API:            http://localhost:54324/<ref>
Realtime:            http://localhost:54325/rt_...
Storage:             http://localhost:54321
Database:            postgresql://localhost:54322/db_...
Mail inbox:          http://localhost:54327
Function todo-stats: http://localhost:54331 (private)
```

Put the variables for your app in `.env.local` and start your app as always:

```bash
infaris-guard hosting dev status --env > .env.local
bun run dev
```

`status --env` gives the names Hosting sets when you attach Auth, the database and a bucket to a site (`DATABASE_URL`, `INFARIS_AUTH_URL`, `NEXT_PUBLIC_INFARIS_AUTH_KEY`, `INFARIS_STORAGE_*`, ...), plus what you list under `[env]` in `infaris.toml`. The values belong to your local stack; keep `.env.local` out of git.

Running `infaris-guard hosting dev` again applies changes to `infaris.toml` and new migrations. Only the database: `hosting dev db apply`.

## Signing in with the local inbox

Sign-in codes and invitations go to the stack's mail inbox, never to real addresses. Open `http://localhost:54327` in the browser, or read it in the terminal:

```bash
infaris-guard hosting dev mail           # the latest mails
infaris-guard hosting dev mail latest    # the latest one, with its code
```

Sign-up is by invitation: only the addresses under `[auth] users` get a code. To open it on purpose, set `signup = "open"`.

## The project file

```toml
project = "todo-quickstart"

[dev]
port = 54321                      # the stack uses port to port+19
app_url = "http://localhost:3000"

[auth]
signup = "invite_only"            # invite_only, domains or open
users = ["sanne@example.nl"]

[db]
migrations = "db/migrations"      # *.sql, once each, in name order
seed = "db/seed.sql"              # test data, after the migrations
types = "lib/database.types.ts"   # where hosting dev types writes

[[db.policies]]
table = "public.todos"
name = "own todos"
using = "(select auth.uid()) = user_id"
with_check = "(select auth.uid()) = user_id"

[[db.expose]]
table = "public.todos"
privileges = ["SELECT", "INSERT", "UPDATE", "DELETE"]

[realtime]
tables = ["public.todos"]

[[storage.buckets]]
name = "todo-files"

[functions.todo-stats]
path = "functions/todo-stats"

[functions.todo-stats.env]
DATA_URL = "${data.url}"
DATA_KEY = "${auth.publishable_key}"

[[crons]]
function = "todo-stats"
path = "/digest"
schedule = "0 7 * * *"

[env]
NEXT_PUBLIC_INFARIS_DATA_URL = "${data.url}"
NEXT_PUBLIC_INFARIS_REALTIME_URL = "${realtime.url}"
NEXT_PUBLIC_STATS_URL = "${functions.todo-stats.url}"
```

With `${...}` you use addresses and keys of the stack: `data.url`, `realtime.url`, `auth.url`, `auth.publishable_key`, `storage.url`, `database.url` and `functions.<name>.url`.

Migrations are recorded in `public.schema_migrations`, the same table the quickstart app uses at start. An app that runs its own migrations sees them as done.

## What the stack runs

| Part | Locally | As on Hosting |
| --- | --- | --- |
| Postgres | the image of the hosts, with pgvector | the same roles, `auth.uid()`, the RLS guard (event trigger), closed default privileges, a host's pg_hba |
| Auth | the hosting-api, on a local key | the same code, PKCE, bot protection, waiting time between codes, invitations |
| Data API | PostgREST behind the agent's gateway | the same gateway: keys, tokens, origins, limits |
| Realtime | the agent's Realtime server | the same server; channels are private until a policy on `realtime.messages` opens them |
| Storage | the hosting-api in front of an S3 backend | the same gateway, signed URLs, keys that expire |
| Functions | Deno 2.5 with the Hosting runtime | private unless `access = "public"`; no private networks |
| Mail | Mailpit | instead of real delivery |

What differs on purpose: no TLS (everything on `localhost`), and a function may reach the stack's own services on `localhost`. Other loopback and private addresses stay closed, as on a host. Crons do not run on a schedule locally; `hosting dev push` puts them on the real project.

## Database: migrations, types and test data

```bash
infaris-guard hosting dev diff               # what the database has and the migrations do not, as SQL
infaris-guard hosting dev migrate add_notes  # writes db/migrations/<time>_add_notes.sql
infaris-guard hosting dev types              # TypeScript types of the tables
infaris-guard hosting dev seed               # db.seed once more (or --from <file>)
```

`diff` replays your migrations on an empty shadow database with the same base and compares it with your local database; the same comparison as for database branches. `migrate` only includes drops with `--include-destructive`. Seeds run as the database's own role, so RLS and the guard apply there too.

```bash
infaris-guard hosting dev lint   # exit 1 on a critical or high finding (--strict also medium), for CI
```

## To a real project

```bash
infaris-guard hosting dev link --database <id> [--auth-project ap_...]
infaris-guard hosting dev push          # the plan: what changes
infaris-guard hosting dev push --yes    # and apply it
infaris-guard hosting dev pull          # the project's configuration into infaris.toml
```

`push` shows the plan first and changes nothing without `--yes`. It only adds or changes: it never removes a policy, table, bucket or function. Opening something (a public bucket or function, a table for `anon`) is listed separately in the plan. Local settings stay local: `localhost` origins, test users, the seed, and variables with `${...}` (set those with `hosting functions secrets set`). On Hosting, migrations run in your app or with `hosting db migrate`.

Without `--auth-project`, `push` makes the Auth project of `[auth]` (or links the existing project with that name), with the sign-up policy, domains, origins and redirect URLs from the file, and stores it in `.infaris/link.json`. Then it switches the Data API on with that project, then Realtime, and only then the policies: a policy on `realtime.messages` works in one go. A policy or Realtime table on a table your migrations still have to create is skipped with that reason; run `hosting db migrate` and then `push` again. A function is only deployed again when its code or entrypoint changed.

`pull` writes the project's policies, exposed tables, Realtime tables, buckets, functions and crons into `infaris.toml` (the old file stays as `infaris.toml.bak`; comments are lost).

## Guard

The local stack is not in Guard: Guard shows your real projects. After `hosting dev push` you see the result there, with the linter and alerts of production.

## Stopping and cleaning up

```bash
infaris-guard hosting dev stop          # stops everything, the data stays
infaris-guard hosting dev reset --yes   # removes containers, volumes and .infaris/dev
infaris-guard hosting dev logs api      # logs (api, db, gateway, realtime, rest or a function)
```

The state of the stack (passwords, keys) lives in `.infaris/dev/` next to `infaris.toml`, readable only for you and excluded from git. `.infaris/link.json` holds ids only and may go into git.

## Problems

- **A port is taken.** Set `[dev] port` to another value; the stack uses it and the 19 after it.
- **`row level security is off`.** A table under `[[db.expose]]` needs RLS. Turn it on in a migration (`alter table ... enable row level security`) and add a policy.
- **Realtime: `not allowed to join this private channel`.** Add a policy on `realtime.messages`, for example `using = "realtime.topic() = 'todos'"` for `authenticated`.
- **Pulling an image fails.** Set another image per part with `INFARIS_DEV_<NAME>_IMAGE` (for example `INFARIS_DEV_POSTGRES_IMAGE`).

## SDK and FAQ

Use the same SDKs as on Hosting, with values from `hosting dev status --env`. [Auth](hosting-auth), [Data API](hosting-data-api), [Storage](hosting-storage) and [Realtime](hosting-realtime) show integration patterns.

**Is `infaris-guard dev` enough?** The command for this stack is `infaris-guard hosting dev`. **Production parity?** Not complete: no local TLS, mail goes to the inbox and crons do not run on schedule. **Only reading docs?** You need not start the stack. Execution requires Docker and enough local memory.

## Develop without Docker

The `--remote` option requires Guard CLI 1.14 or later. Run `infaris-guard hosting dev --help` to check whether your version supports it. Use the local Docker stack with earlier versions.

Use `infaris-guard hosting dev --remote` to work with a restricted database branch and a separate development Auth project on Infaris Cloud. Sign in first and link the working directory to the right database. Use `--dry-run` to see what would be created. Development variables are written to a marked block in `.env.local`. Use the local Docker stack to test the Data API, Realtime, storage, functions and the mail inbox locally.
