
Postgres MCP (Research Project)

2025.04.17
5
Go数据库服务PostgreSQL 接口数据库
Postgres MCP 是一个为 PostgreSQL 数据库实现的 Model Control Protocol (MCP) 服务器。该项目提供了一个简单的 HTTP 接口,通过 MCP 与 PostgreSQL 数据库进行交互,允许执行只读查询和浏览数据库资源。
View on GitHub
Overview
基本能力
产品定位
Postgres MCP 是一个为 PostgreSQL 数据库实现的 MCP 服务器,提供 HTTP 接口进行数据库交互。
核心功能
- MCP 兼容的 HTTP 服务器
- 只读 SQL 查询执行
- 数据库资源列表
- 通过
.env
文件支持环境变量 - 使用 Go 和 Gin 框架构建
适用场景
- 需要远程访问 PostgreSQL 数据库的场景
- 需要执行只读查询的场景
- 需要浏览数据库资源的场景
工具列表
postgres-mcp
:主服务工具,提供 HTTP 接口与 PostgreSQL 数据库交互。
常见问题解答
- 无
使用教程
使用依赖
- Go 1.23.6 或更高版本
- PostgreSQL 数据库
- Git
安装教程
git clone https://github.com/ipfans/postgres-mcp.git
cd postgres-mcp
go mod download
配置
可以通过以下两种方式配置数据库连接: 1. 使用命令行参数:
go run cmd/postgres-mcp/main.go -db "postgres://user:password@localhost:5432/dbname"
- 使用环境变量:
- 在项目根目录创建
.env
文件 - 添加数据库 URL:
DATABASE_URL=postgres://user:password@localhost:5432/dbname
调试方式
- 启动服务器:
go run cmd/postgres-mcp/main.go
服务器默认在 8080 端口启动。
- 与 MCP 端点交互:
- 列出数据库资源: ``` POST http://localhost:8080/mcp Content-Type: application/json
{
"type": "function",
"name": "resources"
}
- 执行只读查询:
POST http://localhost:8080/mcp
Content-Type: application/json
{ "type": "function", "name": "query", "arguments": { "query": "SELECT * FROM your_table LIMIT 10" } } ```