Skip to main content

A simplified PostgreSQL MCP server

Project description

Simplified PostgreSQL MCP Server

中文文档

A lightweight PostgreSQL Model Context Protocol (MCP) server, designed to provide basic database interaction and query analysis capabilities.

Tools

  • query_sql

    • Description: Execute read-only SQL queries (SELECT).
    • Arguments: sql (string) - The SELECT query to execute.
    • Returns: Query results in JSON format.
  • execute_sql

    • Description: Execute modification SQL statements (DML) such as INSERT, UPDATE, DELETE.
    • Arguments: sql (string) - The DML statement to execute.
    • Returns: Execution status message (e.g., number of rows affected).
  • run_ddl

    • Description: Execute database structure definition statements (DDL) such as CREATE, DROP, ALTER, TRUNCATE.
    • Arguments: sql (string) - The DDL statement to execute.
    • Returns: Execution status message.
  • list_tables

    • Description: List tables in the database.
    • Arguments: schema (string, default "public") - The schema name to query.
    • Returns: JSON list containing table names and types.
  • describe_table

    • Description: Get detailed structure information of a table.
    • Arguments:
      • table_name (string) - The name of the table.
      • schema (string, default "public") - The schema name.
    • Returns: JSON list containing column names, data types, nullability, default values, etc.
  • explain_query

    • Description: Analyze SQL query plans, with support for hypothetical indexes.
    • Arguments:
      • sql (string) - The SQL statement to analyze.
      • analyze (boolean, default false) - Whether to actually execute the query (EXPLAIN ANALYZE).
      • hypothetical_indexes (list[string], optional) - List of hypothetical index definitions (requires hypopg extension).
    • Returns: Query plan in JSON format.

Quick Start

This project supports multiple running methods. Choose the one that fits your scenario.

Method 1: Using uvx (Recommended, No Installation Required)

If the code is published to PyPI or used via Git:

# Ensure environment variables are set
set DATABASE_URL=postgresql://postgres:password@localhost:5432/mydb

# Download and run automatically
uvx postgresql-server-mcp

Method 2: Local Development

# Enter directory
cd postgresql-mcp

# Run (uv automatically installs dependencies)
uv run postgresql-server-mcp

Configuration

Environment Variables

You can configure the database connection in one of the following ways:

  1. Method A (Recommended): Using DATABASE_URL

    set DATABASE_URL=postgresql://user:password@localhost:5432/dbname
    
  2. Method B: Using Standard PG Environment Variables If DATABASE_URL is not set, the server will automatically read the following variables:

    • PGUSER: Username
    • PGPASSWORD: Password
    • PGHOST: Host address (default localhost)
    • PGPORT: Port (default 5432)
    • PGDATABASE: Database name

MCP Client Configuration Example

Claude Desktop / Trae Configuration

Add the following configuration to your MCP config file:

{
  "mcpServers": {
    "postgresql": {
      "command": "uvx",
      "args": [
        "postgresql-server-mcp"
      ],
      "env": {
        "PGUSER": "your_username",
        "PGPASSWORD": "your_password",
        "PGHOST": "localhost",
        "PGPORT": "5432",
        "PGDATABASE": "your_dbname"
      }
    }
  }
}

Publishing Guide

If you want to publish this as a standard MCP package for others to use via uvx:

  1. Build:

    uv build
    
  2. Publish to PyPI:

    uv publish
    

After publishing, anyone can run it directly via uvx postgresql-server-mcp.

Hypothetical Index Analysis Example

To use hypothetical index analysis, your PostgreSQL database must have the hypopg extension installed:

-- Execute in database
CREATE EXTENSION hypopg;

Then call the explain_query tool in your MCP client:

  • sql: SELECT * FROM my_table WHERE col_a = 123
  • hypothetical_indexes: ["CREATE INDEX ON my_table (col_a)"]
  • analyze: false (Hypothetical indexes do not support analyze)

The server will simulate index creation and return the query plan, allowing you to compare Cost values to evaluate the index's effect.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

postgresql_server_mcp-0.1.3.tar.gz (6.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

postgresql_server_mcp-0.1.3-py3-none-any.whl (6.1 kB view details)

Uploaded Python 3

File details

Details for the file postgresql_server_mcp-0.1.3.tar.gz.

File metadata

  • Download URL: postgresql_server_mcp-0.1.3.tar.gz
  • Upload date:
  • Size: 6.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for postgresql_server_mcp-0.1.3.tar.gz
Algorithm Hash digest
SHA256 d10060673e39b5449b0b2770af143ca7aed52bbdee83d476d6d250aeef8ab6df
MD5 836c52a7c8a3072e31af9afb5ded49b7
BLAKE2b-256 4d19321580af4ef04dd35634773057c4b03166ba4050381095e7030488576eab

See more details on using hashes here.

File details

Details for the file postgresql_server_mcp-0.1.3-py3-none-any.whl.

File metadata

File hashes

Hashes for postgresql_server_mcp-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 7f8458f2faa6e9691751c7311af9a86d38d383d740317d2dd0c3bbb1b26c5c56
MD5 e4646aa0ec9e1f70ec9a1d70a015a4fc
BLAKE2b-256 3f0e5302b0f7c86d05f8545db400e006eb8bbaca19949c457ee9f14c5ca5ab2b

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page