PostgreSQL MCP Server
Read-only PostgreSQL access for LLMs. Execute SQL queries inside read-only transactions and inspect table schemas as MCP resources.
The PostgreSQL MCP server is one of the original reference implementations from the Model Context Protocol team. It gives an LLM read-only access to a PostgreSQL database: the model can execute SQL queries (wrapped in READ ONLY transactions) and discover table structures through schema resources. It is intentionally narrow in scope, designed to demonstrate a safe, well-bounded MCP server rather than a full-featured database admin tool.
The server exposes a single tool, query, which accepts an SQL string and runs it inside a read-only transaction so writes are rejected by the database engine itself. Alongside that, each table in the connected database is published as a resource at postgres://<host>/<table>/schema, returning JSON describing column names and data types. This makes it straightforward for an agent to list tables, learn their schemas, and then issue informed SELECT statements.
Note: this server has been moved to the modelcontextprotocol/servers-archived repository. It is still installable via npm and Docker, but is no longer actively developed by the MCP steering group. For active maintenance and write capabilities, see the alternatives section.
Tools
| Tool | Description |
|---|---|
query |
Execute a read-only SQL query against the connected PostgreSQL database. Runs inside a READ ONLY transaction so any write statements are rejected by Postgres. |
Prerequisites
- A running PostgreSQL database reachable from the machine running the MCP client
- A connection string in the form
postgresql://user:password@host:port/db-name - Node.js (for the NPX install method) or Docker
Claude Desktop (NPX)
Add to claude_desktop_config.json:
{
"mcpServers": {
"postgres": {
"command": "npx",
"args": [
"-y",
"@modelcontextprotocol/server-postgres",
"postgresql://localhost/mydb"
]
}
}
}
Claude Desktop (Docker)
{
"mcpServers": {
"postgres": {
"command": "docker",
"args": [
"run",
"-i",
"--rm",
"mcp/postgres",
"postgresql://host.docker.internal:5432/mydb"
]
}
}
}
On macOS, use host.docker.internal instead of localhost so the container can reach the host-side Postgres process.
VS Code
Add the same server entry to your User Settings (JSON) under mcp.servers, or place it in a .vscode/mcp.json file in your workspace.
Building the Docker image locally
docker build -t mcp/postgres -f src/postgres/Dockerfile .
The package is licensed under MIT.
- Let an AI agent answer business questions by querying a Postgres analytics database without risk of writes
- Auto-generate SQL from natural language, then execute against a staging database to validate results
- Schema discovery and documentation: have the agent enumerate tables and produce a data dictionary from the exposed schema resources
- Ad-hoc data exploration during product or support investigations, with hard guarantees that no rows can be modified
- Power read-only data Q&A inside internal tools or chat assistants backed by an existing Postgres warehouse
- "List all tables in the connected database and summarize what each one stores."
- "What were total orders by week for the last 8 weeks? Use the
orderstable." - "Find the top 10 customers by revenue in 2026 and include their email addresses."
- "Describe the columns and types of the
userstable, then write a query that counts signups per month." - "Run an EXPLAIN on
SELECT * FROM events WHERE created_at > now() - interval '7 days'and tell me if it uses an index."
- Officially built by the MCP team as a reference implementation, so the protocol usage is canonical
- True read-only safety: queries run inside a
READ ONLYPostgres transaction, enforced by the database engine, not just by prompt - Minimal surface area and easy to audit: one tool, one resource pattern
- Available as both an npm package and a prebuilt Docker image
- Now archived in
modelcontextprotocol/servers-archived; no longer actively maintained by the MCP steering group - Read-only only: no inserts, updates, schema changes, or transaction control
- Very thin feature set (single
querytool, no parameterized queries, no row limits, no result pagination)
- crystaldba/postgres-mcp (Postgres Pro): actively maintained Postgres MCP server with read/write modes, EXPLAIN analysis, and index tuning
- MCP Toolbox for Databases: Google-backed open source server supporting Postgres, AlloyDB, Cloud SQL, MySQL, BigQuery, and more
- AWS Labs Aurora Postgres MCP Server: AWS-maintained server for Aurora Postgres with broader tool coverage