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.
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.
Frequently Asked Questions
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.
Citations (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
Related on TokRepo
Source & Thanks
Part of the MCP Servers collection. Licensed under MIT.
server-postgres — ⭐ 3,000+
Thanks for making databases conversational.