使用说明
项目简介
PostgreSQL MCP Server 是一个基于 Model Context Protocol (MCP) 构建的后端服务,旨在为大型语言模型 (LLM) 应用提供 PostgreSQL 数据库管理能力。它通过标准化的 MCP 协议,使 LLM 能够分析数据库配置和性能,获取安装指导,并进行问题调试。
主要功能点
- 数据库分析 (analyze_database): 分析 PostgreSQL 数据库的配置、性能指标和安全性,并提供优化建议。例如,它可以检查数据库连接数、缓存命中率、表大小等关键指标,并根据分析结果给出改进配置的建议。
- 安装指导 (get_setup_instructions): 提供在 Linux、macOS 和 Windows 平台上安装和配置 PostgreSQL 的详细步骤和最佳实践。无论您使用哪个操作系统,都可以获得清晰的安装指令,并了解生产环境下的安全配置要点。
- 问题调试 (debug_database): 诊断和解决常见的 PostgreSQL 数据库问题,如连接问题、性能瓶颈、锁冲突和复制状态异常。当数据库出现问题时,此功能可以帮助快速定位问题根源,并提供相应的解决建议。
安装步骤
- 克隆仓库: 在您的本地计算机上克隆 GitHub 仓库:
git clone https://github.com/nahmanmate/postgresql-mcp-server.git - 进入仓库目录: 使用 'cd' 命令进入克隆下来的仓库目录:
cd postgresql-mcp-server - 安装依赖: 使用 npm 安装项目所需的依赖包:
npm install - 构建项目: 运行构建命令,将 TypeScript 代码编译为 JavaScript 代码:
npm run build
服务器配置
为了让 MCP 客户端能够连接到 PostgreSQL MCP Server,您需要在 MCP 客户端的配置文件中添加以下服务器配置信息。请注意,您需要将 'args' 字段中的路径替换为 'postgresql-mcp-server' 项目构建后 'index.js' 文件的实际路径。
{ "mcpServers": { "postgresql-mcp": { "command": "node", "args": ["/path/to/postgresql-mcp-server/build/index.js"], // 将 "/path/to/postgresql-mcp-server" 替换为 PostgreSQL MCP Server 的实际路径 "disabled": false, "alwaysAllow": [] } } }
基本使用方法
配置完成后,MCP 客户端可以通过 JSON-RPC 协议与 PostgreSQL MCP Server 进行通信。客户端可以发送请求调用服务器提供的工具,例如:
- 调用 'analyze_database' 工具分析数据库性能:
{ "jsonrpc": "2.0", "method": "call_tool", "params": { "name": "analyze_database", "arguments": { "connectionString": "postgresql://user:password@localhost:5432/dbname", "analysisType": "performance" } }, "id": 1 } - 调用 'get_setup_instructions' 工具获取 Linux 平台 PostgreSQL 安装指导:
{ "jsonrpc": "2.0", "method": "call_tool", "params": { "name": "get_setup_instructions", "arguments": { "platform": "linux", "version": "15", "useCase": "production" } }, "id": 2 } - 调用 'debug_database' 工具调试数据库连接问题:
{ "jsonrpc": "2.0", "method": "call_tool", "params": { "name": "debug_database", "arguments": { "connectionString": "postgresql://user:password@localhost:5432/dbname", "issue": "connection" } }, "id": 3 }
更多工具的详细使用方法和参数说明,请参考仓库的 'README.md' 文件以及代码中工具的定义。
信息
分类
数据库与文件