by PostHog
Run a SQL query
Runs a SQL (HogQL) query over the project's events, persons and other data and returns the rows.
Updated Oct 11, 2026
run-sql-query.md
Run a SQL query
Runs a SQL (HogQL) query over the project's events, persons and other data and returns the rows.
Note: Needs a personal API key with the query:read scope. Returns up to 100 rows by default and up to 50,000 with an explicit LIMIT; it is for ad-hoc analysis, not bulk export.
Set up
Use the first option your agent supports.
MCP (official, remote)
Add this server to your agent's MCP settings, then sign in when asked.
{ "mcpServers": { "posthog": { "url": "https://mcp.posthog.com/mcp" } } }
Call the MCP tool execute-sql.
Server URL: https://mcp.posthog.com/mcp
CLI (official)
Install the command, then confirm it runs.
npm install -g @posthog/cli@latest
posthog-cli --version
Run posthog-cli api call execute-sql.
Set $POSTHOG_CLI_API_KEY in your environment first (get a key: https://app.posthog.com/settings/user-api-keys?preset=mcp_server).
API (official)
- Base URL: https://us.posthog.com
- Endpoint:
POST /api/projects/{project_id}/query/ - Auth: send the header
Authorization: Bearer $POSTHOG_PERSONAL_API_KEY - Get a key: https://us.posthog.com/settings/user-api-keys
- Docs: https://posthog.com/docs/api
Before anything else, confirm access with the cheapest read-only call, like a list or a search. Never send or change anything to test access.
Rules
- Ask the user before anything that sends messages, costs money, or changes data, and say how many records it touches. One approval covers a batch the user has seen.
- Never print API keys.
Ways in
Workflows using Run a SQL query
TContributed by thedogwiththedataonitAlert sales when a company keeps viewing your pricing pageCounts pricing-page views by company in PostHog, sizes each company with Apollo, and posts it to Slack with its HubSpot owner.
P
A
H+1
TContributed by thedogwiththedataonitAlert sales when an ideal-fit company browses your siteResolves anonymous PostHog visitors to companies with People Data Labs, judges fit and buying stage with Jev, and alerts sales in Slack.
P
P
T+2
TContributed by thedogwiththedataonitEmail a trial offer to active free usersFinds opted-in free users in PostHog who reached your activation event, skips paying Stripe customers, and emails a trial offer with Brew.
P
S
B