Skip to main content
Glama
pg-insight-mcp

pg-insight-mcp

pg-insight-mcp

M8ven Score

A read-only PostgreSQL server for the Model Context Protocol. It lets any MCP client (Claude Desktop, VS Code, Cursor, custom agents) explore a database schema, run analytical queries and inspect execution plans, without any chance of changing data.

Features

  • Schema discovery: schemas, tables, views, columns, constraints, indexes and incoming foreign keys

  • Safe querying: every statement runs inside a READ ONLY transaction with a statement timeout

  • Query guard: SQL is checked before it reaches the database. Only one statement is allowed, writes and DDL are rejected, EXPLAIN ANALYZE and dangerous functions such as pg_sleep, pg_read_file, dblink and set_config are blocked, and SELECT ... INTO is refused

  • Row caps: results are limited to a configurable row count and flagged as truncated when more rows exist

  • Plans without execution: explain_query returns the JSON plan plus a readable summary tree

  • Schema allow-list: restrict the server to selected schemas; system schemas are never exposed

  • Structured tool output, an MCP resource with the full schema overview, and a prompt for guided analysis

Related MCP server: PostgreSQL MCP Server

Tools

Tool

Description

list_schemas

User schemas with table and view counts

list_tables

Tables and views in a schema with estimated rows and size

describe_table

Columns, types, defaults, constraints, indexes, referencing tables

sample_rows

First N rows of a table

run_query

Run a read-only SQL statement

explain_query

Execution plan without running the query

Resource: schema://overview. Prompt: analyse(question).

Quick start

Requires Python 3.11+ and Docker (for the sample database).

docker compose up -d
python -m venv .venv
.venv/bin/pip install -e ".[dev]"

On Windows the virtual environment's tools are in .venv\Scripts\ instead of .venv/bin/.

The sample shop database (200 customers, 12 products, 1,500 orders) is created and seeded automatically on port 5434.

Run the server over stdio:

DATABASE_URL=postgresql://shop:shop@localhost:5434/shop pg-insight-mcp

PowerShell:

$env:DATABASE_URL = "postgresql://shop:shop@localhost:5434/shop"
.venv\Scripts\pg-insight-mcp

Or over HTTP:

pg-insight-mcp --transport streamable-http

Try it interactively with the MCP Inspector:

DATABASE_URL=postgresql://shop:shop@localhost:5434/shop npx @modelcontextprotocol/inspector .venv/bin/pg-insight-mcp

Click Connect, open the Tools tab and try:

Tool

Input

Expected

list_tables

schema: public

customers, products, orders, order_items

describe_table

table: orders

columns, primary and foreign keys, indexes

run_query

SELECT status, count(*) FROM orders GROUP BY status

order counts per status

run_query

DELETE FROM orders

rejected: only read-only queries are allowed

explain_query

SELECT * FROM orders WHERE customer_id = 7

plan using the customer index

Claude Desktop

Add the server to claude_desktop_config.json:

{
  "mcpServers": {
    "pg-insight": {
      "command": "/path/to/pg-insight-mcp/.venv/bin/pg-insight-mcp",
      "env": {
        "DATABASE_URL": "postgresql://shop:shop@localhost:5434/shop"
      }
    }
  }
}

On Windows the file is %APPDATA%\Claude\claude_desktop_config.json and the command is C:\\path\\to\\pg-insight-mcp\\.venv\\Scripts\\pg-insight-mcp.exe. Restart Claude Desktop after editing it.

Then ask questions such as "Which product category brought in the most revenue last quarter?".

Configuration

Variable

Default

Description

DATABASE_URL

required

PostgreSQL connection string

PG_INSIGHT_MAX_ROWS

200

Maximum rows returned per query

PG_INSIGHT_STATEMENT_TIMEOUT_MS

5000

Per-statement timeout

PG_INSIGHT_ALLOWED_SCHEMAS

all

Comma-separated schema allow-list

For production databases, also connect with a dedicated role that only has SELECT privileges. The server's checks are a second layer of defence, not a replacement for database permissions.

Project layout

src/pg_insight/
  config.py     settings from environment variables
  guard.py      SQL validation
  database.py   catalog queries and read-only execution
  server.py     MCP tools, resource and prompt
seed/           sample schema and data
tests/          guard unit tests and end-to-end MCP tests

Tests

docker compose up -d
pytest
ruff check src tests

The end-to-end tests connect an in-memory MCP client to the server and run against the sample database. They are skipped when the database is not reachable.

Stop the sample database with docker compose down.

License

MIT

Available Tools

6 tools
describe_tableDescribe tableB
Read-onlyIdempotent

Columns, constraints, indexes and incoming foreign keys of a table.

Inputs: table (required), schema. Returns: object

explain_queryExplain queryA
Read-onlyIdempotent

Show the PostgreSQL execution plan for a read-only query without running it.

Inputs: sql (required). Returns: object

list_schemasList schemasB
Read-onlyIdempotent

List user schemas with table and view counts.

Inputs: no named parameters. Returns: object

list_tablesList tablesA
Read-onlyIdempotent

List tables and views in a schema with estimated row counts and sizes.

Inputs: schema. Returns: object

run_queryRun queryA
Read-onlyIdempotent

Run a single read-only SQL statement inside a read-only transaction. Results are capped and marked as truncated when more rows exist.

Inputs: sql (required), max_rows. Returns: object

sample_rowsSample rowsB
Read-onlyIdempotent

Return the first rows of a table to see what the data looks like.

Inputs: table (required), limit, schema. Returns: object

Tool Schema Changelog

Recent tool additions, removals, and schema changes observed during successful MCP inspections.

Loading tool schema changelog…

    TDQS

    A3.6/5.0

    Scored across 6 tools

    Disambiguation4/5

    Each tool maps to a distinct exploration step (schemas, tables, table detail, plan, sample, ad-hoc query), so selection is generally obvious. Mild overlap exists: sample_rows and simple exploration could be done via run_query, but the descriptions make the intended specialization clear.

    Naming Consistency5/5

    All six names follow a consistent snake_case verb_noun pattern (list_schemas, list_tables, describe_table, explain_query, sample_rows, run_query). No mixed conventions or vague one-word names.

    Tool Count5/5

    Six tools is well-scoped for a read-only PostgreSQL inspection server, and each tool earns its place in the exploration workflow. No bloat or awkwardly thin surface.

    Completeness4/5

    The surface covers the core read-only workflow: enumerate schemas and tables, describe structure, inspect plans, sample data, and run arbitrary SELECTs. Minor gaps like searching for tables/columns by name or listing indexes/extensions across the database are workarounds via run_query rather than dead ends.

    Maintenance

    ActivityMaintained
    ResponsivenessNo recent community issues

    Related MCP Connectors

    Related MCP Servers

    • F
      license
      Not graded
      quality
      D
      maintenance
      Enables interaction with PostgreSQL databases through MCP, allowing users to explore database structures, inspect table schemas, and execute read-only SQL queries.
      -
    • -
      license
      Not graded
      quality
      Not graded
      maintenance
      Enables users to perform SQL query execution, schema exploration, and performance analysis on PostgreSQL databases through any MCP-compatible client. It prioritizes security with read-only protection by default and provides guided workflows for database documentation and optimization.
      -
    • F
      license
      A
      quality
      C
      maintenance
      Enables read-only access to a PostgreSQL database, allowing users to inspect schema, generate SQL from natural language, and execute read-only queries via MCP.
      5
      -
    • A
      license
      Not graded
      quality
      C
      maintenance
      Enables AI assistants to safely explore PostgreSQL schemas, inspect sample rows, draft queries, and read EXPLAIN plans through a read-only enforced interface for MCP clients.
      MIT