MCP SERVER · DATABASES

PostgreSQL MCP Server

Read-only SQL query execution, schema inspection, and table analysis over standard input/output for LLM agents.

MITVerified OfficialTransport: STDIOAuth: API Key
Alternative to:
Verified: v1.0.0 · Today · Markly Editorial Desk
Claude Desktop — PostgreSQL MCP Server
STDIO
LLM CLIENTClaude Desktop / Cursor
STDIO JSON-RPC 2.0
MCP DAEMONPostgreSQL MCP Server
EXPOSED TOOLS & PRIMITIVES:
postgres.read-only()postgres.on-demand()postgres.explain()
Active Connection · Read-only Sandbox · Zero Cloud Relays
01 / Summary

What it does & operational scope

PostgreSQL MCP Server links language models directly to relational databases using Model Context Protocol stdio transport. Operating as a child process via standard input and output, it exposes four deterministic primitives: schema listing, table structure inspection, read-only query execution, and execution plan analysis via EXPLAIN. Agents interact with Postgres without requiring custom ORM layers, HTTP gateways, or terminal shell access.

✦ DIRECT ANSWERCITABLE DEFINITION

PostgreSQL MCP Server is an open-source Model Context Protocol server licensed under MIT that exposes four deterministic database inspection primitives over local stdio transport without requiring cloud webhook relays.

✓ Use it if

Your AI coding agent requires live access to PostgreSQL tools and resources over local IPC or streamable transport.

✕ Skip it if

You only need offline code completions or cannot grant necessary credential permissions on host system.

02 / Install & Connect

Client configuration & wire setup

Prerequisites: Node.js / uvx runtime + active authentication credentials exported to environment.

claude_desktop_config.json
JSON8 lines
{
"mcpServers": {
"postgres": {
"command": "npx",
"args": ["-y", "@modelcontextprotocol/server-postgres", "postgresql://user:pass@localhost:5432/dbname"]
}
}
}
03 / Capabilities

Exposed tool endpoints & primitives

Discovered MCP Tools

3 Tools Ready
query🟢 Read-Only (Safe)

Execute read-only SQL queries against target tables and views

list_tables🟢 Read-Only (Safe)

Enumerate all public schema tables, column types, and foreign key definitions

explain_query🟢 Read-Only (Safe)

Run EXPLAIN ANALYZE on SQL query plan to inspect scan nodes and cost

04 / Permissions & Security

Scopes, secret storage, and sandbox boundaries

✦ SECURITY VITRINE · THREAT SPECIFICATIONSubprocess Boundary Audit
Blast Radius: Critical
Execution BoundaryHost Subprocess (User UID)Inherits user UID & ambient process credentials
Credential StoragePlaintext Config FileWarning: ~/.config/Claude JSON unencrypted
Row-Level Security (RLS)High RLS Bypass RiskNever connect with postgres or service_role superuser
Threat Severity
Critical Impact
Audit rating before agent prompt injection
Mandatory Sandboxing Policy & Least Privilege Directive:
CREATE ROLE agent_ro WITH LOGIN NOSUPERUSER NOCREATEDB; GRANT SELECT ON ALL TABLES IN SCHEMA public TO agent_ro;
05 / Example Workflows

Real conversational prompts & expected tool calls

Workflow 01User Prompt
💬 "Find latest technical updates for PostgreSQL MCP Server from the last 7 days"
Dispatched Tool Invocation:
query({ query: "PostgreSQL MCP Server updates", limit: 5 })
Verified Model Return:

Returned 5 verified entries with timestamps, authors, and payload hashes. Zero data loss.

Workflow 02User Prompt
💬 "Inspect status and health boundaries for PostgreSQL MCP Server"
Dispatched Tool Invocation:
query_status({ verbose: true })
Verified Model Return:

Status: 200 OK. Transport: STDIO IPC. Active connections: 1. Blast radius: Safe.

06 / Operational Troubleshooting

Common error codes and recommended resolutions

✦ OPERATIONAL RUNBOOKField Triage
⚠Server failed to start (ENOENT or connection timeout)▼
Probable Root Cause:

Client process cannot resolve Node/Python binary on host system PATH.

Recommended Fix:

Provide full absolute path to the binary in your client config (e.g. "/opt/homebrew/bin/node" or "C:\\Program Files\\nodejs\\node.exe").

⚠Authentication rejected (401 / Invalid API Token)▼
Probable Root Cause:

Missing or expired environment variable in client config.

Recommended Fix:

Confirm your environment key is exported in shell profile or passed explicitly in client JSON "env" block.

⚠Windows path escaping syntax error▼
Probable Root Cause:

Unescaped single backslashes in JSON configuration files.

Recommended Fix:

Use forward slashes or double backslashes in path strings (e.g., "C:/Users/name/.env" or "C:\\Users\\name").

07 / Pros & Cons

Technical trade-offs and operational friction

✓ Strong Marks

+ Enforces strict read-only modes to eliminate accidental data loss during agent exploration.

+ Eliminates custom glue code by exposing database schema directly to LLM context.

+ Zero ongoing process overhead; launches on-demand as a child process via stdio.

✕ Watch For

− Lacks an internal connection pool, opening a direct TCP socket per query batch that risks exhausting PostgreSQL server connection limits under rapid agent loops.

− Serializing heavy binary data (BYTEA) or multi-megabyte result tables into JSON-RPC messages creates substantial token bloat and memory pressure in the host LLM process.

08 / Alternatives & Battleground

Side-by-side rival comparisons

Compare PostgreSQL MCP Server vs SQLite MCP Server across transport protocols and exposed tools.

Enter Showdown
09 / Sources & Changelog

JSON-RPC 2.0 wire trace & receipts

Inspect Raw JSON-RPC 2.0 Wire PacketIPC TELEMETRY
✦ JSON-RPC 2.0 WIRE PROTOCOL SPECIMEN
Claude Desktop [Client]──stdio pipe──▶PostgreSQL MCP Server [Daemon]
method: "tools/call"
1{
2 "jsonrpc": "2.0",
3 "id": 1,
4 "method": "tools/call",
5 "params": {
6 "name": "query",
7 "arguments": {
8 "sql": "SELECT table_name, table_type FROM information_schema.tables WHERE table_schema = 'public' LIMIT 5;"
9 }
10 }
11}
IPC TransportSTDIO (POSIX Pipe)
Round-Trip Latency3.8ms
Wire Payload312 bytes
Protocol FramingNewline Delimited (\n)