# Self-Hosted State Backends

Source: https://docs.chmonitor.dev/operate/advanced/state-backends

> Save chmonitor dashboards, per-user connections and agent conversations in your own ClickHouse or Postgres instead of Cloudflare D1.

Self-hosted chmonitor saves dashboards, per-user connections and agent conversations in your own ClickHouse (`CHM_STATE_CLICKHOUSE_URL`) or Postgres (`DATABASE_URL`); Cloudflare D1 exists only on the hosted Workers deployment. Without a backend, dashboards fall back to browser `localStorage` and conversations to memory.

  - ClickHouse: Reuse or dedicate an instance via CHM_STATE_CLICKHOUSE_*. Tables are created lazily (CREATE TABLE IF NOT EXISTS, ReplacingMergeTree) — no migrations.

  - Postgres: DATABASE_URL / POSTGRES_URL. Tables are also created lazily on first use. One string serves dashboards, connections, and conversations.

## Resolution order [#resolution-order]

Each store resolves its backend at runtime, fail-open and OSS-first:

 B{set?}
B -->|yes| Z[Use that backend]
B -->|no| C[Cloudflare D1\nCHM_CLOUD_D1]
C --> D{bound?}
D -->|yes, hosted| Z
D -->|no| E[ClickHouse state\nCHM_STATE_CLICKHOUSE_URL]
E --> F{URL set?}
F -->|yes| Z
F -->|no| G[Postgres\nDATABASE_URL]
G --> H{set?}
H -->|yes| Z
H -->|no| I[Fallback\nlocalStorage / memory]`"
/>

**Per-user rows need auth**

  Server-side dashboard and connection storage also requires the per-user feature
  gates (`CHM_FEATURE_CONVERSATION_DB` / `CHM_FEATURE_USER_CONNECTIONS_DB`) and an
  auth provider — rows are keyed by user id. See
  [Environment variables](/reference/environment-variables).

## ClickHouse state backend [#clickhouse-state-backend]

```bash
CHM_STATE_CLICKHOUSE_URL=http://clickhouse:8123

# Optional
CHM_STATE_CLICKHOUSE_USER=default          # default: default
CHM_STATE_CLICKHOUSE_PASSWORD=             # default: empty
CHM_STATE_CLICKHOUSE_DATABASE=chmonitor    # default: chmonitor
CHM_STATE_CLICKHOUSE_TABLE_PREFIX=chm_state_  # default: chm_state_
```

The user needs `CREATE DATABASE` / `CREATE TABLE`, `INSERT`, `SELECT`, and
lightweight `DELETE` permissions on the state database. Tables
(`chm_state_dashboards`, `chm_state_user_connections`) use
`ReplacingMergeTree(updated_at)` with `FINAL` reads, so updates are plain
inserts and no mutations are required for normal operation.

**Monitoring users are often read-only**

  This can be the same ClickHouse you monitor, but a monitoring user is often
  read-only — point `CHM_STATE_CLICKHOUSE_USER` at a writable account (or a
  separate small instance) if so.

## Postgres state backend [#postgres-state-backend]

```bash
DATABASE_URL=postgres://user:pass@host:5432/chmonitor
# or POSTGRES_URL / POSTGRES_PRISMA_URL
```

One connection string serves all three stores (`user_connections`,
`dashboards`, and the conversation tables).

Alert settings (routing, channels, custom rules) do not use the ClickHouse state backend. They need D1 or Postgres; see [Pages that need a state backend](/operate/advanced/feature-permissions#pages-that-need-a-state-backend).

## Related [#related]

  - [Environment variables](/reference/environment-variables): Full reference for every CHM_* variable, including the state backends.

  - [Conversation store backends](/guide/ai-agent/conversation-history/backends): Per-backend setup for conversation history persistence.

  - [Deploy with Docker](/operate/deploy/docker): Compose-based self-hosted deployment.