Skip to main content

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.

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.

Metadata

Release files for postgresql-server-mcp 0.1.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for postgresql-server-mcp 0.1.4
File Size Uploaded Provenance
postgresql_server_mcp-0.1.4.tar.gz 7.0 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for postgresql-server-mcp 0.1.4
File Interpreter ABI Platform
postgresql_server_mcp-0.1.4-py3-none-any.whl Python 3 none any Details

Total release size: 13.2 kB

Release files / postgresql_server_mcp-0.1.4.tar.gz

Download URL postgresql_server_mcp-0.1.4.tar.gz
Size 7.0 kB
Tags Source
SHA-256 checksum
How to use checksums
913e5b1d394ee135d130dc7d91a0de511e1e5b5f5cbef81cbb2edd30271136e1
BLAKE2b-256 checksum
How to use checksums
23fb7965f96743c4f6b151632c2b9f15c87dabc92fa2678825bc51f9595d2f09
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.7

Release files / postgresql_server_mcp-0.1.4-py3-none-any.whl

Download URL postgresql_server_mcp-0.1.4-py3-none-any.whl
Size 6.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
9c136bbc12716be809b5f6e72488c6fad306f0a186bbf4bd120e60ae5b4407f4
BLAKE2b-256 checksum
How to use checksums
c92be77c1683e1d4f8f226f18eccb0dca3208c80129c26f27c503dfaca848af7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.7

Security for postgresql-server-mcp 0.1.4

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page