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.
What it is
PostgreSQL MCP Server is an MCP integration that connects AI coding tools directly to PostgreSQL databases. It enables schema inspection, read-only SQL queries, and data analysis through natural language. The server runs as a local process and communicates with AI agents using the Model Context Protocol standard.
This tool targets developers and data analysts who want to explore databases, debug queries, and generate reports without switching between their AI coding assistant and a database client.
How it saves time or tokens
Instead of copying table schemas and query results between a database client and your AI assistant, the MCP server gives the agent direct read access. The agent can inspect column types, run SELECT queries, and iterate on results in a single conversation. Read-only mode by default prevents accidental data modifications.
How to use
- Add the MCP server to your
.mcp.jsonconfiguration with your PostgreSQL connection string. - Restart your AI coding tool (Claude Code, Cursor, or similar MCP-compatible client).
- Ask the agent to list tables, inspect schemas, or run SQL queries in natural language.
Example
{
"mcpServers": {
"postgres": {
"command": "npx",
"args": ["-y", "@modelcontextprotocol/server-postgres", "postgresql://user:pass@localhost:5432/mydb"]
}
}
}
After configuration, ask your AI agent: 'List all tables in the public schema' or 'Show me the top 10 orders by total amount.'
Related on TokRepo
- PostgreSQL MCP Integration -- deep-dive into the PostgreSQL MCP connector
- AI Tools for Database -- explore other database-focused AI workflows
Common pitfalls
- The connection string is passed as a command argument, which may be visible in process listings; use environment variables for production databases.
- Read-only mode only restricts the MCP server layer; ensure your database user also has limited permissions as defense in depth.
- Large query results consume context window tokens; add LIMIT clauses to keep responses manageable.
Frequently Asked Questions
Yes. The server restricts operations to SELECT queries by default. DDL and DML operations like INSERT, UPDATE, and DELETE are blocked unless you explicitly enable write mode in the configuration.
Any MCP-compatible tool works, including Claude Code, Cursor, and Gemini CLI. The server uses the standard Model Context Protocol, so future tools that adopt MCP will also be compatible.
Pass the full connection string including host, port, username, password, and database name in the args array. For SSL connections, append sslmode=require to the connection string.
Yes. Add multiple entries in your .mcp.json, each with a different server name and connection string. Your AI agent can then switch between databases during a conversation.
Large result sets consume context window tokens and may cause truncation. Use LIMIT and WHERE clauses to keep results focused. The MCP server does not impose its own row limits by default.
Citations (3)
- MCP PostgreSQL Server— MCP server for PostgreSQL database access
- MCP Spec— Model Context Protocol specification
- PostgreSQL Docs— PostgreSQL connection string parameters
Related on TokRepo
Source & Thanks
Created by Anthropic. Licensed under MIT. modelcontextprotocol/servers