PostgreSQL MCP — SQL Database Server for AI Agents
MCP server that gives AI agents direct access to PostgreSQL databases. Run queries, explore schemas, manage tables, and analyze data through natural language. 3,000+ stars.
这个资产会安全暂存
这个资产会先安全暂存。复制的指令会要求 Agent 读取暂存文件,并在激活脚本、MCP 配置或全局配置前先确认。
npx -y tokrepo@latest install faa28c56-1377-4109-9e8f-a9b2be119d16 --target codex先暂存文件;激活前需要读取暂存 README 和安装计划。
What it is
PostgreSQL MCP is a Model Context Protocol server that gives AI agents like Claude direct access to PostgreSQL databases. It translates natural language requests into SQL queries, lets agents explore database schemas, and returns structured results. The server is part of the official MCP reference implementations.
It is designed for developers who want their AI coding assistant to read and write data in PostgreSQL without manually copying queries between the agent and a SQL client.
How it saves time or tokens
Without this MCP server, interacting with a database through an AI agent requires copying schema definitions into the prompt, writing SQL manually, running it in a separate client, and pasting results back. PostgreSQL MCP eliminates this loop. The agent reads the schema directly, writes and executes queries, and receives results in one turn. This reduces context-switching and avoids token waste from pasting large schema dumps.
How to use
- Add the PostgreSQL MCP server to your
.mcp.jsonconfiguration.
{
"mcpServers": {
"postgres": {
"command": "npx",
"args": ["-y", "@modelcontextprotocol/server-postgres"],
"env": {
"POSTGRES_CONNECTION_STRING": "postgresql://user:pass@localhost:5432/mydb"
}
}
}
}
- Restart Claude Code or your MCP-compatible client.
- Ask natural language questions like 'Show all tables in the database' or 'Find the top 10 customers by revenue.'
Example
Querying data through the MCP server:
-- The agent generates and executes this automatically:
SELECT c.name, SUM(o.amount) as total_revenue
FROM customers c
JOIN orders o ON c.id = o.customer_id
GROUP BY c.name
ORDER BY total_revenue DESC
LIMIT 10;
The agent receives the result set and formats it for you without any manual SQL work.
Related on TokRepo
- PostgreSQL MCP integration -- Detailed setup and configuration for the PostgreSQL MCP server.
- Database tools -- Other database tools that work with AI agents.
Common pitfalls
- Never use a production database connection string with write permissions. Create a read-only user for the MCP server to prevent accidental data modification.
- The connection string is stored in
.mcp.jsonwhich may be committed to version control. Add it to.gitignoreor use environment variables. - Large query results consume tokens. Add LIMIT clauses to avoid blowing through context windows on tables with millions of rows.
常见问题
Only if you connect with a read-only database user. The MCP server executes whatever SQL the agent generates, so a user with write permissions could accidentally modify or delete data. Always create a dedicated read-only role for MCP connections.
No. This MCP server is PostgreSQL-specific. Separate MCP servers exist for SQLite and other databases. Check the MCP server registry for your database.
Any MCP-compatible client works, including Claude Code, Claude Desktop, and third-party MCP hosts. The server communicates via the standard MCP protocol over stdio.
The MCP server exposes tools that let the agent query information_schema or pg_catalog to discover tables, columns, types, and relationships. The agent calls these tools before writing queries.
Yes. Create a PostgreSQL role with SELECT permission only on specific tables or schemas. The MCP server inherits the permissions of the database user in the connection string.
引用来源 (3)
- MCP Servers GitHub— MCP server for PostgreSQL database access
- MCP Documentation— Model Context Protocol specification for tool integration
- PostgreSQL Documentation— PostgreSQL information_schema for schema discovery
来源与感谢
Part of MCP Servers. Licensed under MIT.
讨论
相关资产
PostgreSQL MCP Server — Database Queries for AI
MCP server for PostgreSQL database access. Run read-only SQL queries, inspect schemas, list tables, and analyze data directly from Claude Code or Cursor.
WhatsApp MCP Server — Chat with WhatsApp via AI Agents
MCP server connecting Claude and AI agents to your personal WhatsApp. Search contacts, read messages, send replies and media via natural language.
Apify MCP Server — 8,000+ Web Scrapers for Agents
Apify MCP Server connects agents to Apify Actors via a hosted endpoint (mcp.apify.com) or local run, turning thousands of web scrapers into callable tools.
Postgres MCP Pro — Index Tuning + Safe SQL Tools
Postgres MCP Pro is an MCP server for PostgreSQL that runs safe SQL, explains plans, and recommends indexes so agents can tune databases faster.