PostgreSQL MCP Server

io.github.musaddiq-dev由 GitHub 帳號 musaddiq-dev 發布v0.1.2更新於 Oct 24, 2026

Python MCP server for PostgreSQL database inspection and querying

已驗證STDIO僅桌面資料庫資料與分析

概覽

AI 產生的概覽

在本機執行的 Python MCP 伺服器,讓助理檢視並查詢 PostgreSQL 資料庫:列出資料表、查看結構、執行有列數上限的唯讀 SQL 與解讀執行計畫

可以做什麼
列出資料表並檢視資料表的欄位與中繼資料,取得資料庫版本與資料表數量摘要,搜尋公用 SQL 常式與函式。在唯讀交易中執行有列數上限的唯讀 SQL,解讀查詢計畫,預覽資料表最多 10 列資料。檢視外鍵關聯以及索引與其大小。執行單一會提交的修改語句(INSERT、UPDATE、DELETE、CREATE、ALTER、DROP、TRUNCATE)。讀取資料表清單與單一資料表結構等被動資源。
適用情境
適合助理需要探索不熟悉的 PostgreSQL 結構、依據即時資料回答問題,或在對話中查看查詢計畫與索引時使用。適用於已有資料庫連線的本機開發與分析情境。
執行需求
以 Python 套件形式透過 stdio 在本機執行(需 Python 3.11+),以 uvx 啟動,並需要可連線的 PostgreSQL 資料庫。必要環境變數:POSTGRES_HOST、POSTGRES_PORT、POSTGRES_USER、POSTGRES_DB;POSTGRES_PASSWORD 為選用。POSTGRES_READ_QUERY_LIMIT 限制讀取查詢回傳的列數。需要相容於 MCP 的用戶端。
安裝前請注意
伺服器包含可修改資料與結構並提交變更的寫入工具;README 稱其為破壞性操作,建議不要自動核准,並先檢視產生的 SQL。應使用最小權限、最好為唯讀的資料庫使用者。POSTGRES_PASSWORD 是機密,請勿提交含憑證的 .env 檔案或用戶端設定。

安裝

在 SourceWeft 中

  1. 開啟 儀表板中的 PostgreSQL MCP Server,將其新增到工作區。
  2. 為需要使用其工具的對話啟用該服務。

Desktop only,透過 STDIO。 STDIO 服務會啟動本機處理程序,因此需要 SourceWeft 桌面主機。

其他 MCP 客戶端

參照 儲存庫 中的啟動說明。

README

PostgreSQL MCP Server

A Python Model Context Protocol (MCP) server for inspecting and querying PostgreSQL databases from MCP-compatible clients. It provides schema discovery, safe read-only query execution, query explanation, table previews, index analysis, relationship inspection, and PostgreSQL resources for table metadata.

Features

  • List public tables and inspect table schemas
  • Execute read-only SQL in a PostgreSQL read-only transaction
  • Explain query plans without executing the target query directly
  • Preview table rows with a fixed limit
  • Inspect foreign-key relationships and indexes
  • Expose passive MCP resources for table lists and schema details

Safety Model

postgresql_execute_read_query runs with PostgreSQL read-only transaction mode, caps returned rows by POSTGRES_READ_QUERY_LIMIT, and rolls back after execution. The server also includes postgresql_execute_write_query, which only accepts a single INSERT, UPDATE, DELETE, CREATE, ALTER, DROP, or TRUNCATE statement and can modify data/schema if the connected database user has permission. Do not auto-approve write-capable tools in your MCP client. For public or shared use, run the server with a dedicated read-only PostgreSQL user.

Requirements

  • Python 3.11+
  • PostgreSQL database
  • MCP-compatible client such as Claude Desktop, Cursor, VS Code, or another MCP host

Installation

When published to PyPI, install or run the server like a standard Python MCP package:

bash
uvx mdev-postgresql-mcp-server

For local development from source:

bash
git clone https://github.com/musaddiq-dev/postgresql-mcp-server.gitcd postgresql-mcp-serverpython -m venv .venvsource .venv/bin/activatepip install -e .

Configuration

Copy the example environment file and update it with your database connection details.

bash
cp .env.example .env
VariableDescriptionRequiredDefault
POSTGRES_HOSTPostgreSQL hostYeslocalhost
POSTGRES_PORTPostgreSQL portYes5432
POSTGRES_USERPostgreSQL usernameYesNone
POSTGRES_PASSWORDPostgreSQL passwordNoNone
POSTGRES_DBPostgreSQL database nameYesNone
LOG_LEVELPython logging level written to stderrNoINFO
POSTGRES_READ_QUERY_LIMITMaximum rows returned by read queriesNo1000

Example read-only user:

sql
CREATE USER mcp_readonly WITH PASSWORD 'change-me';GRANT CONNECT ON DATABASE your_database TO mcp_readonly;GRANT USAGE ON SCHEMA public TO mcp_readonly;GRANT SELECT ON ALL TABLES IN SCHEMA public TO mcp_readonly;ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT SELECT ON TABLES TO mcp_readonly;

Running

bash
mdev-postgresql-mcp-server

From a local checkout before PyPI publication, run:

bash
python -m postgresql_mcp_server.server

MCP Client Configuration

For published installs, prefer uvx. MCP servers using stdio must write protocol messages only to stdout; this server writes logs to stderr through Python logging.

Claude Desktop / Cursor / Windsurf / Cline

Most MCP clients accept this mcpServers JSON shape:

json
{  "mcpServers": {    "postgresql": {      "command": "uvx",      "args": ["mdev-postgresql-mcp-server"],      "env": {        "POSTGRES_HOST": "localhost",        "POSTGRES_PORT": "5432",        "POSTGRES_USER": "mcp_readonly",        "POSTGRES_PASSWORD": "change-me",        "POSTGRES_DB": "your_database"      }    }  }}

For local development from this repository, use the installed console script path instead:

json
{  "mcpServers": {    "postgresql": {      "command": "/absolute/path/to/postgresql-mcp-server/.venv/bin/mdev-postgresql-mcp-server",      "args": [],      "env": {        "POSTGRES_HOST": "localhost",        "POSTGRES_PORT": "5432",        "POSTGRES_USER": "mcp_readonly",        "POSTGRES_PASSWORD": "change-me",        "POSTGRES_DB": "your_database"      }    }  }}

Claude Code CLI

bash
claude mcp add postgresql \  --env POSTGRES_HOST=localhost \  --env POSTGRES_PORT=5432 \  --env POSTGRES_USER=mcp_readonly \  --env POSTGRES_PASSWORD=change-me \  --env POSTGRES_DB=your_database \  -- uvx mdev-postgresql-mcp-server

VS Code MCP

VS Code uses the same command/args/env model in its MCP configuration:

json
{  "servers": {    "postgresql": {      "type": "stdio",      "command": "uvx",      "args": ["mdev-postgresql-mcp-server"],      "env": {        "POSTGRES_HOST": "localhost",        "POSTGRES_PORT": "5432",        "POSTGRES_USER": "mcp_readonly",        "POSTGRES_PASSWORD": "change-me",        "POSTGRES_DB": "your_database"      }    }  }}

Tools

ToolPurposeSafety
postgresql_list_tablesList public base tablesRead-only
postgresql_describe_tableShow columns and metadata for a tableRead-only
postgresql_execute_read_queryRun bounded SQL under read-only transaction modeRead-only
postgresql_execute_write_queryRun a single approved modifying SQL statement and commitDestructive
postgresql_explain_queryReturn PostgreSQL EXPLAIN output for a single queryRead-only
postgresql_get_database_summaryReturn database version and table countRead-only
postgresql_get_relationshipsInspect foreign-key relationshipsRead-only
postgresql_analyze_indexesInspect indexes and sizesRead-only
postgresql_preview_tableReturn up to 10 rows from a tableRead-only
postgresql_search_sql_definitionsSearch public SQL routines/functionsRead-only

Resources

  • postgres://list_tables returns public table names.
  • postgres://schema/{table_name} returns a generated schema statement for a table.

Smoke Check

Without a database, verify syntax with:

bash
python -m py_compile src/postgresql_mcp_server/server.py

With a configured database, start the server and use your MCP client to call list_tables.

Distribution

This server is published through the standard Python MCP distribution path:

  • PyPI package: mdev-postgresql-mcp-server
  • MCP Registry name: io.github.musaddiq-dev/postgresql-mcp-server
  • Runtime hint: uvx
  • Transport: stdio

The mcp-name marker at the top of this README is required for MCP Registry ownership verification. Users should prefer uvx mdev-postgresql-mcp-server in local MCP client configurations.

Security Notes

  • Do not commit .env or MCP client configs containing credentials.
  • Use least-privilege database users.
  • Treat execute_write_query as destructive and require explicit user approval in your MCP client.
  • Review generated SQL before running write-capable tools.

License

MIT

來源:README.md,提交 3e4ecf6

工具

0
SourceWeft 尚未在沙箱中啟動過該軟體包,因此其工具未知。

版本歷史

1
  1. v0.1.2最新Sep 16, 2026

評分

已滿足適用檢查項的 50%

該評分衡量此收錄條目的完整程度,依據註冊中心條目、端點與公開來源計算得出。它不是對伺服器本身的評價,也不表示伺服器安全或目前可用:可連通性是帶日期的檢查結果,評分也不包含安裝量或使用者評價。

  • 近 30 天內可連通25 分

    請保持端點能夠回應 MCP 握手;SourceWeft 每 30 天探測一次。

  • 已宣告認證方式10 分

    請在註冊中心條目中宣告憑證請求標頭,或保持端點能夠回應探測,以便判定其認證方式。

  • 已列出工具15 分

    請讓伺服器在無憑證時回應 tools/list,或發布可在沙箱中無憑證啟動的軟體包。

  • 描述必需15 分
  • 圖示10 分
  • 文件15 分
  • 授權條款10 分