pg-analytics-mcp
Click on "Install Server".
Wait a few minutes for the server to deploy. Once ready, it will show a "Started" state.
In the chat, type
@followed by the MCP server name and your instructions, e.g., "@pg-analytics-mcpshow monthly donation trends for the last 6 months"
That's it! The server will respond to your query, and you can continue using it as needed.
Here is a step-by-step guide with screenshots.
pg-analytics-mcp
A config-driven, read-only Postgres MCP server for Claude. Expose a Postgres schema to Claude over Streamable HTTP, with schema and enum values introspected from the live database at boot, and everything client-specific in a single YAML file.
Designed to run behind Cloudflare Access on a cloudflared → reverse-proxy
stack (a full provisioning playbook is included), but the server itself has no
Cloudflare dependency and runs anywhere.
Client-agnostic. Nothing under server/ knows about any particular client.
To serve a new one: copy the repo, write a config file, set .env.
Why this exists
The predecessor stacked three processes to work around a vendor package:
supergateway → enrich.py → postgres-mcp → Postgrespostgres-mcp speaks only stdio/SSE (Cloudflare requires Streamable HTTP), has
no configuration surface at all, and supergateway forked a child per MCP
session that was never reaped — measured 23 children / 15 connections against
a role limit of 20, which surfaced as "works for ~9 calls then everything fails,
including SELECT 1".
This server is one process with one shared pool. Measured: 1 process after 30 tool calls.
Related MCP server: postgres-mcp
Architecture
Claude → portal.<zone> Cloudflare MCP Server Portal (OAuth)
→ mcp-origin.<zone> Access app + Managed OAuth
→ cloudflared tunnel
→ traefik Host-header routing
→ this container uvicorn, Streamable HTTP at /mcp
→ Postgres read-only role → analytics.* viewsThe security boundary is the database role, not this server.
Quick start
cp .env.example .env # set DATABASE_URI + the deployment vars
$EDITOR config/example.yaml # domain prose for this client
docker compose up -d --build
curl -s localhost:8000/healthz # ok
curl -s localhost:8000/introspection # what the server decided at bootThen follow docs/PLAYBOOK-NEW-CLIENT.md for the Cloudflare side.
Configuration
.env — host-specific, the only thing that changes between VPSes:
Variable | Purpose |
| Read-only role. On the Supavisor pooler the username must carry |
| Container, image tag, and traefik router name |
| Public hostname; auto-added to the transport-security allowlist |
| External docker network traefik watches |
| Path to the client YAML inside the image |
| Host-side publish port (default 8000) |
config/<client>.yaml — the domain. Do not list columns or enum values
here: they are introspected from the live database at boot, so they cannot go
stale. Write only what introspection cannot know — business meaning and traps.
Tools
Built-in:
execute_sql(sql, limit=, offset=, timeout_ms=)— raw read-only SQL. Its description is assembled at boot from your authored prose plus the generated schema and enum lists. Every result reportsrows_returnedandrows_total; when truncated,rows_totalis stated as a lower bound rather than silently returning a partial answer that looks complete.timeout_msraises the statement timeout for one query, clamped tolimits.statement_timeout_max_msand reset afterwards so it cannot leak onto a pooled connection.explain_query(sql)— the plan, without executing anything.EXPLAIN, neverANALYZE.data_health()— runs the configured anomaly checks and reports only what fired. A number can be correct and still be the symptom of a broken process; this is what surfaces that instead of leaving it to whoever happens to look.list_views()— every readable object with columns, row counts, enums, plus the contract version, server start time and live data freshness.describe_view(name)— columns of one object.
Errors are made actionable. On undefined_column / undefined_table the
server appends the real column list for whichever known objects the query
mentioned. Postgres supplies a HINT only when a close match exists; for a typo
far from any real name it says nothing, and that silence is what leaves a caller
guessing.
Config-defined: every entry under tools.queries becomes a real MCP tool with
typed parameters. Parameters bind via psycopg named placeholders — never string
interpolation — and min/max are enforced before binding.
tools:
queries:
monthly_trend:
description: |
Donations per month. The most recent month is PARTIAL.
params:
months: {type: integer, default: 6, min: 1, max: 36}
sql: |
select ... where donated_at >= date_trunc('month', now())
- make_interval(months => %(months)s - 1)This is the bit that closes the gap with n8n: adding a tool is prose + SQL, not
Python. Every query tool echoes its parameters ([tool=top_cities params={…}])
so a result pasted into a report tomorrow is reconstructible without the
conversation that produced it.
Data-health checks
tools.data_health.checks are SQL that returns rows only when something is
wrong. Each has a severity and a description written for the person who has to
act on it:
tools:
data_health:
checks:
overdue_recurring_charges:
severity: critical
description: >-
Active subscriptions past next_charge_at that have not been charged.
Money not collected — check the charging scheduler.
sql: |
select count(*) as overdue_subscriptions, sum(amount) as iqd_uncollected
from recurring_subscriptions
where status = 'active' and next_charge_at < now()
having count(*) > 50 -- returns nothing when healthyThe having clause is the pattern: a healthy database returns zero rows, so the
check is silent until it matters. On first run against the live WHF data this
surfaced 4,350 overdue subscriptions worth 17.4M IQD per cycle — a finding no
amount of correct query answering would have produced.
No skill, no context document — deliberately
Earlier versions of this project shipped a Claude skill and a "paste into project instructions" document carrying the same domain knowledge. Both were deleted.
Within a day the skill had drifted: it was missing the free-text allowlisting,
the anonymous-sentinel cutoff date, data_health, timeout_ms and the
"still answerable" guidance — and it hardcoded an enum value that the server
now introspects. That is the exact failure this design exists to prevent,
recreated in a second file.
A parallel copy of the truth is a copy that will disagree with the truth. If
something needs to reach the model, put it in the server: server.instructions
for behaviour, the tool description for facts, list_views for the live
contract. All three travel with every conversation and cannot go stale, because
half of each is generated at boot.
For the same reason there is no "fetch the documentation" tool. A tool requires a call the model may not make; a description is simply present.
Why descriptions live here
Tool descriptions are the one context a model sees whenever the tool is available — every client, every conversation, no skill loading and no project instructions. Domain knowledge kept in an external document is knowledge the model often does not have.
Half of each description is authored (judgement), half generated (facts). The
generated half is why the boxy platform/processor and daily frequency can no
longer go missing the way they did in the hand-written prompt that preceded this.
Large schemas
tools.execute_sql.schema_detail controls how much generated schema rides in
the tool description, which is loaded into every conversation:
Value | Contents | Use when |
| objects, columns, row counts, enums | small schemas — best accuracy |
| object names, row counts, enums | large schemas; columns via |
| nothing | the model must call |
Six views cost ~1,000 tokens, which is a cheap insurance premium against
hallucinated column names. Sixty tables would cost ten times that in every
conversation — switch to compact there.
Operations
curl -s localhost:8000/selftest | python3 -m json.tool # privacy boundary assertions
curl -s localhost:8000/introspection | python3 -m json.tool # objects, enums, tools, limits
docker top <container> # must stay at 1 process
docker compose up -d --build # after a config editA config or schema change needs a restart — introspection is cached for the process lifetime, deliberately, so behaviour cannot drift mid-run.
Proving the privacy boundary
domain.not_available_assertions is a list of statements that must fail.
GET /selftest runs them and returns HTTP 500 if any succeeds:
{"pass": true, "checked": 13,
"assertions": [{"sql": "select display_name from customers limit 1",
"result": "column \"display_name\" does not exist", "pass": true}]}Documentation claiming a column is unreachable is only a claim. This turns it into a test — run it in CI, or after any change to views or grants. A statement that succeeds is a security defect, not a documentation one.
The five boundary tests
Re-run after any change to views, grants, or config. All five must fail:
update customers set city = 'x' where false; -- permission denied for view
update donations set amount = 0 where false; -- cannot update view (joined, so
-- not auto-updatable — a second,
-- independent guard)
select count(*) from public.donations; -- permission denied for table
select count(*) from public.website_orders; -- permission denied for table
create table analytics.t (id int); -- read-only transaction
select phone_number from customers limit 1; -- column does not existTwo guards refuse writes and which one fires depends on the view: simple views hit the role's missing grant, views carrying the free-text allowlist joins are rejected earlier as non-updatable. Assert that a write is refused, not that it produced a particular message.
Regression suite
python3 tests/regression.py # against localhost:8000
python3 tests/regression.py --url http://host:8010 --slow32 behavioural assertions covering the contract surface, SQL capability, truncation honesty, paging soundness, the write and PII boundary, derived-tool stability and the timeout override. Exit code 1 on any regression.
Every one of these encodes something that was established by hand and that a later change could silently undo — the truncation format, the ORDER BY caveat, the spike threshold's independence from window size. Run it after any change to the server, the views, or the grants.
limits.select_only exists but defaults off: the role is the boundary, and
a SQL validator on top blocks valid read-only constructs for no gain — that is
why postgres-mcp's restricted mode was abandoned.
Gotchas paid for in blood
Compose label keys are not variable-substituted. Labels must be list-form (
- "traefik...=value"), or you get a router literally named${MCP_CONTAINER_NAME}and traefik 404s.DNS-rebinding protection is on by default in the MCP SDK. The forwarded
Hostbehind a proxy must be allowed;MCP_HOSTNAMEandMCP_LOCAL_PORTare added automatically.Mounting the MCP app under your own Starlette replaces its lifespan. The session manager must be started explicitly (
server.session_manager.run()) or every request 500s with "Task group is not initialized".set_read_only/set_autocommitmust precede anyexecute()on a connection, or the pool fails with "connection in transaction status INTRANS".pg_class.reltuplesis meaningless for views, so row estimates fall back to a boundedcount(*)at boot.Supavisor rewrites
application_nameto "Supavisor", so per-client connection attribution through the pooler is not possible.Cloudflare caches the tool snapshot per MCP server entry. Resync, re-authentication and reconnecting all fail to clear it, and reconnecting can hand a client an older snapshot than it already had. Deleting and re-adding the server entry is the only fix. This is why
list_viewsannounces a contract version — compare it withGET /introspectionon the origin.MCP SDK 2.0 renamed
FastMCPtoMCPServerand moved it out ofmcp.server.fastmcp.requirements.txtis a full lock for that reason.
License
MIT — see LICENSE.
Tool Schema Changelog
Recent tool additions, removals, and schema changes observed during successful MCP inspections. Dates show when Glama detected each change.
No tool schema history has been recorded yet.
This server cannot be installed
Maintenance
Resources
Unclaimed servers have limited discoverability.
Looking for Admin?
If you are the server author, to access and configure the admin panel.
Related MCP Connectors
Query your warehouse or a CSV with Claude/ChatGPT over MCP, governed by table-level ACL + audit.
Read-only analytics for Convex apps, queryable via MCP from Claude, Cursor, and other clients.
Query your org's data in natural language — read-only MCP access to SQL, NoSQL, files & warehouses.
Query 40 databases from Claude, ChatGPT, or Cursor — on any device. Read-only, encrypted, audited.
Related MCP Servers
- AlicenseAqualityAmaintenanceQuery and manage PostgreSQL databases from Claude Code, Cursor, and any MCP client, with read-only by default and built-in schema introspection, EXPLAIN, and performance diagnostics.237,1394MIT
- AlicenseAqualityBmaintenanceExposes PostgreSQL query execution, EXPLAIN, and schema inspection tools to MCP-compatible clients like Claude Desktop.31MIT
- FlicenseNot gradedqualityDmaintenanceConnects to a PostgreSQL database and exposes schema inspection and safe SELECT query execution as MCP tools and resources for use with Claude Desktop or any MCP-compatible client.-
- FlicenseNot gradedqualityCmaintenanceProvides Claude Desktop and other MCP-compatible clients with read-only access to a PostgreSQL sales database, enabling SQL queries, schema inspection, and anomaly detection.-
Latest Blog Posts
- Who's Calling? MCP Hosts Are an Identity Blind Spot (And the Spec Knows It)By Om-Shree-0709 on .mcpAgent IdentityOAuth 2.1
- Your AI Chatbot Just Exposed Your CEO's Salary to an InternBy Om-Shree-0709 on .Agent IdentityMCP SecurityOAuth Delegation
- Why MCP Servers Need Execution Sandboxing (And Why Your Current Stack Isn't Enough)By Om-Shree-0709 on .Agentic AiPrompt InjectionWebAssembly
MCP directory API
We provide all the information about MCP servers via our MCP API.
curl -X GET 'https://glama.ai/api/mcp/v1/servers/Sa3fa/pg-analytics-mcp'
If you have feedback or need assistance with the MCP directory API, please join our Discord server