# How to set up self-hosted analytics

Self-host Pug step by step: one Go binary on PostgreSQL, ClickHouse, NATS, and Dragonfly, a static dashboard, an SDK pointed at your server, and Live.

Source: https://pug.sh/guides/self-hosted-analytics-setup

This guide covers what to provision for a self-hosted Pug and the order to do it in. The copy-paste commands live in the self-hosting docs, which change with each release.

## Pug is one Go binary and a static dashboard. The binary runs on four stores you may already operate.

The `pug` binary is the API server, every worker, the migrations, and the CLI; the dashboard is static files that any web server or CDN can serve. A self-hosted server has no plans or limits: no event cap, no seat count, and no feature kept for a paid edition. SSO works on it too, with Google or any OIDC provider such as Okta, Microsoft Entra ID, or Keycloak.

[PostHog’s self-hosting docs](https://posthog.com/docs/self-host) recommend PostHog Cloud for most users, give self-hosted instances no customer support and no guarantees, keep paid-plan features Cloud-only, and no longer support new Kubernetes deployments, which leaves its “hobby” Docker Compose deploy. [Pug vs PostHog](https://pug.sh/compare/posthog-alternative) compares the rest.

## What you’ll need

- A VM, container platform, or Kubernetes cluster, on amd64 or arm64
- PostgreSQL 15+, ClickHouse, NATS with JetStream, and Dragonfly or any Redis 7+
- Bun, to build the dashboard into static files
- A TLS-terminating proxy in front of the server’s port 3000
- Resend or Amazon SES for sign-in emails, or Google or an OIDC provider

Versions move with each release, so check them against the [self-hosting requirements](https://docs.pug.sh/self-hosting/requirements/).

## Self-hosting from scratch

1

### 1. Provision the four stores

PostgreSQL 15 or later holds accounts, projects, and dashboards; ClickHouse holds the events; NATS carries them from the server to the workers and must run with JetStream on; Dragonfly, or any Redis 7+, holds caches, the daily cookieless salt, and session stitching. Keep Dragonfly off disk (no snapshots, replicas, or backups), because deleting that salt every day is what keeps cookieless visitors anonymous. Managed equivalents such as Amazon RDS or ClickHouse Cloud work at compatible versions.

2

### 2. Migrate, then start the binary

Run `pug migrate` before the first boot and on every upgrade: it applies Postgres, NATS, then ClickHouse. On a small, low-volume node, `pug start` then runs the API server and every worker in one process. In production, run each role from its own image on GHCR (`ghcr.io/pug-sh/pug-server`, `pug-worker-events`, and the rest), published for amd64 and arm64, so you can scale the events worker on its own. Four scheduled jobs (purge, usage, retention, expired tokens) run outside that process, from cron, a systemd timer, or a Kubernetes CronJob.

3

### 3. Build and serve the dashboard

Clone `pug-sh/app`, set `VITE_API_BASE_URL` to your server’s public URL (it is compiled in, so a change means a rebuild), and run `bun run build`. Serve `dist/` from any static host over HTTPS, with unknown paths falling back to `index.html`. Then set `PUG_CORS_ORIGINS` and `PUG_DASHBOARD_BASE_URL` on the server to the dashboard’s origin, so its API calls and the links in sign-in emails work.

4

### 4. Sign in and create a project

Password and magic-link sign-in need an email provider (Resend or Amazon SES). Google and any other OIDC provider go in the server’s `PUG_CONFIG_FILE`, and setting `PUG_SSO_SECRET_KEY` lets org admins add their own SSO connections. Then create a project: it issues a public key (`pub_…`) for client SDKs and a private key (`prv_…`) for server SDKs and reads. Keys from Pug Cloud don’t carry over.

5

### 5. Point an SDK at your server

Add the Web, Android, iOS, or Flutter SDK with your project ID and public key, or the Node, Go, or Ruby SDK with the private key, and set one option, `endpoint` (`Endpoint` in Go), to your server’s public URL: the TLS origin in front of port 3000, not the internal address. Nothing else changes from a Pug Cloud install. On the web, autocapture (page views, clicks, scrolls, forms, rage clicks, and dead clicks) starts at `init()`.

6

### 6. Identify your users

Call `identify(userId)` when someone signs in. Their anonymous history merges into one profile, and traits like plan or email filter every insight. In the Web SDK’s default cookieless mode, `identify()` does nothing until the visitor consents.

7

### 7. Verify in Live

Open Live: it maps the last few minutes of visitors and what they did, refreshed every 10 seconds. Country and city come from Cloudflare’s headers in front of your server (`CF-IPCountry` and the visitor location headers), so without Cloudflare, or a proxy that sets the same headers, events arrive with no location.

- [Self-hosting docs](https://docs.pug.sh/self-hosting/)
- [Connect your SDKs](https://docs.pug.sh/self-hosting/connect-sdks/)
- [SDK quickstarts](https://pug.sh/sdks)

## Stuck on a step?

### How much hardware does it need?

The docs’ starting point for staging is 7 vCPU and 10 GB of RAM across the binary and the four stores. For production up to about 1k events a second, they suggest ClickHouse on 4 vCPU and 16 GB with 500 GB of SSD, PostgreSQL on 2 vCPU and 8 GB, and 2 vCPU and 2 GB for the server and for each worker type. Event volume and retention decide the real numbers.

### Where are the exact install commands?

The copy-paste setup (container images, environment variables, and version requirements) lives in the self-hosting docs, which change with each release. This guide is the map.

### Why ClickHouse and not Postgres for events?

ClickHouse is a columnar database built for fast analytical scans over large event volumes. Postgres handles accounts and config; ClickHouse handles the events that Trends, Funnels, and Retention query.

### Do I need NATS and Dragonfly?

Yes, both. NATS JetStream queues events between the server and the workers that write them, so a traffic spike waits in the stream instead of being dropped. Dragonfly holds the caches and the daily cookieless salt, and the server won’t start without it.

### Is self-hosting free?

Yes. The server and dashboard are AGPL-3.0 and the SDKs MIT, so you pay only for the machines and the stores.
