MCP PostgreSQL Server
MCP PostgreSQL 服务器
提供 PostgreSQL 数据库操作的模型上下文协议服务器。该服务器使 AI 模型能够通过标准化接口与 PostgreSQL 数据库进行交互。
安装
手动安装
npm install mcp-postgres-server或者直接运行:
npx mcp-postgres-serverRelated MCP server: postgresql-mcp-server
配置
服务器需要以下环境变量:
{
"mcpServers": {
"postgres": {
"type": "stdio",
"command": "npx",
"args": ["-y", "mcp-postgres-server"],
"env": {
"PG_HOST": "your_host",
"PG_PORT": "5432",
"PG_USER": "your_user",
"PG_PASSWORD": "your_password",
"PG_DATABASE": "your_database"
}
}
}
}可用工具
1. 连接数据库
使用提供的凭据建立与 PostgreSQL 数据库的连接。
use_mcp_tool({
server_name: "postgres",
tool_name: "connect_db",
arguments: {
host: "localhost",
port: 5432,
user: "your_user",
password: "your_password",
database: "your_database"
}
});2. 查询
使用可选的预处理语句参数执行 SELECT 查询。支持 PostgreSQL 风格的 ($1, $2) 和 MySQL 风格的 (?) 参数占位符。
use_mcp_tool({
server_name: "postgres",
tool_name: "query",
arguments: {
sql: "SELECT * FROM users WHERE id = $1",
params: [1]
}
});3. 执行
使用可选的预处理语句参数执行 INSERT、UPDATE 或 DELETE 查询。支持 PostgreSQL 风格的 ($1、$2) 和 MySQL 风格的 (?) 参数占位符。
use_mcp_tool({
server_name: "postgres",
tool_name: "execute",
arguments: {
sql: "INSERT INTO users (name, email) VALUES ($1, $2)",
params: ["John Doe", "john@example.com"]
}
});4. 列表模式
列出所连接数据库中的所有模式。
use_mcp_tool({
server_name: "postgres",
tool_name: "list_schemas",
arguments: {}
});5. 列表表
列出已连接数据库中的表。接受可选的 schema 参数(默认为“public”)。
// List tables in the 'public' schema (default)
use_mcp_tool({
server_name: "postgres",
tool_name: "list_tables",
arguments: {}
});
// List tables in a specific schema
use_mcp_tool({
server_name: "postgres",
tool_name: "list_tables",
arguments: {
schema: "my_schema"
}
});6. describe_table
获取特定表的结构。接受可选的 schema 参数(默认为“public”)。
// Describe a table in the 'public' schema (default)
use_mcp_tool({
server_name: "postgres",
tool_name: "describe_table",
arguments: {
table: "users"
}
});
// Describe a table in a specific schema
use_mcp_tool({
server_name: "postgres",
tool_name: "describe_table",
arguments: {
table: "users",
schema: "my_schema"
}
});特征
通过自动清理来安全处理连接
查询参数的预处理语句支持
支持 PostgreSQL 样式 ($1、$2) 和 MySQL 样式 (?) 参数占位符
全面的错误处理和验证
TypeScript 支持
自动连接管理
支持 PostgreSQL 特定的语法和功能
数据库操作的多模式支持
安全
使用准备好的语句来防止 SQL 注入
支持通过环境变量进行安全密码处理
执行前验证查询
完成后自动关闭连接
错误处理
服务器提供了常见问题的详细错误消息:
连接失败
无效查询
缺少参数
数据库错误
执照
麻省理工学院
Available Tools
5 toolsdescribe_tableDescribe a tableARead-only
Show the structure of one table: column names, data types, nullability, defaults, and primary-key membership. Call this before writing non-trivial queries against a table. Returns {columns: [{column, type, nullable, default, is_primary_key}, ...]}.
Inputs: table (required), schema.
executeRun a write statement (disabled)ADestructive
Run a data-modifying SQL statement (INSERT/UPDATE/DELETE or DDL). Currently DISABLED: the server is read-only, so this returns an error and changes nothing. To enable writes, the operator must start the server with PG_ALLOW_WRITE=true.
Inputs: sql (required), params.
list_schemasList schemasARead-only
List every schema in the connected database. Start here when exploring an unfamiliar database, then call list_tables for the schema you care about. Returns {schemas: [name, ...]}.
Inputs: no named parameters.
list_tablesList tablesARead-only
List all tables in a schema (default: 'public'). Use this before querying tables you have not seen yet, then call describe_table for column details. Returns {tables: [name, ...]}.
Inputs: schema.
queryRun read-only SQLARead-only
Run one read-only SQL statement against the connected PostgreSQL database and get rows back as JSON. Send exactly one statement per call (SELECT, WITH, EXPLAIN, or SHOW). It runs inside an engine-enforced read-only transaction, so any write is refused by the database. Use this tool for all data reading, aggregation, and query planning. Returns {rows, rowCount, returnedRows, truncated}, plus hint when truncated is true. Prefer $1, $2 placeholders with the params array over interpolating values. Results are capped at ~32768 bytes; truncated:true means rows were dropped - add LIMIT/WHERE or select fewer columns.
Inputs: sql (required), params.
Tool Schema Changelog
Recent tool additions, removals, and schema changes observed during successful MCP inspections.
Loading tool schema changelog…
TDQS
Scored across 5 tools
Each tool targets a clearly separate concern: read queries, write execution, schema discovery, table discovery, and column metadata. There is no meaningful overlap, and the descriptions reinforce the boundaries.
All tool names follow a clean verb_object pattern in snake_case: query, execute, list_schemas, list_tables, describe_table. The naming convention is consistent and predictable.
Five tools is well-scoped for a PostgreSQL server: one read path, one write path, and three introspection tools for exploring the database structure. No tool feels redundant or missing.
The tool surface covers the core database workflow well: discover schemas, inspect tables, describe columns, run read queries, and execute writes. The only caveat is that execute is disabled by default, so write workflows are not available unless the operator explicitly enables them.
Maintenance
Related MCP Connectors
Query Postgres, MySQL, SQL Server, Oracle, BigQuery, ClickHouse and Redshift from your AI client.
Query PostgreSQL databases in plain English — LLM-generated, safety-validated SQL.
Work with your database from AI tools: ask, export, approved changes, and scheduled jobs.
PostgreSQL, MySQL, and SQL Server in one session. 26 read-only MCP tools for AI agents.
Related MCP Servers
- AlicenseNot gradedqualityDmaintenanceEnables AI assistants to interact with PostgreSQL databases through natural language queries, schema inspection, and safe SQL execution.1ISC
- AlicenseNot gradedqualityDmaintenanceEnables AI agents to interact with PostgreSQL databases through the Model Context Protocol, supporting SQL queries, schema management, and data operations.MIT
- AlicenseNot gradedqualityDmaintenanceEnables AI assistants to safely interact with PostgreSQL databases, perform queries, inspect schemas, and analyze query performance.2ISC
- FlicenseNot gradedqualityDmaintenanceEnables AI assistants to securely query and modify PostgreSQL databases, supporting SQL queries, table management, and schema inspection.-