MCP PostgreSQL 服务器
模型-控制器-提供程序 (MCP) 服务器:
连接到 PostgreSQL 数据库
将表模式公开为资源
提供运行只读 SQL 查询的工具
包括常见数据分析任务的提示
特征
模式探索:浏览数据库模式、表和列
只读查询执行:安全地对数据库运行 SQL 查询
数据分析提示:针对常见分析任务的预建 SQL 模板
数据可视化:生成可视化数据
关系探索:可视化表关系和外键
API 文档:自动生成的 OpenAPI 规范
Related MCP server: PostgreSQL MCP Server
建筑学
此应用程序遵循模型-控制器-提供者(MCP)模式:
模型层:直接与数据库交互
提供层:业务逻辑和数据处理
控制器层:API 端点和请求处理
安全功能
只读查询验证
SQL注入保护
速率限制
参数化查询
身份验证支持
CORS 配置
安装
克隆存储库:
git clone <repository-url> cd mcp-postgres-server安装依赖项:
npm install根据
.env.template创建.env文件:cp .env.template .env使用您的 PostgreSQL 数据库凭证更新
.env文件。启动服务器:
npm start
配置
所有配置都通过环境变量进行管理:
服务器:端口、环境、CORS 设置
数据库:连接详细信息、池设置
安全性:JWT 设置、速率限制
查询:执行限制、结果大小限制
API 端点
架构端点
GET /api/schemas- 列出所有架构GET /api/schemas/:schema/tables- 列出模式中的表GET /api/schemas/:schema/tables/:table- 获取表架构详细信息GET /api/schemas/:schema/relationships- 获取表关系GET /api/structure- 获取完整的数据库结构GET /api/search?q=term- 搜索表和列
查询端点
POST /api/query- 执行 SQL 查询POST /api/query/explain- 获取查询执行计划GET /api/schemas/:schema/tables/:table/sample- 获取示例数据GET /api/schemas/:schema/tables/:table/stats- 获取表统计信息
分析提示端点
GET /api/prompts- 列出分析提示模板GET /api/prompts/:templateId- 获取提示模板详细信息POST /api/prompts/:templateId/generate- 从模板生成 SQLGET /api/schemas/:schema/tables/:table/analysis/suggest- 获取分析建议
示例查询
基本表查询
// API request
fetch('/api/query', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
sql: 'SELECT * FROM users LIMIT 10'
})
})
.then(response => response.json())
.then(data => console.log(data));使用分析提示
// Get suggested analysis for a table
fetch('/api/schemas/public/tables/orders/analysis/suggest')
.then(response => response.json())
.then(suggestions => {
// Use a suggestion
const suggestionId = suggestions.data[0].templateId;
const params = suggestions.data[0].params;
// Generate SQL from the template
return fetch(`/api/prompts/${suggestionId}/generate`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ params })
});
})
.then(response => response.json())
.then(data => {
// Execute the generated SQL
return fetch('/api/query', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ sql: data.data.sql })
});
})
.then(response => response.json())
.then(results => console.log(results));发展
以开发模式运行:
npm run dev运行测试:
npm testLint 代码:
npm run lint
执照
麻省理工学院
Appeared in Searches
- A database for searching medical and scientific research articles
- PostgreSQL MCP (Multi-Version Concurrency Control or specific usage)
- Information about PostgreSQL MCP (Managed Cloud Provider or other related concepts)
- Search for information about 'rag'
- Overview and Resources for PostgreSQL Database System