
一个MCP服务器,用于查询任何自然语言的Postgres数据库。
概览
PGMCP通过自然语言查询连接AI助手。用通俗易懂的英语提问,获得结构化的SQL结果,配备自动流和强大的错误处理。 : 光标、Claude Desktop、VS Code 扩展以及任何兼容 MCP 的客户端 PGMCP通过自然语言查询连接并使其能够被AI助手访问。 - PostgreSQL数据库(带有您的模式的现有数据库)- OpenAI API密钥(可选,用于AI驱动的SQL生成) - : 用通俗英语提问 - : 自动处理大型结果集 - : 防止任何写入操作 - : 跨所有文本列搜索 - : 表格、JSON 和 CSV - : 正确处理混合大小写表名称 - : 支持任何 PostgreSQL 数据库 - DATABASE_URL:与现有数据库的PostgreSQL连接字符串 - OPENAI_API_KEY:用于生成AI驱动SQL的OpenAI API密钥- OPENAI_MODEL:用于使用的模型(默认:“gpt-4o-mini”)- HTTP_ADDR:服务器地址(默认:“:8080”)- HTTP_PATH:MCP端点路径......
概述(AI 翻译)
PGMCP通过自然语言查询将AI助手连接到任何PostgreSQL数据库。用通俗易懂的英语提问,获得结构化的SQL结果,配备自动流和稳健的错误处理。
**支持:光标、Claude桌面、VS Code扩展以及任何兼容MCP的客户端
快速启动
PGMCP连接您现有的PostgreSQL数据库,并通过自然语言查询让AI助手访问。
先决条件
- PostgreSQL数据库(包含您的模式的现有数据库)
- OpenAI API 密钥(可选,用于 AI 驱动的 SQL 生成)
基本用法
工作原理如下:
特色
- 自然语言到SQL:用通俗易懂的英语提问
- 自动流式传输:自动处理大型结果集
- 安全只读访问:防止任何写入操作
- 文本搜索:跨所有文本栏搜索
- 多种输出格式:表格、JSON 和 CSV
- PostgreSQL 大小写敏感性:正确处理混合大小写表名称
- 通用兼容性:支持任何PostgreSQL数据库
环境变量
必修:
DATABASE_URL:与现有数据库的PostgreSQL连接字符串
本页由 AI 翻译,技术细节请以原文为准。
README
以下为原文 README 节选,完整内容请查看原文。
PGMCP - PostgreSQL Model Context Protocol Server
PGMCP connects AI assistants to any PostgreSQL database through natural language queries. Ask questions in plain English and get structured SQL results with automatic streaming and robust error handling.
Works with: Cursor, Claude Desktop, VS Code extensions, and any MCP-compatible client
Quick Start
PGMCP connects to your existing PostgreSQL database and makes it accessible to AI assistants through natural language queries.
Prerequisites
- PostgreSQL database (existing database with your schema)
- OpenAI API key (optional, for AI-powered SQL generation)
Basic Usage
# Set up environment variables
export DATABASE_URL="postgres://user:password@localhost:5432/your-existing-db"
export OPENAI_API_KEY="your-api-key" # Optional
# Run server (using pre-compiled binary)
./pgmcp-server
# Test with client in another terminal
./pgmcp-client -ask "What tables do I have?" -format table
./pgmcp-client -ask "Who is the customer that has placed the most orders?" -format table
./pgmcp-client -search "john" -format table
Here is how it works:
👤 User / AI Assistant
│
│ "Who are the top customers?"
▼
┌─────────────────────────────────────────────────────────────┐
│ Any MCP Client │
│ │
│ PGMCP CLI │ Cursor │ Claude Desktop │ VS Code │ ... │
│ JSON/CSV │ Chat │ AI Assistant │ Editor │ │
└─────────────────────────────────────────────────────────────┘
│
│ Streamable HTTP / MCP Protocol
▼
┌─────────────────────────────────────────────────────────────┐
│ PGMCP Server │
│ │
│ 🔒 Security 🧠 AI Engine 🌊 Streaming │
│ • Input Valid • Schema Cache • Auto-Pagination │
│ • Audit Log • OpenAI API • Memory Management │
│ • SQL Guard • Error Recovery • Connection Pool │
└─────────────────────────────────────────────────────────────┘
│
│ Read-Only SQL Queries
▼
┌─────────────────────────────────────────────────────────────┐
│ Your PostgreSQL Database │
│ │
│ Any Schema: E-commerce, Analytics, CRM, etc. │
安装
docker run -e DATABASE_URL="postgres://user:pass@host:5432/db" \配置
{
"mcpServers": {
"pgmcp": {
"transport": {
"type": "http",
"url": "http://localhost:8080/mcp"
}
}
}
}