Skip to main content

A simplified PostgreSQL MCP server

Project description

简易版 PostgreSQL MCP Server

这是一个精简的 PostgreSQL Model Context Protocol (MCP) 服务器,旨在提供基础的数据库交互和查询分析功能。

工具说明

  • query_sql

    • 功能: 执行通用的 SQL 查询。
    • 参数: sql (string) - 要执行的 SQL 语句。
    • 返回: JSON 格式的查询结果或执行状态消息。
  • list_tables

    • 功能: 列出数据库中的表。
    • 参数: schema (string, 默认 "public") - 要查询的模式名称。
    • 返回: 包含表名和表类型的 JSON 列表。
  • describe_table

    • 功能: 获取表的详细结构信息。
    • 参数:
      • table_name (string) - 表名。
      • schema (string, 默认 "public") - 模式名称。
    • 返回: 包含列名、数据类型、可空性、默认值等信息的 JSON 列表。
  • explain_query

    • 功能: 分析 SQL 查询计划,支持虚拟索引。
    • 参数:
      • sql (string) - 要分析的 SQL 语句。
      • analyze (boolean, 默认 false) - 是否实际执行查询 (EXPLAIN ANALYZE)。
      • hypothetical_indexes (list[string], 可选) - 虚拟索引定义列表 (需要 hypopg 扩展)。
    • 返回: JSON 格式的查询计划。

快速开始

本项目支持多种运行方式,您可以根据场景选择。

方式 1: 使用 uvx (推荐,无需安装)

如果您的代码已发布到 PyPI 或通过 Git 使用:

# 确保设置了环境变量
set DATABASE_URL=postgresql://postgres:password@localhost:5432/mydb

# 自动下载并运行
uvx postgresql-server-mcp

方式 2: 本地开发运行

# 进入目录
cd postgresql-mcp

# 运行 (uv 会自动安装依赖)
uv run postgresql-server-mcp

配置

环境变量

您可以使用以下任意一种方式配置数据库连接:

  1. 方式 A (推荐): 使用 DATABASE_URL

    set DATABASE_URL=postgresql://user:password@localhost:5432/dbname
    
  2. 方式 B: 使用标准 PG 环境变量 如果未设置 DATABASE_URL,服务器将自动读取以下变量:

    • PGUSER: 用户名
    • PGPASSWORD: 密码
    • PGHOST: 主机地址 (默认 localhost)
    • PGPORT: 端口 (默认 5432)
    • PGDATABASE: 数据库名

MCP 客户端配置示例

Claude Desktop / Trae 配置

请将以下配置添加到您的 MCP 配置文件中:

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

发布指南

如果您想将其发布为标准的 MCP 包,以便他人通过 uvx 使用:

  1. 构建:

    uv build
    
  2. 发布到 PyPI:

    uv publish
    

发布后,任何人都可以通过 uvx postgresql-server-mcp 直接运行它。

虚拟索引分析示例

要使用虚拟索引分析,您的 PostgreSQL 数据库必须安装 hypopg 扩展:

-- 在数据库中执行
CREATE EXTENSION hypopg;

然后在 MCP 客户端中调用 explain_query 工具:

  • sql: SELECT * FROM my_table WHERE col_a = 123
  • hypothetical_indexes: ["CREATE INDEX ON my_table (col_a)"]
  • analyze: false (虚拟索引不支持 analyze)

服务器将模拟创建索引并返回查询计划,您可以对比 Cost 值来评估索引效果。

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.1.tar.gz (5.3 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.1-py3-none-any.whl (5.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: postgresql_server_mcp-0.1.1.tar.gz
  • Upload date:
  • Size: 5.3 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.1.tar.gz
Algorithm Hash digest
SHA256 0b0143634b56d1ec4fa7ea2dfdff2c002c367a3584e392c5bab69dc8d2c5518b
MD5 1c61e84b6906a7ba74a1dc6757284cc8
BLAKE2b-256 9bda234990ef9a29aa84941995af3f7da33a6dad14d9b1d1acbc160b5a2bb349

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for postgresql_server_mcp-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 a526de560070af8ecf9eafe851ebed100db6868966cb36b623b602b305890571
MD5 2e884e14a459fbc5e4a4d25c9169e465
BLAKE2b-256 75045704be9c38c8c4a47ffa99f2a6a3e0008357fb628cdfbb1c751abc40c272

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