PostgreSQL MCP server

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

Postgres MCP Pro is a server implementation of the Model Context Protocol (MCP) that provides advanced database management capabilities for PostgreSQL databases, including health analysis, index optimization, query plan evaluation, and safe SQL execution.

Installation

Before getting started, ensure you have:

  1. Access credentials for your database
  2. Docker or Python 3.12+ installed

Using Docker

Pull the Docker image:

docker pull crystaldba/postgres-mcp

Using Python

Install with pipx (recommended):

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

Postgres MCP Pro offers two access modes:

  • Unrestricted Mode: Full read/write access, suitable for development
  • Restricted Mode: Read-only with resource constraints, suitable for production

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

SSE Transport Configuration

For shared server setups, you can use the 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 client configuration:

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

Optional Postgres Extensions

For full functionality, install two extensions in your database:

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

These extensions enable comprehensive workload analysis and index simulation capabilities.

Usage Examples

Database Health Analysis

Ask your AI assistant:

Check the health of my database and identify any issues.

Slow Query Analysis

Ask:

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

Performance Optimization

Ask:

My app is slow. How can I make it faster?

Index Recommendations

Ask:

Analyze my database workload and suggest indexes to improve performance.

Query Optimization

Ask:

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

How to add this MCP server to 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 > MCP and click "Add new global MCP server".

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

{
    "mcpServers": {
        "cursor-rules-mcp": {
            "command": "npx",
            "args": [
                "-y",
                "cursor-rules-mcp"
            ]
        }
    }
}

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 explictly ask the agent to use the tool by mentioning the tool name and describing what the function does.

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