Postgres MCP
io.github.edelciomolina由 GitHub 账号 edelciomolina 发布v1.7.0更新于 Oct 25, 2026
PostgreSQL MCP wrapper with .env credential mapping, tool selection, and safe read-only defaults.
概览
一个本地 MCP 服务器,为 AI 助手提供结构化、默认只读的 PostgreSQL 数据库接口,包括模式查看和查询风险判断。
- 可以做什么
- 执行受保护的只读查询(SELECT、COUNT、EXISTS),并查看 EXPLAIN 计划和基于 pg_stat_statements 的慢查询统计。查看模式与 ENUM 类型,获取各平台的配置说明。分析性能、配置和存储,监控连接、锁、查询和复制状态。构建包含模式、表、列、外键和推断关系的数据库知识图谱,描述表的语义与风险,并查找相关表。在不执行的情况下判断查询的风险等级。可选启用写入工具,用于模式、索引、约束、函数、触发器、RLS、用户、数据变更和任意 SQL。
- 适用场景
- 当助手需要安全地浏览或查询 PostgreSQL 数据库、在写查询前了解其结构,或诊断性能与锁问题时使用。适合希望默认只读、对任何修改操作显式开启的团队。
- 运行要求
- 需要 Node.js 18 或更高版本,通过 npx 在本地运行或安装该 npm 包。需要一个包含数据库凭据的 .env 文件,从工作目录向上查找,或用 env-file 参数指定。env 块将 MCP_KEY_HOST、MCP_KEY_PORT、MCP_KEY_NAME、MCP_KEY_SSLMODE、MCP_KEY_USER 和 MCP_KEY_PASS 映射到该文件中的变量名,或用 MCP_KEY_URL 映射连接字符串。写入工具还需设置 POSTGRES_MCP_ALLOW_WRITE=true 并显式传入 tool= 参数。
安装
在 SourceWeft 中
- 打开 控制台中的 Postgres MCP,将其添加到工作区。
- 为需要使用其工具的对话启用该服务。
Desktop only,通过 STDIO。 STDIO 服务会启动本地进程,因此需要 SourceWeft 桌面宿主。
其他 MCP 客户端
参照 仓库 中的启动说明。
README
English | 🌐 Português
✨ What it does
Most LLMs interact with databases by guessing - assuming table names, inventing column names, and writing queries that may fail or expose sensitive data. Postgres MCP solves this by giving the LLM a structured, safe interface to actually understand the database before touching it.
Built with @modelcontextprotocol/sdk and pg, it provides:
- 🧠 Semantic knowledge graph - the LLM gets a complete map of schemas, tables, columns, foreign keys, inferred relations, risk levels, and business domains - built from the real schema, not invented
- 🛡️ Read-only by default - no writes, no DDL, no arbitrary SQL unless you explicitly opt in;
pg_classify_query_risklets the LLM check a query's safety before running it - 🔐 Runtime credential resolution - credentials are read from
.envat startup; nothing sensitive lives inmcp.json - 🎯 Explicit tool selection - every tool is opt-in via
tool=<name>args, so the LLM only sees what you choose to expose
📋 Requirements
- ⚙️ Node.js >= 18
- 📄 A
.envfile with database credentials (anywhere in the project tree - see .env Discovery)
🚀 Installation
There are two ways to use this package. Choose the one that best fits your workflow.
Option 1 - No installation (via npx, recommended for quick start)
No installation needed. npx downloads and runs the package on demand. Add -y as the first argument to skip the confirmation prompt.
This starts the server with the default read-only tool set - no tool= arguments needed. To enable write-capable tools, see Write-capable tools.
💡 Using Supabase, Neon, Railway or another platform that only provides a connection string? Use
MCP_KEY_URLpointing toDATABASE_URL(or whatever variable name the platform uses). The server will prioritize the URL and ignore the individual variables. See Connection via URL.
Option 2 - Install via VS Code (MCP extension marketplace)
VS Code supports discovering and installing MCP servers directly in the editor, without using the terminal.
- Open the Command Palette (Cmd+Shift+P on Mac / Ctrl+Shift+P on Windows/Linux)
- Run
MCP: Add Server - Choose "Browse MCP Servers" (or "From registry", depending on your VS Code version)
- Search for
postgres-mcporedelciomolina - Select Postgres MCP and follow the instructions - VS Code will add the entry to your
mcp.jsonautomatically
💡 You can also open the MCP Servers panel via Copilot chat icon → Manage MCP Servers to browse, enable, or disable servers at any time.
After installing, edit the generated entry in .vscode/mcp.json to add your tool= arguments and env key mappings as shown in the Usage section below.
🚀 Usage in VS Code (mcp.json)
Read-only (default - no tool= arguments needed):
With write tools (explicit opt-in required):
⚠️ Write-capable tools require
POSTGRES_MCP_ALLOW_WRITE=trueinenv. Without it, the server exits at startup.
The corresponding .env file at the root of your project:
⚙️ How mcp.json configuration works
🗝️ env - credential key mapping
The env block does not contain the actual credentials. It maps each MCP_KEY_* to the variable name in your .env file.
Priority: when
MCP_KEY_URL(orDATABASE_URL) is present, the server uses the URL directly and ignores the individual credential keys.
This indirection lets you use any variable name in your .env - useful when sharing a .env across multiple services with different naming conventions.
🔧 args - tool selection via tool= prefix
Each enabled MCP tool is declared as a separate argument in the format tool=<name>:
This makes the tool list explicit and auditable directly in mcp.json - no hidden configuration files. 🔍
🔗 Connection via URL (DATABASE_URL)
In addition to individual credentials, you can provide a full connection string - the standard format on platforms like Supabase, Neon, and Railway.
.env:
mcp.json:
The variable mapped by MCP_KEY_URL has priority over the other keys (MCP_KEY_HOST, MCP_KEY_PORT, etc.). If the URL is present, the other variables are ignored.
If the platform uses a different name (e.g. DB_URL), just adjust the mapping:
🛡️ Why read-only is the default
If you omit all tool= arguments, the server starts with a curated read-only set - all tools that can retrieve, analyze, or explain data, but nothing that can modify it.
✅ Included in defaults (read-only):
💡
pg_execute_queryrejectsINSERT,UPDATE,DELETE, DDL,ANALYZE,VACUUM,EXPLAIN ANALYZEand other write/maintenance commands before the database is queried.
💡
pg_inspect_schemaprovides read-only schema introspection (get_info,get_enums). For DDL operations, usepg_manage_schemawith explicit opt-in.
⚠️ Excluded from defaults - require tool= argument AND POSTGRES_MCP_ALLOW_WRITE=true:
📍 .env file discovery
The server resolves the .env file in this order:
env-file=<path>argument - explicit path relative tocwd; takes priority over everything- Upward search - starting from
cwd, searches each parent directory until a.envis found or the filesystem root is reached
If no .env is found, the server exits with a clear error message.
Monorepos and subfolders
When VS Code starts the MCP process, cwd is typically the workspace root. If your .env is in a subfolder (e.g. functions/.env), use env-file= to point to it explicitly:
💡 The upward search behavior handles the common case automatically. Use
env-file=when you need explicit control (CI, monorepos, Docker bind-mounts).
🧰 Available tools
Read-only (enabled by default)
Write-capable (opt-in via tool= argument + POSTGRES_MCP_ALLOW_WRITE=true)
🧠 Semantic Layer
The four pg_*_graph / pg_*_semantics / pg_*_risk tools build an in-memory knowledge graph of your database at runtime. This gives the LLM a structured map - schemas, tables, columns, foreign keys, inferred relations, risk levels, and business domains - without executing any query against your data.
All inferred fields (column semantic roles, table probable types, inferred relations) are clearly tagged so the LLM knows to treat them as hints, not schema facts.
Optional configuration (mcp-config.json)
Place a mcp-config.json file beside your .env to tune the semantic layer and security limits. All fields are optional - omitting the file applies safe defaults.
🏗️ Architecture
For a detailed view of the communication flow between the MCP client, the proxy, and PostgreSQL - including the full sequence diagram - see ARCHITECT.md.
📄 License
MIT © Edelcio Molina
来源:README.md,提交 6e2fb2e
工具
0版本历史
1- v1.7.0最新Sep 16, 2026
评分
已满足适用检查项的 40%
该评分衡量此收录条目的完整程度,依据注册中心条目、端点与公开来源计算得出。它不是对服务器本身的评价,也不表示服务器安全或当前可用:可连通性是带日期的检查结果,评分也不包含安装量或用户评价。
- 近 30 天内可连通25 分
请保持端点能够响应 MCP 握手;SourceWeft 每 30 天探测一次。
- 已声明认证方式10 分
请在注册中心条目中声明凭据请求头,或保持端点能够响应探测,以便判定其认证方式。
- 已列出工具15 分
请让服务器在无凭据时响应 tools/list,或发布可在沙箱中无凭据启动的软件包。
- 描述必需15 分
- 图标10 分
- 文档15 分
- 许可证10 分
请在仓库或注册中心条目中声明许可证。
