MCP ConfigsApr 6, 2026·2 min read

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.

TL;DR
PostgreSQL MCP lets AI agents like Claude run SQL queries, explore schemas, and manage tables through the Model Context Protocol.
§01

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.

§02

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.

§03

How to use

  1. Add the PostgreSQL MCP server to your .mcp.json configuration.
{
  "mcpServers": {
    "postgres": {
      "command": "npx",
      "args": ["-y", "@modelcontextprotocol/server-postgres"],
      "env": {
        "POSTGRES_CONNECTION_STRING": "postgresql://user:pass@localhost:5432/mydb"
      }
    }
  }
}
  1. Restart Claude Code or your MCP-compatible client.
  1. Ask natural language questions like 'Show all tables in the database' or 'Find the top 10 customers by revenue.'
§04

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.

§05

Related on TokRepo

§06

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.json which may be committed to version control. Add it to .gitignore or 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

Is PostgreSQL MCP safe to use on production databases?+

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.

Does PostgreSQL MCP work with other databases like MySQL?+

No. This MCP server is PostgreSQL-specific. Separate MCP servers exist for SQLite and other databases. Check the MCP server registry for your database.

What MCP clients support PostgreSQL MCP?+

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.

How does the agent know my database schema?+

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.

Can I limit which tables the agent can access?+

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)
🙏

Source & Thanks

Part of the MCP Servers collection. Licensed under MIT.

server-postgres — ⭐ 3,000+

Thanks for making databases conversational.

Discussion

Sign in to join the discussion.
No comments yet. Be the first to share your thoughts.