使用说明

项目简介

Postgres-MCP 是一个模型上下文协议 (MCP) 服务器,旨在使大型语言模型 (LLM) 能够与 PostgreSQL 数据库交互。它允许 LLM 执行只读 SQL 查询并以结构化的 JSON 格式检索数据。此服务器专门设计用于与 MCP 客户端(如 Claude Desktop)配合使用,从而扩展其访问和利用 PostgreSQL 数据库数据的能力。

主要功能点

  • 只读 SQL 执行:在 PostgreSQL 数据库上执行 'SELECT' 查询,确保数据完整性和安全性。
  • 结构化 JSON 输出:以标准化的 JSON 格式返回查询结果,易于 LLM 消费。
  • 安全验证:实施检查以确保仅执行只读查询,防止未经授权的数据修改。
  • 简易安装:提供多种安装选项,包括使用 Smithery、NPX、安装脚本和手动设置。

安装步骤

选择以下安装方法之一:

  1. 使用 Smithery (推荐):

    npm install -g @smithery/cli
    smithery install postgres-mcp --client claude
  2. 使用 NPX:

    npx postgres-mcp "Your PostgreSQL Connection String"

    将 '"Your PostgreSQL Connection String"' 替换为您的实际 PostgreSQL 连接详细信息。

  3. 使用 PowerShell 安装脚本 (Windows):

    # 如果需要,先克隆仓库: git clone https://github.com/thromel/Postgres-MCP.git
    cd Postgres-MCP/src/PostgresMcp/PostgresMcp
    powershell -ExecutionPolicy Bypass -File .\install.ps1 "Your PostgreSQL Connection String"

    将 '"Your PostgreSQL Connection String"' 替换为您的实际 PostgreSQL 连接详细信息。

  4. 使用 Shell 脚本 (macOS/Linux):

    # 如果需要,先克隆仓库: git clone https://github.com/thromel/Postgres-MCP.git
    cd Postgres-MCP/src/PostgresMcp/PostgresMcp
    chmod +x install.sh
    ./install.sh "Your PostgreSQL Connection String"

    将 '"Your PostgreSQL Connection String"' 替换为您的实际 PostgreSQL 连接详细信息。

  5. 手动设置:

    # 如果需要,先克隆仓库: git clone https://github.com/thromel/Postgres-MCP.git
    cd Postgres-MCP/src/PostgresMcp/PostgresMcp
    dotnet build
    dotnet run

    在这种情况下,您需要手动将 MCP 服务器注册到您的 MCP 客户端(如 Claude Desktop)。

服务器配置 (针对 MCP 客户端)

安装完成后,MCP 服务器注册文件 'postgres-mcp.json' 会在 Claude Desktop 的 MCP 配置目录中创建。您通常不需要手动配置客户端的 command 和 args,因为安装脚本会自动处理注册。但是,如果需要手动注册或为了理解配置,配置可能如下所示(安装程序已自动处理):

{
  "server name": "postgres-mcp",
  "command": "指向您的启动脚本的路径 (例如,start-postgres-mcp.sh 或 start-postgres-mcp.ps1)",
  "args": []
}

注意: 'command' 将指向安装期间创建的启动脚本,'args' 对于此服务器通常为空。安装脚本会自动处理创建和放置此注册文件以供 Claude Desktop 使用。

基本使用方法

安装完成后并重启 Claude Desktop(或您的 MCP 客户端),您可以使用自然语言通过 Claude 与 PostgreSQL 数据库进行交互。例如:

  • "Show me the contents of the users table"
  • "List all tables in my PostgreSQL database"
  • "What columns are in the customers table?"
  • "Execute a SQL query on my PostgreSQL database: SELECT * FROM products WHERE price > 100"

信息

分类

数据库与文件