PostgreSQL MCP server

Helps you and your AI agents throughout the entire development process—from writing SQL to tuning performance safely.
Back to servers
Setup instructions
Provider
Crystal DBA
Release date
Mar 25, 2025
Language
Python
Package
Stats
23.0K downloads
664 stars

Postgres MCP Pro is a powerful Model Context Protocol (MCP) server that provides advanced database management capabilities for PostgreSQL, including health monitoring, index tuning, and query optimization.

Installation

Before getting started, ensure you have:

  1. Valid PostgreSQL database credentials
  2. Either Docker or Python 3.12+

Using Docker

Pull the Docker image:

docker pull crystaldba/postgres-mcp

Using Python

Install with pipx:

pipx install postgres-mcp

Or with uv:

uv pip install postgres-mcp

Configuring Your AI Assistant

Claude Desktop Configuration

Edit the Claude Desktop configuration file located at:

  • MacOS: ~/Library/Application Support/Claude/claude_desktop_config.json
  • Windows: %APPDATA%/Claude/claude_desktop_config.json

Docker Configuration

{
  "mcpServers": {
    "postgres": {
      "command": "docker",
      "args": [
        "run",
        "-i",
        "--rm",
        "-e",
        "DATABASE_URI",
        "crystaldba/postgres-mcp",
        "--access-mode=unrestricted"
      ],
      "env": {
        "DATABASE_URI": "postgresql://username:password@localhost:5432/dbname"
      }
    }
  }
}

Python Configuration (pipx)

{
  "mcpServers": {
    "postgres": {
      "command": "postgres-mcp",
      "args": [
        "--access-mode=unrestricted"
      ],
      "env": {
        "DATABASE_URI": "postgresql://username:password@localhost:5432/dbname"
      }
    }
  }
}

Access Modes

  • Unrestricted Mode: Full read/write access - suitable for development
  • Restricted Mode: Read-only operations - suitable for production

To use restricted mode, replace --access-mode=unrestricted with --access-mode=restricted.

SSE Transport Setup

To use Server-Sent Events (SSE) transport:

docker run -p 8000:8000 \
  -e DATABASE_URI=postgresql://username:password@localhost:5432/dbname \
  crystaldba/postgres-mcp --access-mode=unrestricted --transport=sse

Then update your MCP client configuration:

{
    "mcpServers": {
        "postgres": {
            "type": "sse",
            "url": "http://localhost:8000/sse"
        }
    }
}

Optional PostgreSQL Extensions

For full functionality, install these PostgreSQL extensions:

CREATE EXTENSION IF NOT EXISTS pg_statements;
CREATE EXTENSION IF NOT EXISTS hypopg;

Usage Examples

Database Health Overview

Ask: "Check the health of my database and identify any issues."

Analyze Slow Queries

Ask: "What are the slowest queries in my database? And how can I speed them up?"

Generate Index Recommendations

Ask: "Analyze my database workload and suggest indexes to improve performance."

Optimize a Specific Query

Ask: "Help me optimize this query: SELECT * FROM orders JOIN customers ON orders.customer_id = customers.id WHERE orders.created_at > '2023-01-01';"

Available Tools

Postgres MCP Pro provides several powerful tools:

  • list_schemas: Lists all database schemas
  • list_objects: Lists database objects within a schema
  • get_object_details: Provides detailed information about database objects
  • execute_sql: Executes SQL statements
  • explain_query: Gets execution plans for queries
  • get_top_queries: Reports the slowest SQL queries
  • analyze_workload_indexes: Recommends optimal indexes based on workload
  • analyze_query_indexes: Recommends indexes for specific queries
  • analyze_db_health: Performs comprehensive health checks

How to install this MCP server

For Claude Code

To add this MCP server to Claude Code, run this command in your terminal:

claude mcp add-json "postgres" '{"command":"postgres-mcp","args":["--access-mode=unrestricted"],"env":{"DATABASE_URI":"postgresql://username:password@localhost:5432/dbname"}}'

See the official Claude Code MCP documentation for more details.

For Cursor

There are two ways to add an MCP server to Cursor. The most common way is to add the server globally in the ~/.cursor/mcp.json file so that it is available in all of your projects.

If you only need the server in a single project, you can add it to the project instead by creating or adding it to the .cursor/mcp.json file.

Adding an MCP server to Cursor globally

To add a global MCP server go to Cursor Settings > Tools & Integrations and click "New MCP Server".

When you click that button the ~/.cursor/mcp.json file will be opened and you can add your server like this:

{
    "mcpServers": {
        "postgres": {
            "command": "postgres-mcp",
            "args": [
                "--access-mode=unrestricted"
            ],
            "env": {
                "DATABASE_URI": "postgresql://username:password@localhost:5432/dbname"
            }
        }
    }
}

Adding an MCP server to a project

To add an MCP server to a project you can create a new .cursor/mcp.json file or add it to the existing one. This will look exactly the same as the global MCP server example above.

How to use the MCP server

Once the server is installed, you might need to head back to Settings > MCP and click the refresh button.

The Cursor agent will then be able to see the available tools the added MCP server has available and will call them when it needs to.

You can also explicitly ask the agent to use the tool by mentioning the tool name and describing what the function does.

For Claude Desktop

To add this MCP server to Claude Desktop:

1. Find your configuration file:

  • macOS: ~/Library/Application Support/Claude/claude_desktop_config.json
  • Windows: %APPDATA%\Claude\claude_desktop_config.json
  • Linux: ~/.config/Claude/claude_desktop_config.json

2. Add this to your configuration file:

{
    "mcpServers": {
        "postgres": {
            "command": "postgres-mcp",
            "args": [
                "--access-mode=unrestricted"
            ],
            "env": {
                "DATABASE_URI": "postgresql://username:password@localhost:5432/dbname"
            }
        }
    }
}

3. Restart Claude Desktop for the changes to take effect

Want to 10x your AI skills?

Get a free account and learn to code + market your apps using AI (with or without vibes!).

Nah, maybe later