Skip to main content

pg-analytics-mcp

An MCP server for PostgreSQL analytics — a general-purpose "DBA-lite" toolkit that gives any MCP client (Claude Code, Cursor, etc.) instant visibility into schema structure, data quality, relationships, performance, and multi-environment comparison.

What it does

Exposes 20 read-only tools (+ 4 optional) organised in 6 categories:

Schema Discovery

  • database_summary — high-level overview: schema/table/view/FK/index counts, total size, extensions
  • scan_schemas — row counts for every table, grouped by schema
  • describe_table — column details: name, type, nullable, default, position
  • table_sizes — disk usage (data + indexes + toast) per table, ordered by size
  • find_tables — search tables by name pattern (ILIKE)
  • find_columns — find tables that have a column matching a pattern
  • list_empty_tables — quickly find tables with 0 rows
  • list_environments — list configured environments

Data Exploration

  • recent_rows — peek at the most recent rows (auto-detects timestamp/PK ordering)
  • column_value_counts — distinct values and frequencies for a column
  • column_stats — min, max, avg, null count, distinct count for a column

Relationships

  • list_constraints — all constraints (PK, unique, check, FK) for a table
  • foreign_keys — bidirectional FK relationships (incoming + outgoing)
  • compare_envs — compare row counts across DEV / STG / PROD

Performance

  • index_usage — index scan stats and unused index detection
  • slow_query_candidates — tables with high sequential scan counts (missing index candidates)
  • bloat_estimate — tables with dead tuples that may need VACUUM

Data Quality

  • table_health — row count + last inserted_at/updated_at for a table
  • null_report — null percentage for every column in a table
  • duplicate_check — find duplicate rows based on a set of columns

Fail Table Tracking (opt-in via PG_FAIL_SCHEMA)

These 4 tools are only registered when PG_FAIL_SCHEMA is set. They discover *_fails tables in the specified schema that have run_id, stage, comment, failed_at columns.

  • pipeline_fail_tables — discover all *_fails tables with row counts and stats
  • pipeline_fail_summary — cross-entity failure summary grouped by entity, stage, or both
  • pipeline_fail_details — drill into a specific entity's fail table with optional filters
  • pipeline_fail_runs — analyse which runs generated the most failures

Quick start

Prerequisites

  • Python 3.12+
  • uv (recommended) or pip
  • One or more PostgreSQL instances

Install

Option A — run directly with uvx (no clone needed):

uvx pg-analytics-mcp

Option B — clone and run:

git clone https://github.com/fabdendev/pg-analytics-mcp.git
cd pg-analytics-mcp
uv sync

Configure

Set environment variables for each PostgreSQL environment (at least one is required):

Variable Description Required
PG_LOCAL_URL PostgreSQL DSN for LOCAL At least one URL
PG_DEV_URL PostgreSQL DSN for DEV At least one URL
PG_STG_URL PostgreSQL DSN for STG Optional
PG_PROD_URL PostgreSQL DSN for PROD Optional
PG_INCLUDE_SCHEMAS Comma-separated allowlist of schemas to scan Optional
PG_IGNORE_SCHEMAS Comma-separated schemas to skip (added to internal exclusions) Optional
PG_FAIL_SCHEMA Schema containing *_fails tables — enables pipeline_fail_* tools Optional
PG_READ_ONLY Reserved for future write tools (not yet used) Optional
export PG_DEV_URL="postgresql://user:pass@host:5432/dbname"
export PG_STG_URL="postgresql://user:pass@host:5432/dbname"   # optional
export PG_PROD_URL="postgresql://user:pass@host:5432/dbname"  # optional

# Schema filtering (optional — pick one, not both)
export PG_INCLUDE_SCHEMAS="core,trading,pipeline"  # only scan these
export PG_IGNORE_SCHEMAS="orion,shared"             # skip these

Supports postgresql+asyncpg:// URLs (the driver prefix is stripped automatically). If both PG_INCLUDE_SCHEMAS and PG_IGNORE_SCHEMAS are set, the include list takes precedence.

Add to Claude Code

Add to your Claude Code MCP settings (~/.claude/settings.json or .mcp.json):

If using uvx:

{
  "mcpServers": {
    "pg-analytics": {
      "command": "uvx",
      "args": ["pg-analytics-mcp"],
      "env": {
        "PG_DEV_URL": "postgresql://user:pass@host:5432/dbname",
        "PG_STG_URL": "postgresql://user:pass@host:5432/dbname"
      }
    }
  }
}

If installed from clone:

{
  "mcpServers": {
    "pg-analytics": {
      "command": "uv",
      "args": ["run", "--directory", "/path/to/pg-analytics-mcp", "pg-analytics-mcp"],
      "env": {
        "PG_DEV_URL": "postgresql://user:pass@host:5432/dbname"
      }
    }
  }
}

Security

All tools are read-only. No data is ever modified. Additional safeguards:

  • Identifier validation — all user-provided schema/table/column names are validated against ^[a-zA-Z_][a-zA-Z0-9_]*$ and quoted
  • Row limits — row-level queries capped at 100, aggregation queries at 200
  • Statement timeout — potentially expensive queries (null_report, column_stats, duplicate_check, column_value_counts) use a 30s timeout
  • Direction validation — order_dir restricted to ASC/DESC only

Multi-environment support

Configure up to 4 environments (LOCAL, DEV, STG, PROD). The first configured environment becomes the default. Use compare_envs to quickly spot row count differences across environments.

Development

uv sync --extra dev
uv run ruff check pg_analytics_mcp/    # lint
uv run python -m pg_analytics_mcp      # start server locally

License

MIT

Release files for pg-analytics-mcp 0.5.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pg-analytics-mcp 0.5.0
File Size Uploaded
pg_analytics_mcp-0.5.0.tar.gz 73.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pg-analytics-mcp 0.5.0
File Interpreter ABI Platform
pg_analytics_mcp-0.5.0-py3-none-any.whl Python 3 none any Details

Total release size: 88.6 kB

Release files / pg_analytics_mcp-0.5.0.tar.gz

Download URL pg_analytics_mcp-0.5.0.tar.gz
Size 73.4 kB
Tags Source
SHA-256 checksum
How to use checksums
cfa5b040dccc5c8e30379bbc9010a3327322fba3179f21b5ff311a967da15c11
BLAKE2b-256 checksum
How to use checksums
fb99d3131bb3bdaed982b22e0df85cece2e6a3428b587a5723f2656d31ded769
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.9.26 {"installer":{"name":"uv","version":"0.9.26","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"22.04","id":"jammy","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Release files / pg_analytics_mcp-0.5.0-py3-none-any.whl

Download URL pg_analytics_mcp-0.5.0-py3-none-any.whl
Size 15.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a2a826557b87d65f04c0b52d2ab5b65f6f3d4b8f595f80c48f5ef4be33ce4485
BLAKE2b-256 checksum
How to use checksums
cb409ceda78ddc5f149866f92e5e900794edec8fe8b85b99a7b7ec2f0bf55463
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.9.26 {"installer":{"name":"uv","version":"0.9.26","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"22.04","id":"jammy","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Release history Release notifications | RSS feed

This release

0.5.0 This release

2 release files

0.4.1

2 release files

0.4.0

2 release files

0.3.1

2 release files

0.3.0

2 release files

0.2.3

2 release files

0.2.2

2 release files

0.2.1

2 release files

0.2.0

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page