querying-local-postgres
Skill721 starsChanged 58 days agoMITView on GitHub
Run read-only SQL against the local Postgres app database (SELECT, EXPLAIN, EXPLAIN ANALYZE on SELECT). Default local URL postgres://posthog:posthog@localhost:5432/posthog; else DATABASE_URL. Use when querying the local DB, inspecting tables, debugging data, or analyzing query plans. Mutations are strictly forbidden.
- Reads credentials
1 other project uses a version of this.
Adopted by
Most recently taken up by
- PostHog/posthogword for word, first seen Oct 4, 2026
Other versions1 project uses a version of this.
Word for word · 1
- PostHog/posthog★ 40k100% same
Edited · 0
None found.
Wrote their own version · 0
None found.
How matches are found. Word for word: the exact same text. Edited: shares passages with this file (“99% same” means 99% of its five-word phrases also appear here; 10% or more counts). Their own version: similar meaning, or, weakest, the same name. We can’t tell which project had it first. Projects are counted among the 200 most-starred candidates, and mentions are links to the repository we found, up to 50 per site, so both counts are a floor.
In this repository119 other files it gives its agents.
- AGENTS.mdAGENTS.md
- common/AGENTS.mdAGENTS.md
- proto/AGENTS.mdAGENTS.md
- rust/AGENTS.mdAGENTS.md
- CLAUDE.mdCLAUDE.md
- common/CLAUDE.mdCLAUDE.md
- proto/CLAUDE.mdCLAUDE.md
- rust/CLAUDE.mdCLAUDE.md
- .cursor/rules/django-python.mdcCursor rule
- .cursor/rules/react-typescript.mdcCursor rule
- .cursor/rules/rust.mdcCursor rule
- adding-activity-loggingSkill
- adding-api-scopesSkill
- adding-inbound-webhooksSkill
- adding-inbox-sourcesSkill
- adding-ingestion-warningsSkill
- adding-mcp-store-serversSkill
- adding-personhog-rpcSkill
- adding-product-alertingSkill
- adding-project-secret-api-key-authSkill
- adopting-generated-api-typesSkill
- analyzing-experiment-precompute-canarySkill
- analyzing-experiment-query-performanceSkill
- analyzing-insights-across-teamsSkill
- announcing-behavior-changesSkill
- auditing-llm-gateway-paritySkill
- auditing-warehouse-source-coverageSkill
- authenticating-to-clickhouseSkill
- authoring-ci-workflowsSkill
- autoresolving-pr-conflictsSkill
- building-product-empty-statesSkill
- clickhouse-migrationsSkill
- debugging-ci-failuresSkill
- debugging-local-replaySkill
- debugging-local-task-agent-runsSkill
- debugging-mcp-analyticsSkill
- debugging-signals-pipelineSkill
- debugging-table-access-deniedSkill
- depot-ciSkill
- depot-container-buildsSkill
- depot-github-runnersSkill
- developing-subscriptionsSkill
- django-migrationsSkill
- django-startup-timeSkill
- documenting-warehouse-sourcesSkill
- editing-agents-mdSkill
- establishing-code-ownershipSkill
- extending-hobby-smoke-testsSkill
- extending-personhog-test-harnessSkill
- finding-llm-gateway-migration-candidatesSkill
- fixing-flaky-testsSkill
- gating-production-deploysSkill
- gating-sensitive-actionsSkill
- generating-clickhouse-query-performance-reportsSkill
- hogliSkill
- implementing-mcp-toolsSkill
- implementing-mcp-ui-appsSkill
- implementing-warehouse-sourcesSkill
- improving-drf-endpointsSkill
- improving-mcp-toolsSkill
- The first 60 shown.
The file
We split this skill into the rules it gives an agent
5 rules. About 0 are common, 0 are distinctive, and 5 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈0
Distinctive · estimate
This author's own choices. Worth a look.
5
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Very specific: line 44
If the user requests a write operation, say: "This skill is read-only.
- Very specific: line 74
Use this hardcoded URL for day-to-day local queries (matches typical Docker Compose + port 5432 on localhost, SSL off):
- Very specific: line 146
Use single quotes for string literals in SQL inside the shell as usual.
- Very specific: line 147
Default LIMIT 100 unless the user specifies otherwise.
- Very specific: line 161
Use EXPLAIN ANALYZE on SELECT for slow Django queries replicated as SQL — mind loading production-sized data.
≈ “Common” is an estimate: at least 1 in 5 of the 213,897 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
Querying local Postgres (READ-ONLY) — PostHog repo
User's query: $ARGUMENTS
Scope: This repo uses PostgreSQL for app metadata (teams, projects, flags, Django models, etc.). Analytics event data lives in ClickHouse, not Postgres — use HogQL / ClickHouse tools for events-style questions unless the user explicitly wants Postgres.
When to use
- User asks to query the database, inspect tables, or run SQL against Postgres
- Debugging: Row-level checks (e.g. why a team/project/flag row looks wrong), migrations, constraints, duplicate keys
- Performance:
EXPLAIN/EXPLAIN (ANALYZE, …)on read-onlySELECTagainst Django or app tables
Instructions
- Strictly forbid mutations — See "Mutations strictly forbidden" below. If the user asks for any write or mutation, refuse and explain the skill is read-only.
- Translate the user's question into one or more read-only SQL statements.
- Show the SQL in a code block before running.
- Run using the command pattern below (always with
PGOPTIONS='-c default_transaction_read_only=on'to force a read-only connection). - Show results and give a brief interpretation (especially when used for debugging or plan review).
Mutations strictly forbidden
Do not run, suggest, or generate any of the following. Refuse and state that this skill is read-only.
- DML:
INSERT,UPDATE,DELETE,MERGE,TRUNCATE - DDL:
CREATE,DROP,ALTER,RENAME - Other writes:
COPY ... TO program,CALL(if it mutates),GRANT/REVOKE EXPLAIN ANALYZEon anything other than a read-onlySELECT(includingWITH … SELECT). Do not wrap DML inEXPLAIN ANALYZE— it would execute the write. The read-only connection below rejects writes, but the agent must not attempt this pattern.- Any statement that modifies data, schema, or roles
Allowed:
SELECT(includingWITH … SELECT)EXPLAIN…SELECT(estimate-only plan; no execution)EXPLAIN (ANALYZE, …) SELECT— executes theSELECTonce; use only for performance analysis. Must run on the read-only connection below.SHOW,SELECTfrom catalog views (pg_stat_*,information_schema, etc.) when read-only
If the user requests a write operation, say: "This skill is read-only. I can't run INSERT/UPDATE/DELETE or other mutations. Use a DB client or migration tool for writes."
EXPLAIN and EXPLAIN ANALYZE (performance)
| Goal | What to use |
| Plan shape, estimated costs, no execution | EXPLAIN (FORMAT TEXT, COSTS) or add VERBOSE |
| Actual timings, row counts, buffer hits | EXPLAIN (ANALYZE, BUFFERS, FORMAT TEXT) on the SELECT |
| Buffer + WAL stats | BUFFERS requires ANALYZE; WAL requires ANALYZE (PostgreSQL 13+) |
Safe pattern: the analyzed statement must be only a SELECT (or WITH … SELECT), run on the read-only connection (see Usage below). Example:
PGOPTIONS='-c default_transaction_read_only=on' psql "$DATABASE_URL" -v ON_ERROR_STOP=1 -c "
EXPLAIN (ANALYZE, BUFFERS, VERBOSE, FORMAT TEXT)
SELECT … LIMIT 100;"Optional flags (when useful): SETTINGS (show non-default GUCs), WAL (with ANALYZE), TIMING (default on in recent versions for ANALYZE).
Caveats:
EXPLAIN ANALYZEruns the query — can be slow or heavy on large scans; prefer a boundedSELECT(e.g. realisticWHERE,LIMITmatching production shape) when exploring.- Production / shared DBs — analyzing hot or wide queries can add load; prefer staging, a replica, or off-peak when the user cares about impact.
EXPLAINwithoutANALYZE— does not execute the inner statement (except some special cases); still only wrap read-only SQL.
PostHog: connection and DATABASE_URL
Local Postgres (host machine) — default for this skill
Use this hardcoded URL for day-to-day local queries (matches typical Docker Compose + port 5432 on localhost, SSL off):
| Setting | Value |
| Host | localhost |
| Port | 5432 |
| User | posthog |
| Password | posthog |
| Database | posthog |
| SSL | off |
# Prefer this unless the user says their local password/db differs
LOCAL_POSTGRES_URL='postgres://posthog:posthog@localhost:5432/posthog'Equivalent: postgresql://posthog:posthog@localhost:5432/posthog
Other local DBs on the same server: swap the path only, e.g. ...5432/posthog_persons.
Configuration source of truth (app): posthog/settings/data_stores.py (Django DATABASES, optional replica POSTHOG_POSTGRES_READ_HOST, direct POSTHOG_POSTGRES_DIRECT_HOST, PERSONS_DB_WRITER_URL, product DB routing from products/db_routing.yaml).
When not using the hardcoded URL: Connecting from the host with the same credentials is documented in Developing locally (fe_sendauth troubleshooting). Ensure containers are running.
Default env when DEBUG is on: Django builds a default DATABASE_URL from PGHOST (default db), PGUSER / PGPASSWORD, PGPORT, PGDATABASE — matching in-container hostnames. From the host, use localhost and the same user/password/database name unless your shell already exports DATABASE_URL.
Multiple PostgreSQL databases (same server in local compose; separate logical DBs):
- Main app DB: usually
posthog - Persons DB:
posthog_persons(PERSONS_DB_WRITER_URL/PERSONS_DB_READER_URL) - Product-isolated DBs:
posthog_<name>perproducts/db_routing.yaml(created bydocker/postgres-init-scripts/create-product-dbs.sh) - Other init scripts may create additional DBs (e.g. cyclotron) — inspect
docker/postgres-init-scripts/if needed
Point psql at the right database by changing the path in DATABASE_URL (e.g. .../posthog_persons).
Rust / sqlx: Some services use rust/.env for DATABASE_URL when working from posthog/rust — see rust/README.md.
Usage (command pattern)
Always force the connection read-only via PGOPTIONS='-c default_transaction_read_only=on' so Postgres rejects writes even if the generated SQL is wrong.
Why
PGOPTIONS, notSET SESSION CHARACTERISTICS AS TRANSACTION READ ONLY? Apsql -c "..."string with multiple statements runs as a single implicit transaction.SET SESSION CHARACTERISTICS AS TRANSACTION READ ONLYonly sets the default for subsequent transactions — the in-progress one keeps the read-write mode it was given atBEGIN, so a write in the same-cwould not be rejected.PGOPTIONS='-c default_transaction_read_only=on'sets the GUC at connection startup, so every transaction (including the implicit-cone) starts read-only. The inline equivalent isSET TRANSACTION READ ONLY;as the first statement of the-cstring (it affects the current transaction, unlikeSET SESSION CHARACTERISTICS).
Run from the PostHog repo root so relative env paths resolve.
Default — local hardcoded URL (posthog / posthog @ localhost:5432 / db posthog):
PGOPTIONS='-c default_transaction_read_only=on' psql "postgres://posthog:posthog@localhost:5432/posthog" -v ON_ERROR_STOP=1 -c "SELECT 1;"Option A — DATABASE_URL already in the shell (e.g. after flox activate or manual export):
PGOPTIONS='-c default_transaction_read_only=on' psql "$DATABASE_URL" -v ON_ERROR_STOP=1 -c "SELECT 1;"Option B — load from a gitignored env file at repo root (if DATABASE_URL is set there):
npx dotenv -e .env -- bash -c "PGOPTIONS='-c default_transaction_read_only=on' psql \"\$DATABASE_URL\" -c 'SELECT ...'"- Use single quotes for string literals in SQL inside the shell as usual; escape carefully when nesting quotes in
-c. - Default
LIMIT 100unless the user specifies otherwise. - For wide rows use
-x:psql ... -x -c "...".
Schema reference (PostHog)
- Django models → tables: see
posthog/models/(and product packages underproducts/). Table names are usually prefixed withposthog_and snake-cased (e.g.posthog_team,posthog_user). Confirm with\dt posthog_*in psql, or check the model'sMeta.db_tableif nonstandard. - Migrations:
posthog/migrations/(and product migration paths) define the authoritative DDL over time. - Person table name: configurable via
PERSON_TABLE_NAME(seedata_stores.py); defaultposthog_person.
Debugging with the query runner (PostHog-flavored)
- Confirm a row exists for a team, project, user, or feature-flag linkage; check soft-delete /
deletedfields where applicable. - Compare counts and joins to what the app assumes (e.g. membership, project access).
- Validate replica vs primary read differences only if the user is connected to the right host (replica:
POSTHOG_POSTGRES_READ_HOST). - Use
EXPLAIN ANALYZEonSELECTfor slow Django queries replicated as SQL — mind loading production-sized data.
Cross-reference
- Local setup and DB gotchas:
docs/published/handbook/engineering/developing-locally.md - Repo CLI:
hogli(see.agents/skills/hogli/SKILL.md)
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

