Skip to main content
Glama
pg-inspect-mcp

pg-inspect-mcp

by barkya28

pg-inspect-mcp

A read-only Postgres schema-inspection MCP server. Point an MCP client (Claude Desktop, Claude Code, or anything speaking MCP) at a database and explore schemas, tables, indexes, sizes, sample data and query plans in natural language.

Design principle: impossible, not forbidden

Most "read-only" AI database tools are read-only because a prompt says so. That is a warning label, not a constraint — prompt injection or model error walks straight through it.

This server enforces read-only access structurally, in two independent layers, neither of which trusts the model:

  1. The connection itself is read-only. Every connection is opened with default_transaction_read_only=on, so Postgres rejects any write — even one that somehow reaches the database.

  2. A SQL guard rejects writes before the database sees them. Free-form input (only accepted by explain_query) must be a single SELECT/WITH statement; stacked statements, writing CTEs, and DDL/DML keywords are refused. All identifier inputs are validated against a strict pattern and quoted with psycopg.sql.Identifier.

No amount of clever input produces a write, because no code path can perform one.

Related MCP server: PostgreSQL MCP Server

Tools

Tool

What it does

list_schemas

Non-system schemas

list_tables

Tables/views in a schema with estimated row counts

describe_table

Columns, types, nullability, defaults

list_indexes

Index definitions

table_stats

On-disk size, index size, live/dead tuples, last vacuum/analyze

sample_rows

Up to 50 sample rows

explain_query

EXPLAIN (FORMAT JSON) for a SELECT/WITH query — plan only, never executed

Setup

pip install -e .
export DATABASE_URL=postgresql://user:pass@host:5432/dbname
pg-inspect-mcp

For extra safety in shared environments, connect as a Postgres role that has only SELECT grants — then the least-privilege credential is a third independent layer.

Claude Desktop / Claude Code config

{
  "mcpServers": {
    "pg-inspect": {
      "command": "pg-inspect-mcp",
      "env": { "DATABASE_URL": "postgresql://user:pass@host:5432/dbname" }
    }
  }
}

Local database for development

docker compose up -d
export DATABASE_URL=postgresql://postgres:postgres@localhost:5433/postgres

Tests

The safety layer is the point of the server, so it is what gets tested:

pip install pytest && pytest

Covers identifier injection shapes, stacked statements, writing CTEs (WITH x AS (DELETE ...)), EXPLAIN DELETE, COPY, and the read-only connection option.

Failure modes considered

  • Prompt injection via table contents — sampled rows may contain hostile text; the guard doesn't care what the model wants, only what the connection can do.

  • Writing CTEs — WITH x AS (DELETE ... RETURNING *) is valid SQL that begins with an allowed keyword; explicitly rejected.

  • EXPLAIN ANALYZE — actually executes the query; this server only ever issues plain EXPLAIN.

  • Stacked statements — SELECT 1; DELETE ... refused before parsing.

License

MIT

Available Tools

7 tools
describe_tableA

Show columns, types, nullability and defaults for a table.

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

explain_queryA

Return the JSON query plan (EXPLAIN, no execution) for a SELECT/WITH query.

Inputs: query (required). Returns: result

list_indexesB

List indexes on a table with their definitions.

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

list_schemasA

List non-system schemas in the connected database.

Inputs: no named parameters. Returns: result

list_tablesA

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

Inputs: schema. Returns: result

sample_rowsA

Fetch up to 50 sample rows from a table.

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

table_statsB

Table size on disk, index size, and live/dead tuple estimates.

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

Tool Schema Changelog

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

Loading tool schema changelog…

    TDQS

    A3.8/5.0

    Scored across 7 tools

    Disambiguation5/5

    Each tool targets a distinct aspect of database inspection: schemas, tables, table structure, indexes, table statistics, sample data, and query plans. There is minimal overlap between them, and an agent can easily select the right tool based on the task.

    Naming Consistency4/5

    The names mostly follow a consistent verb_noun pattern (list_schemas, list_tables, describe_table, list_indexes, sample_rows, explain_query). table_stats breaks the pattern slightly, being noun_noun rather than verb_noun, but it is still readable and predictable.

    Tool Count5/5

    Seven tools is well-scoped for a PostgreSQL inspection server. Each tool serves a clear purpose without unnecessary redundancy, and the count fits comfortably within the ideal range.

    Completeness4/5

    The tool surface covers the primary inspection workflow: discovering schemas/tables, examining columns, viewing indexes, retrieving table statistics, sampling rows, and explaining queries. Minor gaps like constraint/foreign key information or listing database objects beyond tables exist, but they are not critical for basic inspection.

    Maintenance

    ActivityStale
    ResponsivenessNo recent community issues

    Related MCP Connectors

    Related MCP Servers

    • -
      license
      Not graded
      quality
      Not graded
      maintenance
      Enables secure read-only interactions with PostgreSQL databases through natural language. Provides database inspection, table listing, and SQL query execution with built-in security validation.
      -
    • A
      license
      Not graded
      quality
      D
      maintenance
      Enables AI assistants to safely interact with PostgreSQL databases through read-only operations, providing schema discovery, table inspection, and query execution capabilities with structured context awareness.
      MIT
    • A
      license
      Not graded
      quality
      D
      maintenance
      Provides read-only access to PostgreSQL databases, enabling users to list tables, view table structures and statistics, and execute SELECT queries safely through natural language.
      153 npm
      MIT
    • A
      license
      Not graded
      quality
      B
      maintenance
      Enables secure read-only access to PostgreSQL databases, allowing users to list tables, query schemas, execute SELECT statements, and inspect table structures through natural language interactions.
      468 npm
      4
      MIT