PostgreSQL MCP Server

PostgreSQL MCP Server

🚀 PostgreSQL MCP 服务器

PostgreSQL MCP 服务器是一个基于 Model Context Protocol (MCP) 的服务器,它提供了强大的 PostgreSQL 数据库管理功能。该服务器能够分析现有的 PostgreSQL 设置,为用户提供实现指导,还能对数据库问题进行调试,极大地提升了数据库管理的效率和便捷性。

smithery 徽章

🚀 快速开始

PostgreSQL MCP 服务器提供了一系列强大的功能,帮助你更好地管理和维护 PostgreSQL 数据库。在使用之前,请确保满足相应的先决条件,并按照安装步骤进行操作。

✨ 主要特性

1. 数据库分析 (analyze_database)

该功能可对 PostgreSQL 数据库的配置和性能指标进行全面分析,具体包括:

  • 配置分析:深入了解数据库的各项配置参数。
  • 性能指标:评估数据库的性能表现。
  • 安全评估:检查数据库的安全设置。
  • 优化建议:根据分析结果给出优化建议。
// 示例用法
{
"connectionString": "postgresql://user:password@localhost:5432/dbname",
"analysisType": "performance" // 可选: "configuration" | "performance" | "security"
}

2. 安装说明 (get_setup_instructions)

提供 PostgreSQL 安装和配置的详细分步指导,涵盖:

  • 平台特定安装步骤:针对不同操作系统提供相应的安装方法。
  • 配置推荐:给出合理的配置建议。
  • 安全最佳实践:确保数据库的安全设置。
  • 安装后任务:指导安装完成后的必要操作。
// 示例用法
{
"platform": "linux", // 必填: "linux" | "macos" | "windows"
"version": "15", // 可选:PostgreSQL 版本
"useCase": "production" // 可选: "development" | "production"
}

3. 数据库调试 (debug_database)

用于调试常见的 PostgreSQL 问题,如:

  • 连接问题:解决数据库连接方面的故障。
  • 性能瓶颈:找出影响数据库性能的因素。
  • 锁冲突:处理锁冲突问题。
  • 复制状态:检查数据库复制状态。
// 示例用法
{
"connectionString": "postgresql://user:password@localhost:5432/dbname",
"issue": "performance", // 必填: "connection" | "performance" | "locks" | "replication"
"logLevel": "debug" // 可选: "info" | "debug" | "trace"
}

📦 安装指南

先决条件

  • Node.js >= 18.0.0
  • PostgreSQL 服务器(用于目标数据库操作)
  • 具备对目标 PostgreSQL 实例的网络访问权限

通过 Smithery 安装

要通过 Smithery 自动安装,可执行以下命令:

npm install -g @nahmanmate/postgresql-mcp-server

手动安装

克隆仓库并运行以下命令:

git clone https://github.com/nahmanmate/postgresql-mcp-server.git
cd postgresql-mcp-server
npm install
npm start

💻 使用示例

基础用法

try {
const result = await postgresqlMCPService.analyzeDatabase({
connectionString: "postgresql://user:password@localhost:5432/dbname",
analysisType: "performance"
});
console.log(result);
} catch (error) {
console.error("发生错误:", error.message);
}

高级用法

在开发环境中使用:

git clone https://github.com/nahmanmate/postgresql-mcp-server.git
cd postgresql-mcp-server
npm install
npm run dev

🔧 技术细节

安全考虑

  • 务必确保 PostgreSQL 服务器的安全配置,使用强密码和访问控制,防止未经授权的访问。
  • 定期备份数据库,以防止数据丢失。
  • 持续监控数据库的性能和错误,及时发现并解决潜在问题。

最佳实践

  • 使用连接池管理数据库连接,提高数据库的访问效率。
  • 配置适当的查询超时,避免长时间运行的查询占用资源。
  • 使用基于角色的访问控制,确保不同用户具有不同的权限。
  • 启用日志记录和监控,方便对数据库进行管理和维护。

📄 许可证

MIT License

Copyright (c) 2024 nahmanmate

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

🤝 贡献指南

如果你想为该项目贡献代码,请先 fork 仓库,然后在主分支上提交你的更改。

  • 0 关注
  • 0 收藏,11 浏览
  • system 提出于 2025-09-24 13:30

相似服务问题