About Postgresql Mcp Ddz
Postgresql Mcp Ddz is an MCP server for interacting with PostgreSQL databases. It supports parameterized queries and data changes, alongside schema inspection and query-plan analysis.
Key features
- 01Run parameterized SELECT queries
- 02Insert, update, and delete rows
- 03List tables and inspect schemas
- 04Analyze queries with EXPLAIN
- 05Connect using environment variables
Use cases
- 01Query PostgreSQL data from an AI assistant
- 02Modify rows with parameterized SQL
- 03Inspect table structure and keys
- 04Review query plans for performance
MCP support
Exposes PostgreSQL connection, query, execution, schema-inspection, and analysis tools to MCP clients.
Configuration
Local server config is the same for every client. Copy it into your client’s MCP config file.
npx -y postgresql-mcp-ddzPOSTGRES_HOSTPOSTGRES_PORTPOSTGRES_USERPOSTGRES_PASSWORDSecretPOSTGRES_DBLocal servers need a runtime on your machine (e.g. Node.js 18+). Config file locations differ by client; see your client’s docs.
Tools
7Saved tool list · updated 3w ago
| Parameter | Type | Required | Description |
|---|---|---|---|
| host | string | Yes | Database host |
| port | number | No | Database port (default: 5432) |
| user | string | Yes | Database user |
| password | string | Yes | Database password |
| database | string | Yes | Database name |
Details
From the project READMEPostgreSQL MCP Server
一个基于模型上下文协议(MCP)的 PostgreSQL 数据库操作服务器,让 AI 助手能够通过标准化接口与 PostgreSQL 数据库交互。
功能特性
- 查询执行:执行带参数化的 SELECT 查询
- 数据修改:执行 INSERT、UPDATE、DELETE 操作
- 结构查询:列出数据库表、查看表结构
- 性能分析:使用 EXPLAIN 分析查询性能
- 连接管理:支持环境变量自动连接,安全可靠
安装
npm install postgresql-mcp-ddz
或直接使用 npx:
npx postgresql-mcp-ddz
配置
方式一:环境变量自动连接(推荐)
在 MCP 客户端配置中添加环境变量,服务器启动时会自动连接数据库:
{
"mcpServers": {
"postgresql-mcp-ddz": {
"command": "npx",
"args": ["postgresql-mcp-ddz"],
"env": {
"POSTGRES_HOST": "127.0.0.1",
"POSTGRES_PORT": "5432",
"POSTGRES_USER": "postgres",
"POSTGRES_PASSWORD": "your_password",
"POSTGRES_DB": "your_database"
}
}
}
}
支持的环境变量:
| 变量名 | 说明 | 必填 |
|---|---|---|
POSTGRES_HOST | 数据库主机地址 | 是 |
POSTGRES_PORT | 数据库端口(默认 5432) | 否 |
POSTGRES_USER | 数据库用户名 | 是 |
POSTGRES_PASSWORD | 数据库密码 | 是 |
POSTGRES_DB | 数据库名称 | 是 |
方式二:手动连接
不在配置中添加环境变量时,需要通过 connect_db 工具手动连接:
{
"mcpServers": {
"postgresql": {
"command": "npx",
"args": ["postgresql-mcp-ddz"]
}
}
}
可用工具
connect_db
连接到 PostgreSQL 数据库(环境变量配置后可省略此步骤)。
参数:
host(字符串,必填):数据库主机地址port(数字,可选):数据库端口,默认 5432user(字符串,必填):数据库用户名password(字符串,必填):数据库密码database(字符串,必填):数据库名称
示例:
{
"host": "localhost",
"port": 5432,
"user": "postgres",
"password": "password",
"database": "mydb"
}
query
执行 SELECT 查询并返回结果。
参数:
sql(字符串,必填):SQL SELECT 查询语句params(数组,可选):查询参数
示例:
{
"sql": "SELECT * FROM users WHERE age > $1",
"params": [18]
}
execute
执行 INSERT、UPDATE 或 DELETE 查询。
参数:
sql(字符串,必填):SQL 语句params(数组,可选):查询参数
示例:
{
"sql": "INSERT INTO users (name, email) VALUES ($1, $2)",
"params": ["张三", "[email protected]"]
}
list_tables
列出当前数据库中的所有表。
describe_table
获取指定表的结构信息,包括列、主键和外键。
参数:
table(字符串,必填):表名
示例:
{
"table": "users"
}
explain
使用 EXPLAIN 分析 SQL 查询性能。
参数:
sql(字符串,必填):要分析的 SQL 查询
示例:
{
"sql": "SELECT * FROM users WHERE email = '[email protected]'"
}
show_statement
执行 SHOW 语句。
参数:
sql(字符串,必填):SHOW SQL 语句
示例:
{
"sql": "SHOW ALL"
}
安全说明
query工具仅允许 SELECT、SHOW、EXPLAIN 和 WITH 查询- 数据修改操作请使用
execute工具 - 所有查询支持参数化输入,防止 SQL 注入
- 数据库凭据仅在运行时传递,不会被持久化存储
Comments
New to MCP?
From what the protocol is to installing your first server, these answers cover most first questions.
An open protocol that defines how AI applications connect to external tools and data in a uniform way. Clients talk to the model; MCP servers provide tools, resources and prompt templates. They exchange standard messages, so one server can be reused by many clients.