# Postgres-MCP 工具
## 基本信息
- Slug: `crystaldba-postgres-mcp`
- Source: modelscope
- Publisher: @crystaldba/postgres-mcp
- Categories: databases / developer-tools
- Hosted: No
- License: MIT License
- Source URL: https://www.modelscope.cn/mcp/servers/@crystaldba/postgres-mcp
## 简介
Postgres Pro 是一个开源的模型上下文协议（MCP）服务器，旨在支持你和你的人工智能代理在整个开发过程中——从最初的编码，到测试和部署，再到生产调优和维护。
## 安装提示

```bash
docker pull crystaldba/postgres-mcp
```

## MCP Server 详情

<div align="center">

<img src="assets/postgres-mcp-pro.png" alt="Postgres MCP Pro Logo" width="600"/>

[![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](https://opensource.org/licenses/MIT)
[![PyPI - Version](https://img.shields.io/pypi/v/postgres-mcp)](https://pypi.org/project/postgres-mcp/)
[![Discord](https://img.shields.io/discord/1336769798603931789?label=Discord)](https://discord.gg/4BEHC7ZM)
[![Twitter Follow](https://img.shields.io/twitter/follow/auto_dba?style=flat)](https://x.com/auto_dba)
[![Contributors](https://img.shields.io/github/contributors/crystaldba/postgres-mcp)](https://github.com/crystaldba/postgres-mcp/graphs/contributors)

<h3>一个带有索引调优、执行计划、健康检查和安全SQL执行的Postgres MCP服务器。</h3>

<div class="toc">
  <a href="#overview">概述</a> •
  <a href="#demo">演示</a> •
  <a href="#quick-start">快速开始</a> •
  <a href="#technical-notes">技术说明</a> •
  <a href="#mcp-server-api">MCP API</a> •
  <a href="#related-projects">相关项目</a> •
  <a href="#frequently-asked-questions">常见问题</a>
</div>

</div>

## 概述

**Postgres MCP Pro** 是一个开源的模型上下文协议（MCP）服务器，旨在支持您和您的AI代理在整个开发过程中——从初始编码、测试和部署，到生产调优和维护。

Postgres MCP Pro 不仅仅是封装了一个数据库连接。

功能包括：

- **🔍 数据库健康** - 分析索引健康、连接利用率、缓冲缓存、vacuum健康、序列限制、复制延迟等。
- **⚡ 索引调优** - 使用工业级算法探索数千种可能的索引来找到最适合您工作负载的解决方案。
- **📈 查询计划** - 通过审查EXPLAIN计划并模拟假设索引的影响来验证和优化性能。
- **🧠 模式智能** - 基于对数据库模式的详细理解进行上下文感知的SQL生成。
- **🛡️ 安全SQL执行** - 可配置的访问控制，包括支持只读模式和安全SQL解析，使其既可用于开发也可用于生产。

Postgres MCP Pro 支持 [标准输入/输出 (stdio)](https://modelcontextprotocol.io/docs/concepts/transports#standard-input%2Foutput-stdio) 和 [服务器发送事件 (SSE)](https://modelcontextprotocol.io/docs/concepts/transports#server-sent-events-sse) 传输方式，以适应不同的环境。

有关我们为什么构建Postgres MCP Pro的更多背景，请参阅 [我们的发布博客文章](https://www.crystaldba.ai/blog/post/announcing-postgres-mcp-server-pro)。

## 演示

*从不可用到闪电般快速*
- **挑战：** 我们使用AI助手生成了一个电影应用，但SQLAlchemy ORM代码运行得非常慢。
- **解决方案：** 使用Postgres MCP Pro与Cursor，我们在几分钟内解决了性能问题。

我们做了什么：
- 🚀 解决了性能问题 - 包括ORM查询、索引和缓存
- 🛠️ 修复了一个损坏的页面 - 通过提示代理探索数据、修复查询并添加相关内容。
- 🧠 改进了顶级电影 - 通过探索数据并修复ORM查询以呈现更相关的结果。

请观看下方视频或阅读 [逐个步骤](examples/movie-app.md)。

## 快速开始

### 前提条件

在开始之前，请确保您具备以下条件：
1. 数据库的访问凭证。
2. Docker *或* Python 3.12 或更高版本。

#### 访问凭证
您可以使用 `psql` 或图形用户界面工具（例如 [pgAdmin](https://www.pgadmin.org/)）来确认您的访问凭证是否有效。

#### Docker 或 Python

选择使用 Docker 还是 Python 取决于您自己。
我们通常推荐使用 Docker，因为使用 Python 的用户可能会遇到更多与环境相关的问题。
然而，通常来说，使用您最熟悉的方法会更有意义。

### 安装

请选择以下方法之一来安装 Postgres MCP Pro：

#### 方法 1：使用 Docker

拉取 Postgres MCP Pro MCP 服务器的 Docker 镜像。
该镜像包含了所有必要的依赖项，提供了一种在各种环境中可靠运行 Postgres MCP Pro 的方式。

```bash
docker pull crystaldba/postgres-mcp
```


#### 方法 2：使用 Python

如果您已经安装了 `pipx`，可以使用以下命令安装 Postgres MCP Pro：

```bash
pipx install postgres-mcp
```


否则，可以使用 `uv` 来安装 Postgres MCP Pro：

```bash
uv pip install postgres-mcp
```


如果您需要安装 `uv`，请参阅 [uv 安装说明](https://docs.astral.sh/uv/getting-started/installation/)。

### 配置您的 AI 助手

我们提供了配置 Claude Desktop 与 Postgres MCP Pro 的完整说明。
许多 MCP 客户端具有类似的配置文件，您可以根据所选客户端调整这些步骤。

#### Claude Desktop 配置

您需要编辑 Claude Desktop 的配置文件以添加 Postgres MCP Pro。
该文件的位置取决于您的操作系统：
- MacOS: `~/Library/Application Support/Claude/claude_desktop_config.json`
- Windows: `%APPDATA%/Claude/claude_desktop_config.json`

您也可以通过 Claude Desktop 中的 `Settings` 菜单项找到配置文件。

接下来，您将编辑配置文件中的 `mcpServers` 部分。

##### 如果您使用的是 Docker

```json
{
  "mcpServers": {
    "postgres": {
      "command": "docker",
      "args": [
        "run",
        "-i",
        "--rm",
        "-e",
        "DATABASE_URI",
        "crystaldba/postgres-mcp",
        "--access-mode=unrestricted"
      ],
      "env": {
        "DATABASE_URI": "postgresql://username:password@localhost:5432/dbname"
      }
    }
  }
}
```


Postgres MCP Pro Docker 镜像会自动将主机名 `localhost` 映射为容器内部可用的形式。

- MacOS/Windows: 自动使用 `host.docker.internal`
- Linux: 自动使用 `172.17.0.1` 或适当的主机地址


##### 如果您使用的是 `pipx`

```json
{
  "mcpServers": {
    "postgres": {
      "command": "postgres-mcp",
      "args": [
        "--access-mode=unrestricted"
      ],
      "env": {
        "DATABASE_URI": "postgresql://username:password@localhost:5432/dbname"
      }
    }
  }
}
```


##### 如果您使用的是 `uv`

```json
{
  "mcpServers": {
    "postgres": {
      "command": "uv",
      "args": [
        "run",
        "postgres-mcp",
        "--access-mode=unrestricted"
      ],
      "env": {
        "DATABASE_URI": "postgresql://username:password@localhost:5432/dbname"
      }
    }
  }
}
```


##### 连接 URI

将 `postgresql://...` 替换为您自己的 [Postgres 数据库连接 URI](https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNSTRING-URIS)。


##### 访问模式

Postgres MCP Pro 支持多种 *访问模式*，以便您可以控制 AI 代理对数据库执行的操作：
- **无限制模式**：允许完全读写访问以修改数据和架构。适用于开发环境。
- **限制模式**：仅限于只读事务，并对资源使用（目前仅指执行时间）施加限制。适用于生产环境。

要使用受限模式，请将上述配置示例中的 `--access-mode=unrestricted` 替换为 `--access-mode=restricted`。

#### 其他 MCP 客户端

许多 MCP 客户端的配置文件与 Claude Desktop 类似，您可以根据需要调整上面的示例以适应您选择的客户端。

- 如果您使用的是 Cursor，可以通过 `命令面板` 导航到 `Cursor 设置`，然后打开 `MCP` 选项卡来访问配置文件。
- 如果您使用的是 Windsurf，可以通过 `命令面板` 导航到 `打开 Windsurf 设置页面` 来访问配置文件。
- 如果您使用的是 Goose，请运行 `goose configure`，然后选择 `添加扩展`。

## SSE 传输

Postgres MCP Pro 支持 [SSE 传输](https://modelcontextprotocol.io/docs/concepts/transports#server-sent-events-sse)，这允许多个 MCP 客户端共享一个服务器，可能是远程服务器。要使用 SSE 传输，您需要使用 `--transport=sse` 选项启动服务器。

例如，使用 Docker 运行：

```bash
docker run -p 8000:8000 \
  -e DATABASE_URI=postgresql://username:password@localhost:5432/dbname \
  crystaldba/postgres-mcp --access-mode=unrestricted --transport=sse
```


然后更新您的 MCP 客户端配置以调用 MCP 服务器。例如，在 Cursor 的 `mcp.json` 或 Cline 的 `cline_mcp_settings.json` 中，您可以这样设置：

```json
{
    "mcpServers": {
        "postgres": {
            "type": "sse",
            "url": "http://localhost:8000/sse"
        }
    }
}
```


对于 Windsurf，`mcp_config.json` 中的格式略有不同：

```json
{
    "mcpServers": {
        "postgres": {
            "type": "sse",
            "serverUrl": "http://localhost:8000/sse"
        }
    }
}
```


## Postgres 扩展安装（可选）

为了启用索引调优和全面的性能分析，您需要在数据库上加载 `pg_statements` 和 `hypopg` 扩展。

- `pg_statements` 扩展允许 Postgres MC…

