# Pg Doctor

> Pg Doctor is an open-source MCP server for databases. Read-only PostgreSQL diagnostics for AI agents: slow queries, plans, bloat, locks. It runs locally on your machine and is started by your AI client.

- **Kind:** MCP server
- **Pricing:** Open source
- **Platforms:** CLI, Linux, macOS, Windows, Self-hosted, API
- **Repository:** https://github.com/alaev-dev/pg-doctor
- **MCP Registry name:** io.github.alaev-dev/pg-doctor
- **Categories:** [databases](https://ai-powered.com/categories/databases)
- **Listing updated:** 2026-12-02

## About

Pg Doctor is a Go server that gives an AI assistant read-only access to PostgreSQL performance data. Instead of handing the agent a SQL prompt, it offers eight fixed tools built on pg_stat views and system catalogs, so the agent can find the statements that cost the most time, plan them, and check indexes and table health without running queries of its own.

The server runs over stdio from a Docker image or a Go binary, or over Streamable HTTP for shared deployments. Every session is forced into a read-only transaction with a five-second statement timeout, and the database role is expected to hold pg_monitor. Query text comes only from pg_stat_statements with literals already masked, and table or column comments that try to instruct the agent are withheld and flagged.

It suits developers and database operators who use MCP clients such as Claude Code or Cursor and want help tracking down slow pages, lock chains, replica lag or unused indexes. Remote mode adds per-tool scopes, OAuth token introspection, rate limits, an audit log and OpenTelemetry tracing.

## Features

- **Slow query ranking:** Lists the statements using the most total or mean time from pg_stat_statements, with each one's share of overall load.
- **Read-only query plans:** Plans a recorded statement by its queryid using a generic plan without executing it or accepting SQL input.
- **Lock chain analysis:** Follows blocking chains to find the root blocker and flags sessions idle inside open transactions.
- **Index and table inspection:** Shows table sizes, index usage, sequential versus index scans, dead rows and vacuum history, and finds unused indexes.
- **Replication health checks:** Reports replay lag per replica, replication slots, retained WAL and whether replay is paused.
- **Scoped remote access:** Limits each token to chosen tool groups and applies per-token rate limits and audit logging in HTTP mode.

## Good for

- Finding out why a page that queries the database has become slow
- Spotting the root blocker when requests hang on database locks
- Checking whether replicas are falling behind and how much WAL slots retain
- Identifying indexes that have not been scanned since statistics were reset
- Reviewing table bloat and vacuum status before planning maintenance

## Install

### Claude Code: Add with the CLI (Terminal)

```sh
claude mcp add pg-doctor --env DATABASE_URL='<DATABASE_URL>' -- docker run -i --rm -e DATABASE_URL ghcr.io/alaev-dev/pg-doctor:0.3.0
```

Add --scope user to use it in every project, or --scope project to share it in .mcp.json.

### Claude Code: Or share it with your project (.mcp.json)

```json
{
  "mcpServers": {
    "pg-doctor": {
      "command": "docker",
      "args": [
        "run",
        "-i",
        "--rm",
        "-e",
        "DATABASE_URL",
        "ghcr.io/alaev-dev/pg-doctor:0.3.0"
      ],
      "env": {
        "DATABASE_URL": "<DATABASE_URL>"
      }
    }
  }
}
```

### Claude Desktop (claude_desktop_config.json)

```json
{
  "mcpServers": {
    "pg-doctor": {
      "command": "docker",
      "args": [
        "run",
        "-i",
        "--rm",
        "-e",
        "DATABASE_URL",
        "ghcr.io/alaev-dev/pg-doctor:0.3.0"
      ],
      "env": {
        "DATABASE_URL": "<DATABASE_URL>"
      }
    }
  }
}
```

Settings → Developer → Edit Config, then quit and reopen Claude Desktop.

### Cursor (~/.cursor/mcp.json)

```json
{
  "mcpServers": {
    "pg-doctor": {
      "command": "docker",
      "args": [
        "run",
        "-i",
        "--rm",
        "-e",
        "DATABASE_URL",
        "ghcr.io/alaev-dev/pg-doctor:0.3.0"
      ],
      "env": {
        "DATABASE_URL": "<DATABASE_URL>"
      }
    }
  }
}
```

Use .cursor/mcp.json in a project to share it with the project.

### VS Code (~/.copilot/mcp-config.json)

```json
{
  "mcpServers": {
    "pg-doctor": {
      "type": "stdio",
      "command": "docker",
      "args": [
        "run",
        "-i",
        "--rm",
        "-e",
        "DATABASE_URL",
        "ghcr.io/alaev-dev/pg-doctor:0.3.0"
      ],
      "env": {
        "DATABASE_URL": "<DATABASE_URL>"
      }
    }
  }
}
```

Or put the same entry in .mcp.json at your workspace root to share it.

### Codex: Add with the CLI (Terminal)

```sh
codex mcp add pg-doctor --env DATABASE_URL='<DATABASE_URL>' -- docker run -i --rm -e DATABASE_URL ghcr.io/alaev-dev/pg-doctor:0.3.0
```

### Codex: Or add to config.toml (~/.codex/config.toml)

```toml
[mcp_servers.pg-doctor]
command = "docker"
args = ["run", "-i", "--rm", "-e", "DATABASE_URL", "ghcr.io/alaev-dev/pg-doctor:0.3.0"]
env = { DATABASE_URL = "<DATABASE_URL>" }
```

### Gemini CLI and Antigravity: Gemini CLI (~/.gemini/settings.json)

```json
{
  "mcpServers": {
    "pg-doctor": {
      "command": "docker",
      "args": [
        "run",
        "-i",
        "--rm",
        "-e",
        "DATABASE_URL",
        "ghcr.io/alaev-dev/pg-doctor:0.3.0"
      ],
      "env": {
        "DATABASE_URL": "<DATABASE_URL>"
      }
    }
  }
}
```

Or run: gemini mcp add -s user -e DATABASE_URL='<DATABASE_URL>' pg-doctor docker -- run -i --rm -e DATABASE_URL ghcr.io/alaev-dev/pg-doctor:0.3.0

### Gemini CLI and Antigravity: Antigravity (~/.gemini/config/mcp_config.json)

```json
{
  "mcpServers": {
    "pg-doctor": {
      "command": "docker",
      "args": [
        "run",
        "-i",
        "--rm",
        "-e",
        "DATABASE_URL",
        "ghcr.io/alaev-dev/pg-doctor:0.3.0"
      ],
      "env": {
        "DATABASE_URL": "<DATABASE_URL>"
      }
    }
  }
}
```

Use .agents/mcp_config.json to add it to one workspace only.

### Devin Desktop (~/.config/devin/mcp_config.json)

```json
{
  "mcpServers": {
    "pg-doctor": {
      "command": "docker",
      "args": [
        "run",
        "-i",
        "--rm",
        "-e",
        "DATABASE_URL",
        "ghcr.io/alaev-dev/pg-doctor:0.3.0"
      ],
      "env": {
        "DATABASE_URL": "<DATABASE_URL>"
      }
    }
  }
}
```

On Windows the file is %APPDATA%\\devin\\mcp_config.json. Use .devin/mcp_config.json to share it with a project.

### Cline (cline_mcp_settings.json)

```json
{
  "mcpServers": {
    "pg-doctor": {
      "command": "docker",
      "args": [
        "run",
        "-i",
        "--rm",
        "-e",
        "DATABASE_URL",
        "ghcr.io/alaev-dev/pg-doctor:0.3.0"
      ],
      "env": {
        "DATABASE_URL": "<DATABASE_URL>"
      }
    }
  }
}
```

Open it from the MCP Servers icon → Configure → Configure MCP Servers.

### Zed (Zed settings.json)

```json
{
  "context_servers": {
    "pg-doctor": {
      "command": "docker",
      "args": [
        "run",
        "-i",
        "--rm",
        "-e",
        "DATABASE_URL",
        "ghcr.io/alaev-dev/pg-doctor:0.3.0"
      ],
      "env": {
        "DATABASE_URL": "<DATABASE_URL>"
      }
    }
  }
}
```

Open it with zed: open settings file. Merge context_servers into the settings you already have.

### JetBrains AI Assistant and Junie: AI Assistant (Settings → Tools → AI Assistant → Model Context Protocol (MCP) → Add)

```json
{
  "mcpServers": {
    "pg-doctor": {
      "command": "docker",
      "args": [
        "run",
        "-i",
        "--rm",
        "-e",
        "DATABASE_URL",
        "ghcr.io/alaev-dev/pg-doctor:0.3.0"
      ],
      "env": {
        "DATABASE_URL": "<DATABASE_URL>"
      }
    }
  }
}
```

The AI Assistant docs only show command and args. If the env block is ignored, use Junie for this server.

### JetBrains AI Assistant and Junie: Junie (~/.junie/mcp/mcp.json)

```json
{
  "mcpServers": {
    "pg-doctor": {
      "command": "docker",
      "args": [
        "run",
        "-i",
        "--rm",
        "-e",
        "DATABASE_URL",
        "ghcr.io/alaev-dev/pg-doctor:0.3.0"
      ],
      "env": {
        "DATABASE_URL": "<DATABASE_URL>"
      }
    }
  }
}
```

Use .junie/mcp/mcp.json to share it with a project.

### Kiro (~/.kiro/settings/mcp.json)

```json
{
  "mcpServers": {
    "pg-doctor": {
      "command": "docker",
      "args": [
        "run",
        "-i",
        "--rm",
        "-e",
        "DATABASE_URL",
        "ghcr.io/alaev-dev/pg-doctor:0.3.0"
      ],
      "env": {
        "DATABASE_URL": "<DATABASE_URL>"
      }
    }
  }
}
```

Use .kiro/settings/mcp.json to add it to one workspace only.

### Other clients (your client's MCP settings (most use this shape))

```json
{
  "mcpServers": {
    "pg-doctor": {
      "command": "docker",
      "args": [
        "run",
        "-i",
        "--rm",
        "-e",
        "DATABASE_URL",
        "ghcr.io/alaev-dev/pg-doctor:0.3.0"
      ],
      "env": {
        "DATABASE_URL": "<DATABASE_URL>"
      }
    }
  }
}
```

## Tools

From the tool list in its README, going by the names, 4 of 8 tools read data; 4 names don't say. From the server's own tool labels and tool names; not verified by us.

- `slow_queries`: Lists the statements that cost the most time, sorted by total or mean time, calls or disk reads.
- `explain_statement` (name suggests: reads): Plans a recorded statement by queryid using EXPLAIN GENERIC_PLAN without executing it.
- `list_tables` (name suggests: reads): Lists tables largest first with heap and index size, row estimate and comment.
- `describe_table` (name suggests: reads): Shows a table's columns, indexes, scan counts, dead rows and vacuum and analyze times.
- `table_bloat`: Estimates dead space per table from pg_stats without reading the tables.
- `blocking_locks`: Shows waiting sessions and root blockers using pg_blocking_pids.
- `replication_status` (name suggests: reads): Reports replica lag, replication slots, retained WAL and replay pause state.
- `unused_indexes`: Lists indexes not scanned since statistics were reset, excluding constraint-backing indexes.

## FAQ

### Can the agent run arbitrary SQL through Pg Doctor?

No. None of the tools accept SQL, and every session runs as a read-only transaction with a five-second timeout.

### What database permissions does Pg Doctor need?

A dedicated role with pg_monitor. table_bloat and explain_statement also need SELECT on the tables they inspect.

### Does Pg Doctor return my table rows?

No. It works from statistics and catalogs and does not select from your tables.

### Does slow_queries require any setup?

Yes, pg_stat_statements must be in shared_preload_libraries and the extension must be created in the database.

### Is Pg Doctor free?

Yes. Pg Doctor is open source, so you can use and self-host it for free.

### How do I add Pg Doctor to Claude Code?

Run this command in your terminal: claude mcp add pg-doctor --env DATABASE_URL='<DATABASE_URL>' -- docker run -i --rm -e DATABASE_URL ghcr.io/alaev-dev/pg-doctor:0.3.0

### What are the best alternatives to Pg Doctor?

The closest alternatives to Pg Doctor are PostgreSQL Performance (read-only), pgmcp, Pg Checkup, PostgreSQL (hardened, read-only) and Postgres AIops.

## Where this comes from

- Description: written from the project's README and its MCP Registry entry.
- Install steps: from the official MCP Registry entry.

## Alternatives

- [PostgreSQL Performance (read-only)](https://ai-powered.com/mcp/postgresql-performance-read-only): PostgreSQL Performance (read-only) is an open-source MCP server for databases.
- [pgmcp](https://ai-powered.com/mcp/pgmcp): pgmcp is an open-source MCP server for databases.
- [Pg Checkup](https://ai-powered.com/mcp/pg-checkup): Pg Checkup is an open-source MCP server for databases.
- [PostgreSQL (hardened, read-only)](https://ai-powered.com/mcp/postgresql-hardened-read-only): PostgreSQL (hardened, read-only) is an open-source MCP server for databases.
- [Postgres AIops](https://ai-powered.com/mcp/postgres-aiops): Postgres AIops is an open-source MCP server for databases.
- [Mcpg](https://ai-powered.com/mcp/mcpg): Mcpg is an open-source MCP server for databases.
- [MCP Database Doctor](https://ai-powered.com/mcp/mcp-database-doctor): MCP Database Doctor is an open-source MCP server for databases.
- [PostgreSQL performance Tuner MCP Server](https://ai-powered.com/mcp/postgresql-performance-tuner-mcp-server): PostgreSQL performance Tuner MCP Server is an open-source MCP server for databases.

---
Source: https://ai-powered.com/mcp/pg-doctor
