Skip to main content
Glama

PostgreSQL MCP Server

by gldc

PostgreSQL MCP サーバー

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

特徴

  • データベーススキーマの一覧表示
  • スキーマ内のテーブルを一覧表示する
  • テーブル構造を説明する
  • テーブルの制約と関係を一覧表示する
  • 外部キー情報を取得する
  • SQLクエリを実行する

クイックスタート

# 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

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

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

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

-
security - not tested
A
license - permissive license
-
quality - not tested

remote-capable server

The server can be hosted and run remotely because it primarily relies on remote services or has no dependency on the local environment.

AI エージェントがモデル コンテキスト プロトコルを介して PostgreSQL データベースと対話できるようにし、データベース スキーマの探索、テーブル構造の検査、SQL クエリ実行機能を提供します。

  1. 特徴
    1. クイックスタート
      1. インストール
        1. Smithery経由でインストール
        2. 手動インストール
      2. 使用法
        1. Dockerで実行する
        2. mcp.json による設定
      3. 安全
        1. 貢献
          1. 関連プロジェクト
            1. ライセンス

              Related MCP Servers

              • -
                security
                F
                license
                -
                quality
                A server that allows AI models to interact with PostgreSQL databases through a standardized protocol, providing database schema information and SQL query execution capabilities.
                Last updated -
                JavaScript
              • -
                security
                A
                license
                -
                quality
                A Model Context Protocol server that enables powerful PostgreSQL database management capabilities including analysis, schema management, data migration, and monitoring through natural language interactions.
                Last updated -
                355
                46
                TypeScript
                AGPL 3.0
                • Linux
                • Apple
              • A
                security
                A
                license
                A
                quality
                Enables AI models to interact with PostgreSQL databases through a standardized interface, supporting operations like queries, table manipulation, and schema inspection.
                Last updated -
                5
                127
                2
                JavaScript
                MIT License
              • -
                security
                F
                license
                -
                quality
                A Model Context Protocol server that enables interaction with PostgreSQL databases to list tables, retrieve schemas, and execute read-only SQL queries.
                Last updated -
                16,948
                JavaScript
                • Linux
                • Apple

              View all related MCP servers

              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