Skip to main content
Glama
gldc

PostgreSQL MCP Server

by gldc

PostgreSQL MCP サーバー

鍛冶屋のバッジ

モデルコンテキストプロトコル(MCP) Python SDKを使用したPostgreSQL MCPサーバー実装。MCPは、LLMアプリケーションと外部データソース間のシームレスな統合を可能にするオープンプロトコルです。このサーバーにより、AIエージェントは標準化されたインターフェースを介してPostgreSQLデータベースと対話できるようになります。

特徴

  • データベーススキーマの一覧表示

  • スキーマ内のテーブルを一覧表示する

  • テーブル構造を説明する

  • テーブルの制約と関係を一覧表示する

  • 外部キー情報を取得する

  • SQLクエリを実行する

Related MCP server: PostgreSQL MCP Server

クイックスタート

# Run the server without a DB connection (useful for Glama or inspection)
python postgres_server.py

# With a live database – pick one method:
export POSTGRES_CONNECTION_STRING="postgresql://user:pass@host:5432/db"
python postgres_server.py

# …or…
python postgres_server.py --conn "postgresql://user:pass@host:5432/db"

# Or using Docker (build once, then run):
# docker build -t mcp-postgres . && docker run -p 8000:8000 mcp-postgres

インストール

Smithery経由でインストール

Smithery経由で Claude Desktop 用の PostgreSQL MCP サーバーを自動的にインストールするには:

npx -y @smithery/cli install @gldc/mcp-postgres --client claude

手動インストール

  1. このリポジトリをクローンします:

git clone <repository-url>
cd mcp-postgres
  1. 仮想環境を作成してアクティブ化します (推奨):

python -m venv venv
source venv/bin/activate  # On Windows, use: venv\Scripts\activate
  1. 依存関係をインストールします:

pip install -r requirements.txt

使用法

  1. MCP サーバーを起動します。

    # Without a connection string (server starts, DB‑backed tools will return a friendly error)
    python postgres_server.py
    
    # Or set the connection string via environment variable:
    export POSTGRES_CONNECTION_STRING="postgresql://username:password@host:port/database"
    python postgres_server.py
    
    # Or pass it using the --conn flag:
    python postgres_server.py --conn "postgresql://username:password@host:port/database"
  2. サーバーは次のツールを提供します。

  • query : データベースに対してSQLクエリを実行する

  • list_schemas : 利用可能なすべてのスキーマを一覧表示する

  • list_tables : 特定のスキーマ内のすべてのテーブルを一覧表示する

  • describe_table : テーブルの構造に関する詳細情報を取得する

  • get_foreign_keys : テーブルの外部キー関係を取得する

  • find_relationships : テーブルの明示的および暗黙的な関係の両方を検出します

Dockerで実行する

イメージをビルドします。

docker build -t mcp-postgres .

データベース接続なしでコンテナを実行します(サーバーは検査可能なままです)。

docker run -p 8000:8000 mcp-postgres

POSTGRES_CONNECTION_STRINGを指定してライブ PostgreSQL データベースで実行します。

docker run \
  -e POSTGRES_CONNECTION_STRING="postgresql://username:password@host:5432/database" \
  -p 8000:8000 \
  mcp-postgres

環境変数を省略すると、サーバーは通常どおり起動し、環境変数が指定されるまで、すべてのデータベース バックアップ ツールは「接続文字列が設定されていません」というわかりやすいメッセージを返します。

mcp.json による設定

このサーバーを MCP 互換ツール (Cursor など) と統合するには、 ~/.cursor/mcp.jsonに追加します。

{
  "servers": {
    "postgres": {
      "command": "/path/to/venv/bin/python",
      "args": [
        "/path/to/postgres_server.py"
      ],
      "env": {
        "POSTGRES_CONNECTION_STRING": "postgresql://username:password@host:5432/database?ssl=true"
      }
    }
  }
}

POSTGRES_CONNECTION_STRINGが省略された場合でも、サーバーは起動し、完全に検査可能です。データベース バックアップ ツールは、変数が指定されるまで、情報エラーを返すだけです。

交換する:

  • /path/to/venv仮想環境のパスに置き換えます

  • /path/to/postgres_server.pyにサーバースクリプトへの絶対パスを記述します。

安全

  • コード内で機密データベースの認証情報を公開しないでください

  • データベース接続文字列には環境変数または安全な構成ファイルを使用する

  • リソース管理を改善するために接続プールの使用を検討する

  • 適切なアクセス制御とユーザー認証を実装する

貢献

貢献を歓迎します!お気軽にプルリクエストを送信してください。

関連プロジェクト

ライセンス

MITライセンス

著作権 (c) 2025 gldc

本ソフトウェアおよび関連ドキュメント ファイル (以下「本ソフトウェア」) のコピーを入手したすべての人物は、以下の条件に従い、本ソフトウェアを無制限に扱う権利 (使用、コピー、変更、統合、公開、配布、サブライセンス、および/または販売する権利を含みますが、これに限定されません) および本ソフトウェアの提供を受けた人物が同様の行為を行うことを許可する権利を無償で付与されます。

上記の著作権表示およびこの許可通知は、ソフトウェアのすべてのコピーまたは大部分に含めるものとします。

本ソフトウェアは「現状有姿」で提供され、明示的または黙示的を問わず、商品性、特定目的への適合性、非侵害性を含むがこれらに限定されない、いかなる種類の保証も付与されません。いかなる場合においても、著作者または著作権者は、契約違反、不法行為、またはその他の行為にかかわらず、本ソフトウェア、本ソフトウェアの使用、またはその他の取り扱いに起因または関連して発生するいかなる請求、損害、またはその他の責任についても責任を負わないものとします。

A
license - permissive license
-
quality - not tested
D
maintenance

Maintenance

Maintainers
Response time
Release cycle
Releases (12mo)
Commit activity

Resources

Unclaimed servers have limited discoverability.

Looking for Admin?

If you are the server author, to access and configure the admin panel.

Related MCP Servers

  • A
    license
    B
    quality
    F
    maintenance
    Enables AI models to interact with PostgreSQL databases through a standardized interface, supporting operations like queries, table manipulation, and schema inspection.
    6
    505
    16
    MIT
  • A
    license
    -
    quality
    D
    maintenance
    Enables AI agents to query, explore, and analyze PostgreSQL databases through the Model Context Protocol. It provides robust security features including read-only mode, schema restrictions, and query timeouts for safe data interaction.
    11
    MIT
  • F
    license
    -
    quality
    D
    maintenance
    Enables AI assistants to interact with PostgreSQL databases by executing SQL queries and inspecting database schemas. It provides tools to facilitate database exploration and management through the Model Context Protocol.
  • A
    license
    A
    quality
    C
    maintenance
    Enables AI assistants to query PostgreSQL databases, inspect schemas, and retrieve complete DDL with built-in read-only protection. It supports multiple database connections and allows for secure database interaction and exploration via the Model Context Protocol.
    5
    23
    1
    MIT

View all related MCP servers

Related MCP Connectors

View all MCP Connectors

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/gldc/mcp-postgres'

If you have feedback or need assistance with the MCP directory API, please join our Discord server