PostgreSQL Multi-Cluster Performance Tool (postgres-mcp)

0.3.0 · active · verified Thu Apr 16

PostgreSQL Tuning and Analysis Tool (Multi-Cluster Performance), currently at version 0.3.0. It's a command-line utility and a Python library designed to help analyze and optimize PostgreSQL database performance by providing recommendations and insights. Its release cadence is irregular, typical for a community-driven tool in its early stages.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to programmatically use `postgres-mcp` to connect to a PostgreSQL database and fetch performance recommendations using the `ConnectionManager` and `Recommender` classes. It requires setting database connection details via environment variables or hardcoding them (not recommended for production).

import os
from postgres_mcp.connections import ConnectionManager
from postgres_mcp.recommender import Recommender

# Ensure these environment variables are set or provide default values
DB_HOST = os.environ.get('PG_MCP_DB_HOST', 'localhost')
DB_PORT = int(os.environ.get('PG_MCP_DB_PORT', 5432))
DB_USER = os.environ.get('PG_MCP_DB_USER', 'postgres')
DB_PASS = os.environ.get('PG_MCP_DB_PASS', '') # Consider using an actual password or no password if allowed
DB_NAME = os.environ.get('PG_MCP_DB_NAME', 'postgres')

if not DB_PASS and DB_USER != 'postgres': # Simple check for non-default user requiring password
    print("Warning: DB_PASS environment variable not set. Connection might fail if user requires a password.")

try:
    # Initialize connection manager
    conn_manager = ConnectionManager(
        host=DB_HOST,
        port=DB_PORT,
        user=DB_USER,
        password=DB_PASS,
        dbname=DB_NAME
    )
    
    # Initialize the recommender with the connection manager
    recommender = Recommender(conn_manager)
    
    # Get recommendations
    print(f"Fetching recommendations for {DB_USER}@{DB_HOST}:{DB_PORT}/{DB_NAME}...")
    recommendations = recommender.get_recommendations()
    
    print("\nPostgreSQL Recommendations:")
    if recommendations:
        for category, recs in recommendations.items():
            print(f"[{category.upper()}]")
            for key, value in recs.items():
                print(f"  - {key}: {value}")
    else:
        print("No specific recommendations found (this might be normal for a healthy DB or small workload).")

except Exception as e:
    print(f"\nAn error occurred during recommendation generation: {e}")
    print("Please ensure:")
    print("1. Your PostgreSQL server is running and accessible from this machine.")
    print("2. The provided database connection details (host, port, user, password, dbname) are correct.")
    print("3. The database user has sufficient permissions to query system catalog views (e.g., pg_stat_activity, pg_settings).")

view raw JSON →