National Address Register search/retrieval workspace.
- The public site runs entirely on hosted services: the web app on Netlify, the API as a Neon Function, and a 3M-row sample of the data in Neon Postgres. See Deployment.
- Large source datasets, GeoJSON files, and local database storage are ignored by Git.
- For development, Postgres 16 runs locally on port
55432with the full NAR CSV dataset imported intonar_addresses. - The HTTP API lives in
server/(Fastify + TypeScript). - A React frontend is available from
apps/web. - Verified row count:
17,169,294. - Verified Burlington rows:
Burlington, ON:79,160Burlington, NL:101
server/ Fastify + TypeScript bearer-token HTTP API (see server/README.md)
apps/
web/ Vite React frontend for random address retrieval
docs/
reference/ NAR PDF/image reference files
scripts/ Local database lifecycle and CLI lookup scripts
sql/ Postgres schema for imported NAR CSV chunks
Install dependencies once:
pnpm installStart Postgres, then start the app:
./scripts/db-start.sh
pnpm devOpen the frontend at:
http://127.0.0.1:5173
pnpm install installs all workspace packages (the API in server/ and the web
app in apps/web/), and pnpm dev starts both the API and the frontend.
The API (server/) listens at http://*********:8787. In dev mode, the API and
frontend default to the bearer token local-dev-token. Set ADDRESS_API_TOKEN
and VITE_ADDRESS_API_TOKEN to the same value if you override it.
The web app sends product analytics to PostHog. All keys are optional — when unset, analytics simply no-ops, so local dev works without any PostHog account.
Client-side variables are read by the browser bundle (Vite requires the VITE_
prefix) and can be set in apps/web/.env.local:
| Variable | Required | Description |
|---|---|---|
VITE_POSTHOG_PROJECT_TOKEN |
No | Public project token (phc_...) used to send events from the browser. Leave unset to disable analytics. |
VITE_POSTHOG_HOST |
No | PostHog ingestion host. Defaults to https://us.i.posthog.com. |
Build-time variables enable PostHog source-map upload so production stack
traces de-minify in PostHog error tracking. They must not use the VITE_
prefix (they are never bundled into the browser) and are set on the build
runner — for this project, in the Netlify site's environment variables. The
upload step only runs when both POSTHOG_API_KEY and POSTHOG_PROJECT_ID are
present, so local and PR builds skip it automatically:
| Variable | Required | Description |
|---|---|---|
POSTHOG_API_KEY |
For upload | Personal API key with the error-tracking:write scope. |
POSTHOG_PROJECT_ID |
For upload | Numeric project ID from PostHog project settings. |
POSTHOG_HOST |
No | PostHog host for the upload. Defaults to https://us.i.posthog.com. |
The random pick uses an index-only scan over a covering index (Heap Fetches: 0), which keeps it fast and uniform without sampling (~20 ms warm). The shell
CLI uses the same exact-query path. See server/README.md.
Example API call:
curl \
-H "Authorization: Bearer local-dev-token" \
"http://127.0.0.1:8787/api/random-address?city=Burlington&province=ON&verbose=true"Production API start requires an explicit token:
ADDRESS_API_TOKEN="replace-me" NODE_ENV=production pnpm --filter random-address-api startPostgres data lives directly on the external FATRIOT APFS SSD at
/Volumes/FATRIOT/postgres/data. Because that drive is APFS (not ExFAT), the old
sparseimage workaround is gone; the scripts just verify the drive is mounted
before starting Postgres. Override PG_MOUNT/PGDATA (e.g. in .env.local) if
your data lives elsewhere. See FATRIOT setup and
link & cabling.
./scripts/db-init.sh # one-time: initdb + schema (creates the cluster)
./scripts/import-addresses.sh # bulk-load the NAR CSVs and rebuild indexes
./scripts/db-optimize.sh # one-time: covering index + VACUUM ANALYZE
./scripts/db-start.sh # start Postgres (./scripts/db-stop.sh to stop)
./scripts/random-address.sh Burlington
./scripts/random-address.sh Burlington ON
./scripts/random-address.sh --city Burlington --province ON
./scripts/random-address.sh --city Burlington --province ON --verbose
cp .env.example .env.local
./scripts/api-start.shBy default, random-address.sh prints the formatted address, city, province, and
postal code. Use --verbose when you also need the source loc_guid and
addr_guid.
To host a free-tier-sized copy (about 3M sampled rows, 350 MB), see Hosted sample database.
The web app's Facts page (municipality sizes, ties for smallest, street and
building extremes) reads apps/web/src/facts.generated.json, which is
committed. It is computed from the full local import, not the hosted sample,
so it only needs regenerating after a new NAR release is imported:
./scripts/db-start.sh
./scripts/facts-build.sh # NAR_ZIP defaults to tmp/nar-202507.zipThe script only reads the database (its SQL, sql/facts.sql, works in temp
tables) and takes about five minutes. It needs the release zip because the
Locations/*.csv files carry each building's census subdivision code and
coordinates, which nar_addresses does not. Output is deterministic, and the
script fails if new data contradicts a claim the page states in words (for
example, that the smallest municipality is a tie), so the copy gets updated
rather than going stale. apps/web/src/FactsView.test.tsx pins this release's
rendered values (such as "Toronto (1,316,783)"), so update its expectations
along with the regenerated JSON or CI will fail.
The API is a strongly-typed Fastify + TypeScript service
in server/ that uses a pooled pg connection (see
server/README.md). Locally it listens on 127.0.0.1:8787
and requires ADDRESS_API_TOKEN on every request. In production the same app
runs as a Neon Function next to the hosted database; see
Deployment. Never expose Postgres directly.
./scripts/db-optimize.sh # one-time: build covering index + VACUUM ANALYZE
./scripts/api-start.sh # start Postgres + build & run the API
./scripts/healthcheck.sh # verify Postgres + API are upA random pick is served by an index-only scan over a covering index (~20 ms
warm; cold first request drops from ~16.8 s to a few hundred ms). Run
./scripts/db-optimize.sh once after a bulk import.
Connection:
postgresql://janac@127.0.0.1:55432/random_address_retriever
Storage after import (on the FATRIOT APFS SSD):
- Postgres logical database size: about
5.3 GB - Data directory on the FATRIOT SSD (
/Volumes/FATRIOT/postgres/data): about6.3 GB - Covering index for fast lookups (
scripts/db-optimize.sh): about2.7 GB
Useful query:
SELECT civic_no, official_street_name, official_street_type, mail_postal_code
FROM nar_addresses
WHERE lower(csd_eng_name) = lower('Burlington')
AND mail_prov_abvn = 'ON'
ORDER BY random()
LIMIT 1;Address data is adapted from Statistics Canada's National Address Register (NAR). The NAR is available free of charge under the Statistics Canada Open Licence and contains information licensed under the Open Government Licence – Yukon.
When redistributing data or screenshots derived from the NAR, include the following attribution:
Adapted from Statistics Canada, National Address Register. This does not constitute an endorsement by Statistics Canada of this product. Contains information licensed under the Open Government Licence – Yukon.
This project is independent and is not affiliated with, endorsed by, or
officially associated with Statistics Canada or any Government of Canada
department. The raw NAR datasets (/Addresses/, /ca/) are large local
artifacts and are intentionally excluded from version control (see
.gitignore).
When deploying publicly, surface this attribution in the UI, keep the Terms of Service and Privacy Policy reachable, and retain bearer-token auth plus rate limiting on any exposed endpoint.