Skip to content
Database Engineering & SQLJune 2026 · 13 min read

PostgreSQL MCP Server Setup Guide: Natural Language SQL & Schema AI

Connect Claude Desktop, Cursor IDE, and VS Code directly to PostgreSQL databases to introspect schemas, optimize queries with EXPLAIN ANALYZE, and explore relational datasets using natural language.

1. Transforming Relational Databases into Conversational Partners

Navigating complex relational schemas across dozens of tables, foreign key mappings, and composite indexes is one of the most time-consuming aspects of backend software development. Developers regularly jump between GUI database clients, terminal psql sessions, and code editors just to inspect column names, verify data types, or craft complex multi-table SQL queries.

The PostgreSQL Model Context Protocol Server (@modelcontextprotocol/server-postgres) bridges this gap by exposing your database schema as dynamic MCP resources and providing parameterized query tools. Instead of manually writing boilerplate SQL queries or crafting complex JOIN operations, you can describe your data requirements in plain English, and your AI assistant will query the database, validate table structures, and output formatted markdown tables.

Whether analyzing user churn in an analytics database, inspecting foreign key cascades, or optimizing slow queries, the PostgreSQL MCP server gives your assistant real-time database intelligence with strict security isolation.

2. Hardening Security: Creating a Dedicated Read-Only User

Never connect an AI assistant using the administrative postgres superuser account. Execute the following SQL script to create a secure, isolated read-only role with a defensive statement timeout:

-- Step 1: Create isolated role
CREATE ROLE mcp_readonly WITH LOGIN PASSWORD 'YourStrongPassword123#';

-- Step 2: Grant database connection and schema visibility
GRANT CONNECT ON DATABASE production_db TO mcp_readonly;
GRANT USAGE ON SCHEMA public TO mcp_readonly;

-- Step 3: Grant SELECT on all current and future tables
GRANT SELECT ON ALL TABLES IN SCHEMA public TO mcp_readonly;
ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT SELECT ON TABLES TO mcp_readonly;

-- Step 4: Ensure no mutation capabilities exist
REVOKE INSERT, UPDATE, DELETE, TRUNCATE ON ALL TABLES IN SCHEMA public FROM mcp_readonly;

-- Step 5: Enforce 5-second statement timeout to prevent table locks
ALTER ROLE mcp_readonly SET statement_timeout = '5000';

Setting a statement timeout ensures that even if the AI generates an unindexed multi-table JOIN query against millions of rows, the database engine will automatically cancel the query after 5 seconds before impacting production traffic.

3. Multi-Client Installation Configurations

Register the PostgreSQL server inside your AI client configuration:

Claude Desktop Config

claude_desktop_config.json
{
  "mcpServers": {
    "postgres": {
      "command": "npx",
      "args": [
        "-y",
        "@modelcontextprotocol/server-postgres",
        "postgresql://mcp_readonly:YourStrongPassword123#@localhost:5432/production_db"
      ]
    }
  }
}

Cursor IDE Config

.cursor/mcp.json
{
  "mcpServers": {
    "postgres": {
      "command": "npx",
      "args": [
        "-y",
        "@modelcontextprotocol/server-postgres",
        "postgresql://mcp_readonly:YourStrongPassword123#@localhost:5432/production_db"
      ]
    }
  }
}

4. High-Impact Database Workflows

Once your server is running, you can explore and optimize your database through conversational prompts:

Schema Introspection & ERD Diagramming

Prompt: “Read our database schema. Identify all foreign key relationships with the users table and output a Mermaid ERD diagram.”

The MCP server reads the schema resource, parses primary and foreign keys, and outputs a complete visual entity-relationship diagram.

Query Performance Optimization with EXPLAIN ANALYZE

Prompt: “Run EXPLAIN ANALYZE on our slow customer analytics query and suggest composite indexes to eliminate sequential table scans.”

Claude inspects query cost metrics, execution node timings, and recommends exact CREATE INDEX CONCURRENTLY statements.

5. Connecting Cloud Databases (Neon, Supabase, RDS)

When connecting to remote cloud databases, always append ?sslmode=require (or ?sslmode=verify-full) to your connection URI to ensure encrypted transport over TLS:

postgresql://readonly_user:<password>@ep-cool-base-123456.us-east-2.aws.neon.tech/neondb?sslmode=require

If your database is hosted behind a corporate firewall or VPN, you can run the MCP server on a secure bastion host and expose it to your client via SSH port forwarding.