# PostgreSQL MCP Server

> PostgreSQL MCP Server is an open-source MCP server for databases. Query PostgreSQL, inspect schemas and read EXPLAIN plans, read-only by default. It runs locally on your machine and is started by your AI client.

- **Kind:** MCP server
- **Pricing:** Open source
- **Platforms:** CLI
- **Website:** https://yaw.sh/mcp-servers/postgres-mcp/
- **Repository:** https://github.com/yawlabs/postgres-mcp
- **MCP Registry name:** io.github.YawLabs/postgres-mcp
- **Categories:** databases, developer tools
- **Listing updated:** 2026-11-14

## About

Writes are off unless you set ALLOW_WRITES=1, so an agent cannot silently drop tables; the connection string goes in a DATABASE_URL variable and the page recommends a least-privileged role. The site argues that Anthropic's reference Postgres server was archived and has a documented stacked-query injection, and presents this one as written from scratch.

Beyond parameterised SQL it covers schema and view introspection, EXPLAIN with actual timings, and diagnostics that otherwise need hand-written pg_catalog joins: top queries from pg_stat_statements, unused indexes, sequential-scan tables, locks with blocking and blocked PIDs, and extension checks such as pgvector.

The page shows chained jobs, for example inspecting locks and cancelling the blocker, or ranking slow queries and then explaining the worst. MIT, from Yaw Labs, on npm.

## Features

- **Read-only default:** Writes require an explicit environment variable.
- **Schema introspection:** Describes columns, keys, constraints and indexes.
- **Plans with timings:** EXPLAIN with actual row counts and timing, plus index advice.
- **Performance diagnostics:** Top queries, unused indexes and seq-scan tables.
- **Lock inspection:** Shows blocked and blocking processes.
- **Extension checks:** Reports whether extensions like pgvector are installed.

## Good for

- Describe a table and its indexes
- Find why a query is slow
- Triage a hung app by locks
- Spot unused indexes before a cleanup
- Run oncall health checks

## Install

### Claude Code (Terminal)

```sh
claude mcp add postgres-mcp -- npx -y @yawlabs/postgres-mcp
```

### Claude Desktop (claude_desktop_config.json)

```json
{
  "mcpServers": {
    "postgres-mcp": {
      "command": "npx",
      "args": [
        "-y",
        "@yawlabs/postgres-mcp"
      ]
    }
  }
}
```

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

```json
{
  "mcpServers": {
    "postgres-mcp": {
      "command": "npx",
      "args": [
        "-y",
        "@yawlabs/postgres-mcp"
      ]
    }
  }
}
```

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

```json
{
  "servers": {
    "postgres-mcp": {
      "type": "stdio",
      "command": "npx",
      "args": [
        "-y",
        "@yawlabs/postgres-mcp"
      ]
    }
  }
}
```

### Windsurf (~/.codeium/windsurf/mcp_config.json)

```json
{
  "mcpServers": {
    "postgres-mcp": {
      "command": "npx",
      "args": [
        "-y",
        "@yawlabs/postgres-mcp"
      ]
    }
  }
}
```

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

```json
{
  "mcpServers": {
    "postgres-mcp": {
      "command": "npx",
      "args": [
        "-y",
        "@yawlabs/postgres-mcp"
      ]
    }
  }
}
```

## Tools

- `pg_query`: Run parameterised SQL.
- `pg_list_schemas`: List schemas.
- `pg_list_tables`: List tables.
- `pg_list_views`: List views.
- `pg_list_functions`: List functions.
- `pg_list_extensions`: List installed extensions.
- `pg_describe_table`: Describe a table with columns, keys, constraints and indexes.
- `pg_explain`: Show the plan for a query.
- `pg_index_advisor`: Suggest indexes.
- `pg_inspect_locks`: Show blocked and blocking processes.
- `pg_seq_scan_tables`: Find tables read with sequential scans.
- `pg_unused_indexes`: List non-unique, non-primary indexes with low scan counts.
- `pg_top_queries`: Show top queries from pg_stat_statements.

## FAQ

### Can the agent change data?

Not by default; add ALLOW_WRITES=1 to allow it.

### How do I connect?

Set DATABASE_URL in the MCP config env block.

### On Windows?

Wrap the command in cmd /c.

### Is PostgreSQL MCP Server free?

Yes. PostgreSQL MCP Server is open source, so you can use and self-host it for free.

### How do I add PostgreSQL MCP Server to Claude Code?

Run this command in your terminal: claude mcp add postgres-mcp -- npx -y @yawlabs/postgres-mcp

### What are the best alternatives to PostgreSQL MCP Server?

The closest alternatives to PostgreSQL MCP Server are REA, Codebase Memory, GitHub, Serena MCP: the IDE for your agent and XERJ.

## Alternatives

- [REA](https://ai-powered.com/mcp/rea): REA is an open-source MCP server for developer tools.
- [Codebase Memory](https://ai-powered.com/mcp/codebase-memory): Codebase Memory is an open-source MCP server for knowledge and memory.
- [GitHub](https://ai-powered.com/mcp/github-2): GitHub is an open-source MCP server for developer tools.
- [Serena MCP: the IDE for your agent](https://ai-powered.com/mcp/serena-mcp-the-ide-for-your-agent): Serena MCP: the IDE for your agent is an open-source MCP server for developer tools.
- [XERJ](https://ai-powered.com/mcp/xerj): XERJ is an open-source MCP server for knowledge and memory.
- [Ouroboros](https://ai-powered.com/mcp/ouroboros): Ouroboros is an open-source MCP server for developer tools.
- [Claude Code Ultimate Guide](https://ai-powered.com/mcp/claude-code-ultimate-guide): Claude Code Ultimate Guide is an open-source MCP server for ai models and llm tooling.
- [Concord MCP](https://ai-powered.com/mcp/concord-mcp): Concord MCP is a freemium MCP server for developer tools.

---
Source: https://ai-powered.com/mcp/postgresql-mcp-server-5
