PostgreSQL数据库服务

一个为Claude Desktop提供PostgreSQL数据库访问的Model Context Protocol (MCP)服务器,支持查询执行、模式检查、CRUD操作和安全特性。

  • by chenz4027
  • TypeScript
  • 本地部署
  • 更新于
在 GitHub 查看 chenz4027/postgres-mcp

使用说明

PostgreSQL MCP服务器

一个模型上下文协议(MCP)服务器,提供PostgreSQL数据库对Claude Desktop的访问。此服务器使Claude能够通过安全、定义良好的接口与本地PostgreSQL数据库进行交互。

特性

  • 查询执行:执行带有参数化的自定义SQL查询以确保安全
  • 架构检查:列出表,描述表结构,并获取模式概述
  • CRUD操作:使用结构化接口创建、读取、更新和删除记录
  • 安全功能:输入验证、参数化查询和连接池
  • 类型安全:带Zod验证的完整TypeScript实现

可用工具

  1. postgresquery -执行自定义SQL查询
  2. postgres_list_tables -列出数据库中的所有表
  3. postgres_descripte_table -获取详细的表格信息
  4. postgres_schema_概述 -获取完整的数据库架构
  5. postgres_select -选择具有过滤和分页功能的数据
  6. postgres_insert -插入新记录
  7. postgres_update -更新现有记录
  8. postgres_delate -删除记录

安装

  1. 克隆此存储库:

    git clone <repository-url>
    cd postgres-mcp
    
  2. 安装依赖项:

    npm install
    
  3. 构建项目:

    npm run build
    

配置

PostgreSQL设置

确保你的PostgreSQL服务器在localhost:5432上运行,数据库名为 postgres。默认配置假定您可以在没有用户名/密码的情况下连接(信任身份验证)。

Claude桌面配置

将以下内容添加到您的Claude Desktop配置文件中:

macOS: ~/Library/Application Support/Claude/claude_desktop_config.json 视窗: %APPDATA%\\Claude\\claude_desktop_config.json

{
  "mcpServers": {
    "postgres": {
      "command": "node",
      "args": ["/path/to/postgres-mcp/dist/index.js"]
    }
  }
}

替换 /path/to/postgres-mcp 带有项目目录的实际路径。

用法

  1. 启动克劳德桌面
  2. PostgreSQL MCP服务器将自动连接到您的数据库
  3. 通过Claude的界面使用可用的工具

查询示例

  • “显示数据库中的所有表”
  • “描述用户表”
  • “选择年龄>25岁的所有用户”
  • “插入一个名为“John”、电子邮件为“”的新用户john@example.com'"

安全考虑

  • 所有查询都使用参数化语句来防止SQL注入
  • 使用Zod模式进行输入验证
  • 连接池超时
  • 无硬编码凭据(使用环境/默认连接)

发展

# Development mode (watch for changes)
npm run dev

# Build
npm run build

# Lint
npm run lint

# Test
npm test

故障排除

  1. 连接问题:确保PostgreSQL正在运行且可访问
  2. 权限错误:检查数据库用户权限
  3. 未找到工具:验证Claude Desktop配置路径

许可证

麻省理工学院