pg-mcp
Enables natural language querying of PostgreSQL databases, converting natural language to SQL, executing queries, and providing schema introspection and safety validation.
Click on "Install Server".
Wait a few minutes for the server to deploy. Once ready, it will show a "Started" state.
In the chat, type
@followed by the MCP server name and your instructions, e.g., "@pg-mcpshow me the total sales per product for the last quarter"
That's it! The server will respond to your query, and you can continue using it as needed.
Here is a step-by-step guide with screenshots.
pg-mcp: PostgreSQL MCP Server
基于自然语言的 PostgreSQL 查询 MCP 服务。接入 DeepSeek 大模型,自动将自然语言转化为 SQL 并执行。
快速开始
1. 安装
pip install -e .2. 配置
复制 .env.example 为 .env,填写以下必填项:
PG_MCP_DEEPSEEK_API_KEY=sk-your-key
PG_MCP_DATABASES='[{"name":"mydb","dsn":"postgresql://user:pass@localhost:5432/mydb","default":true}]'或单库模式:
PG_MCP_DEEPSEEK_API_KEY=sk-your-key
PG_MCP_DATABASE_URL=postgresql://user:pass@localhost:5432/mydb3. 启动
pg-mcp或
python -m pg_mcp.serverRelated MCP server: nl2sql-mcp
MCP Tool 列表
Tool | 功能 |
| 自然语言 → SQL / 查询结果(核心) |
| 自然语言解释 SQL 功能 |
| 手动刷新数据库 Schema 缓存 |
| 列出所有可访问数据库 |
MCP Client 配置
在 mcp.json 中添加:
{
"mcpServers": {
"pg-mcp": {
"command": "python",
"args": ["-m", "pg_mcp.server"],
"env": {
"PG_MCP_DEEPSEEK_API_KEY": "${DEEPSEEK_API_KEY}",
"PG_MCP_DATABASES": "[{\"name\":\"mydb\",\"dsn\":\"postgresql://user:pass@localhost:5432/mydb\",\"default\":true}]"
}
}
}
}安全
三重防线:
sqlglot AST 分析 — 自动拦截 DDL/DML/DCL 及危险表引用
正则兜底 — 处理解析器无法识别的边界情况
数据库只读用户 — 从数据库层面杜绝写入
Fail-Closed 原则:任何 SQL 解析失败、异常 → 一律拒绝执行,绝不默认放行。
开发
# 安装开发依赖
pip install -e ".[dev]"
# 运行测试
pytest tests/ -v
pytest tests/test_sql_validator.py -v # 安全校验(最重要)
# 跳过集成测试
pytest -m "not integration" -v技术栈
FastMCP 3.4.x — MCP Server 框架
asyncpg — PostgreSQL 异步驱动
sqlglot — SQL 解析与安全校验
DeepSeek (openai SDK) — LLM 生成 SQL
pydantic-settings — 配置管理
structlog — 结构化日志
This server cannot be installed
Maintenance
Resources
Unclaimed servers have limited discoverability.
Looking for Admin?
If you are the server author, to access and configure the admin panel.
Related MCP Servers
- AlicenseAqualityAmaintenanceAn MCP server that gives AI agents direct read-only access to PostgreSQL databases, enabling natural language analytics through tools for schema exploration, querying, trend analysis, and data quality checks.Last updated115MIT
- Flicense-qualityFmaintenanceA production-ready MCP server that transforms natural language into safe, executable SQL queries with multi-database support and intelligent schema analysis.Last updated1
- Alicense-qualityBmaintenanceA read-only MCP server for PostgreSQL that enables safe database introspection and querying via natural language.Last updated576MIT
- Flicense-qualityDmaintenanceAn MCP server that transforms natural language questions into SQL, executes queries on PostgreSQL, and returns human-friendly responses, with Redis memory for context.Last updated1
Related MCP Connectors
GibsonAI MCP server: manage your databases with natural language
Query PostgreSQL databases in plain English — LLM-generated, safety-validated SQL.
Analytical memory for AI agents: a real Postgres queried in plain English over MCP. One command.
Latest Blog Posts
- Who's Calling? MCP Hosts Are an Identity Blind Spot (And the Spec Knows It)By Om-Shree-0709 on .mcpAgent IdentityOAuth 2.1
- Your AI Chatbot Just Exposed Your CEO's Salary to an InternBy Om-Shree-0709 on .Agent IdentityMCP SecurityOAuth Delegation
- Why MCP Servers Need Execution Sandboxing (And Why Your Current Stack Isn't Enough)By Om-Shree-0709 on .Agentic AiPrompt InjectionWebAssembly
MCP directory API
We provide all the information about MCP servers via our MCP API.
curl -X GET 'https://glama.ai/api/mcp/v1/servers/txy670584307/pg-mcp'
If you have feedback or need assistance with the MCP directory API, please join our Discord server