# pgmcp MCP server

> Provides read-only PostgreSQL diagnostics for analyzing slow queries, index health, table bloat, locks, and replication lag.

- Repository: https://github.com/pascalallen/pgmcp
- Page: https://hostedmcp.org/servers/pascalallen/pgmcp
- Transport: stdio
- Stars: 0
- License: MIT
- Latest version: 1.1.0

## Install

### Claude Code

```sh
claude mcp add --env 'PGMCP_DATABASE_URL=<YOUR_PGMCP_DATABASE_URL>' pgmcp -- docker run -i --rm ghcr.io/pascalallen/pgmcp:1.1.0
```

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

```json
{
  "mcpServers": {
    "pgmcp": {
      "command": "docker",
      "args": [
        "run",
        "-i",
        "--rm",
        "ghcr.io/pascalallen/pgmcp:1.1.0"
      ],
      "env": {
        "PGMCP_DATABASE_URL": "<YOUR_PGMCP_DATABASE_URL>"
      }
    }
  }
}
```

### VS Code (`.vscode/mcp.json`)

```json
{
  "servers": {
    "pgmcp": {
      "type": "stdio",
      "command": "docker",
      "args": [
        "run",
        "-i",
        "--rm",
        "ghcr.io/pascalallen/pgmcp:1.1.0"
      ],
      "env": {
        "PGMCP_DATABASE_URL": "<YOUR_PGMCP_DATABASE_URL>"
      }
    }
  }
}
```

### VS Code CLI

```sh
code --add-mcp '{"name":"pgmcp","type":"stdio","command":"docker","args":["run","-i","--rm","ghcr.io/pascalallen/pgmcp:1.1.0"],"env":{"PGMCP_DATABASE_URL":"<YOUR_PGMCP_DATABASE_URL>"}}'
```

### Claude Desktop (`claude_desktop_config.json`)

```json
{
  "mcpServers": {
    "pgmcp": {
      "command": "docker",
      "args": [
        "run",
        "-i",
        "--rm",
        "ghcr.io/pascalallen/pgmcp:1.1.0"
      ],
      "env": {
        "PGMCP_DATABASE_URL": "<YOUR_PGMCP_DATABASE_URL>"
      }
    }
  }
}
```

### Codex

```sh
codex mcp add pgmcp --env 'PGMCP_DATABASE_URL=<YOUR_PGMCP_DATABASE_URL>' --env 'PGMCP_DATABASE_URL=<YOUR_PGMCP_DATABASE_URL>' -- docker run -i --rm ghcr.io/pascalallen/pgmcp:1.1.0
```

### Codex config.toml (`~/.codex/config.toml`)

```toml
[mcp_servers.pgmcp]
command = "docker"
args = ["run", "-i", "--rm", "ghcr.io/pascalallen/pgmcp:1.1.0"]

[mcp_servers.pgmcp.env]
PGMCP_DATABASE_URL = "<YOUR_PGMCP_DATABASE_URL>"
```

### Gemini CLI

```sh
gemini mcp add -s user -e 'PGMCP_DATABASE_URL=<YOUR_PGMCP_DATABASE_URL>' -e 'PGMCP_DATABASE_URL=<YOUR_PGMCP_DATABASE_URL>' pgmcp docker -- run -i --rm ghcr.io/pascalallen/pgmcp:1.1.0
```

### Gemini settings.json (`~/.gemini/settings.json`)

```json
{
  "mcpServers": {
    "pgmcp": {
      "command": "docker",
      "args": [
        "run",
        "-i",
        "--rm",
        "ghcr.io/pascalallen/pgmcp:1.1.0"
      ],
      "env": {
        "PGMCP_DATABASE_URL": "<YOUR_PGMCP_DATABASE_URL>"
      }
    }
  }
}
```

## Environment variables

| Name | Required | Secret | Description |
|---|---|---|---|
| `PGMCP_DATABASE_URL` | yes | yes | Postgres connection string (read-only role recommended) |
| `PGMCP_DATABASE_URL` | yes | yes | Postgres connection string (read-only role recommended) |
| `PGMCP_API_KEYS` | no | no |  |
| `PGMCP_AUTH_MODE` | no | no |  |
| `PGMCP_TEST_DSN` | no | no |  |

## Tools (9)

- `top_queries`
- `explain`
- `index_health`
- `table_health`
- `lock_waits`
- `connections`
- `replication`
- `config_check`
- `query`

## FAQ

### Is this server safe to use on production databases?

Yes, it is read-only by construction using a dedicated database role with no write privileges, read-only transactions, and a SQL parser guard.

### What PostgreSQL versions are supported?

The server requires PostgreSQL 13 or later and has been tested against PostgreSQL 16.

### Can I restrict the SQL queries to specific schemas?

Yes, you can use the `--query-schemas` flag to restrict the query and explain tools to specific named schemas.

### How can I install it on Claude Desktop?

You can download the .mcpb bundle from the Releases page and open it, which will prompt for a connection string and handle the setup automatically.
