Postgres

io.github.marcelo-ochoa由 GitHub 帳號 marcelo-ochoa 發布v1.0.8更新於 Oct 24, 2026

MCP server for interacting with PostgreSQL databases

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

概覽

AI 產生的概覽

本機 MCP 伺服器,讓助理以唯讀方式存取 PostgreSQL 資料庫:執行查詢、檢視資料表結構、解釋執行計畫並產生效能統計。

可以做什麼
在唯讀交易中執行唯讀 SQL 查詢,並解釋查詢的執行計畫。取得單一資料表的統計資訊,並產生類似 Oracle AWR 的效能報告,涵蓋資料庫統計、熱門查詢、資料表與索引統計及最佳化建議。以資源形式瀏覽自動探索的資料表結構,包含欄位名稱與資料型別。執行時透過連線字串、使用者名稱與密碼連線資料庫。
適用情境
適合讓助理安全地探查或分析既有的 PostgreSQL 資料庫,例如回答資料相關問題、檢視資料表結構,或在不授予寫入權限的情況下排查慢查詢與效能問題。
執行需求
以 stdio 方式在本機執行,可透過 npm(npx)或 Docker 安裝。需要可連線的 PostgreSQL 伺服器,以及包含主機、連接埠與資料庫名稱的連線字串,並透過環境變數或連線工具提供資料庫使用者名稱與密碼。可透過連線字串參數或環境變數啟用 SSL。
安裝前請注意
伺服器會持有資料庫憑證;密碼應放在環境變數或連線工具中,不要寫入共用的設定檔。說明為唯讀存取且查詢在唯讀交易中執行,但所用憑證本身可能擁有更大權限,建議使用受限的資料庫帳號。效能報告依賴已安裝 pg_stat_statements 擴充功能。

安裝

在 SourceWeft 中

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

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

其他 MCP 客戶端

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

README

PostgreSQL

A Model Context Protocol server that provides read-only access to PostgreSQL databases. This server enables LLMs to inspect database schemas and execute read-only queries.

Components

Tools

  • pg-query

    • Execute read-only SQL queries against the connected database
    • Input: sql (string): The SQL query to execute
    • All queries are executed within a READ ONLY transaction
  • pg-stats

    • Get statistics for a specific table
    • Input: name (string): The name of the table to get statistics for
  • pg-explain

    • Explain Plan for a given SQL query
    • Input: sql (string): The SQL query to explain
  • pg-connect

    • Connect to a PostgreSQL database
    • Inputs:
      • connectionString (string): The PostgreSQL connection string without credentials (e.g. postgresql://host:port/dbname or host:port/dbname)
      • user (string): The PostgreSQL username
      • password (string): The PostgreSQL password

Example: pg-connect host.docker.internal:5432/postgres postgres pg_2025

  • pg-awr
    • Generate a PostgreSQL performance report similar to Oracle AWR. Includes database statistics, top queries (requires pg_stat_statements extension), table/index statistics, connection info, and optimization recommendations.

Resources

The server provides schema information for each table in the database:

  • Table Schemas (postgres://<dbname>/<table>/schema)
    • JSON schema information for each table
    • Includes column names and data types
    • Automatically discovered from database metadata

Change Log

See Change Log for the history of changes.

Configuration

The PostgreSQL server uses environment variables or the pg-connect tool for secure credential management:

  • PG_USER: PostgreSQL username (optional if using pg-connect)
  • PG_PASSWORD: PostgreSQL password (optional if using pg-connect)

Connection String

The connection string should contain only the host, port, and database information (without embedded credentials). Providing it as a command-line argument is optional. If omitted at startup, you must use the pg-connect tool to establish a connection before using other functionality.

Supported connection string formats:

  • postgresql://host:port/dbname
  • host:port/dbname

To enable encryption (SSL), append ?sslmode=require to the connection string or set the PG_SSL environment variable to true.

Usage with Claude Desktop

To use this server with the Claude Desktop app, add the following configuration to the "mcpServers" section of your claude_desktop_config.json:

Docker

  • When running Docker on macOS, use host.docker.internal if the PostgreSQL server is running on the host network (e.g., localhost)
  • Credentials are passed via environment variables PG_USER and PG_PASSWORD
json
{  "mcpServers": {    "postgres": {      "command": "docker",      "args": [        "run",         "-i",         "--rm",        "-e", "PG_USER=myuser",        "-e", "PG_PASSWORD=mypassword",        "mochoa/mcp-postgres"      ]    }  }}

Note: You can still provide the connection string as a final argument if you want to connect automatically on startup: "args": [..., "mochoa/mcp-postgres", "postgresql://host.docker.internal:5432/mydb"].

NPX

json
{  "mcpServers": {    "postgres": {      "command": "npx",      "args": [        "-y",        "@marcelo-ochoa/server-postgres",        "postgresql://localhost:5432/mydb"      ],      "env": {        "PG_USER": "myuser",        "PG_PASSWORD": "mypassword"      }    }  }}

Replace /mydb with your database name.

Note: Replace the following placeholders with your actual values:

  • myuser and mypassword with your PostgreSQL credentials
  • localhost:5432 with your PostgreSQL server host and port
  • mydb with your database name

Usage with VS Code

For quick installation, use one of the one-click install buttons below...

[Install with NPX in VS Code] [Install with NPX in VS Code Insiders]

[Install with Docker in VS Code] [Install with Docker in VS Code Insiders]

For manual installation, add the following JSON block to your User Settings (JSON) file in VS Code. You can do this by pressing Ctrl + Shift + P and typing Preferences: Open User Settings (JSON).

Optionally, you can add it to a file called .vscode/mcp.json in your workspace. This will allow you to share the configuration with others.

Note that the mcp key is not needed in the .vscode/mcp.json file.

Docker

Note: When using Docker and connecting to a PostgreSQL server on your host machine, use host.docker.internal instead of localhost in the connection URL.

json
{  "mcp": {    "inputs": [      {        "type": "promptString",        "id": "pg_user",        "description": "PostgreSQL username"      },      {        "type": "promptString",        "id": "pg_password",        "description": "PostgreSQL password",        "password": true      }    ],    "servers": {      "postgres": {        "command": "docker",        "args": [          "run",          "-i",          "--rm",          "-e", "PG_USER=${input:pg_user}",          "-e", "PG_PASSWORD=${input:pg_password}",          "mochoa/mcp-postgres",          "postgresql://localhost:5432/mydb"        ]      }    }  }}

Note: You can add an input for pg_url and append it to args if you want to connect on startup.

NPX

json
{  "mcp": {    "inputs": [      {        "type": "promptString",        "id": "pg_user",        "description": "PostgreSQL username"      },      {        "type": "promptString",        "id": "pg_password",        "description": "PostgreSQL password",        "password": true      }    ],    "servers": {      "postgres": {        "command": "npx",        "args": [          "-y",          "@marcelo-ochoa/server-postgres",          "postgresql://localhost:5432/mydb"        ],        "env": {          "PG_USER": "${input:pg_user}",          "PG_PASSWORD": "${input:pg_password}"        }      }    }  }}

PostgreSQL AWR in action

See PostgreSQL AWR in action for an example of a performance report generated by the pg-awr tool for a production Moodle database, highlighting critical performance issues and optimization opportunities.

Demos

See Demos for usage examples with Claude Desktop, Docker AI, Gemini CLI, and Antigravity Code Editor.

Building

Docker:

sh
docker build -t mochoa/mcp-postgres -f src/postgres/Dockerfile .

Sources

As usual the code of this extension is at GitHub, feel free to suggest changes and make contributions, note that I am a beginner developer of React and TypeScript so contributions to make this UI better are welcome.

📜 License

This project is licensed under the Apache License, Version 2.0 for new contributions, with existing code under MIT - see the LICENSE file for details.

來源:src/postgres/README.md,提交 bd2ce8f

工具

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

版本歷史

1
  1. v1.0.8最新Sep 16, 2026

評分

已滿足適用檢查項的 40%

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

  • 近 30 天內可連通25 分

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

  • 已宣告認證方式10 分

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

  • 已列出工具15 分

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

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

    請在儲存庫或註冊中心條目中宣告授權條款。