mcp-postgres
mcp-postgres
PostgreSQL用MCPサーバー。Model Context Protocolを通じて、AIエージェントにスキーマインテリジェンス、クエリ実行、DBAツール機能を提供します。
汎用的なデータベースMCPサーバーとは異なり、mcp-postgresはPostgresネイティブです。テーブルやカラムのコメントを抽出し、Postgres固有のカタログビューを理解し、インデックス分析を提供します。また、LLMに無制限のデータベースアクセス権を与えないよう、アクセスレベルを設定可能です。
特徴
スキーマインテリジェンス
スキーマ、テーブル、ビューをサイズと行数付きで一覧表示
テーブルの詳細説明:カラム、型、制約、インデックス、外部キー
COMMENT ONメタデータの抽出 — カラムの意味に関するセマンティックなコンテキストをLLMに提供データベース全体で名前やコメントによるオブジェクト検索
クエリ実行
自動行制限付きの読み取り専用
queryツールアクセスレベルによって制御される書き込み可能な
executeツール人間が読みやすい出力形式の
EXPLAIN ANALYZE
DBAツール
テーブル統計:ライブ/デッドタプル、肥大化率、バキューム履歴、スキャンパターン
インデックス分析:使用統計、未使用インデックスの検出、不足インデックスの提案
データベースの健全性:接続数、キャッシュヒット率、長時間実行クエリ、スループット
安全性
4つのアクセスレベル:
readonly、readwrite、admin、unrestrictedSQLステートメントの分類(SELECT、DML、DDL、admin)と強制適用
stderrへの監査ログ出力(JSON形式、クエリごとに1エントリ)
Related MCP server: Postgres Scout MCP
クイックスタート
npx mcp-postgres --connection-string "postgres://user:pass@localhost:5432/mydb"または環境変数を使用する場合:
DATABASE_URL="postgres://user:pass@localhost:5432/mydb" npx mcp-postgresClaude Desktop
claude_desktop_config.json に追加します:
{
"mcpServers": {
"postgres": {
"command": "npx",
"args": [
"-y",
"mcp-postgres",
"--connection-string",
"postgres://user:pass@localhost:5432/mydb"
]
}
}
}Claude Code
プロジェクトの .mcp.json に追加します:
{
"mcpServers": {
"postgres": {
"command": "npx",
"args": ["-y", "mcp-postgres"],
"env": {
"DATABASE_URL": "postgres://user:pass@localhost:5432/mydb"
}
}
}
}ツール
ツール | 説明 | アクセス |
| テーブル数とサイズを含むスキーマ一覧 | readonly |
| コメント、行数、サイズを含むテーブル一覧 | readonly |
| カラム、インデックス、FK、コメントを含むテーブル詳細 | readonly |
| 名前またはコメントによるオブジェクト検索 | readonly |
| SELECTクエリの実行 | readonly |
| INSERT/UPDATE/DELETE/CREATE等の実行 | 可変 |
| 読みやすい出力形式の EXPLAIN (ANALYZE) | readonly* |
| テーブル統計、肥大化、バキューム情報 | readonly |
| インデックス使用状況、未使用インデックス、不足インデックスのヒント | readonly |
| 接続数、キャッシュ率、長時間クエリ、肥大化 | readonly |
* analyze=true を指定した explain_query はクエリを実行するため、ステートメントのアクセスレベルに従います。
リソース
URI | 説明 |
| スキーマの完全なDDL(コメント付きのCREATE TABLE文) |
| インストール済みのPostgreSQL拡張機能 |
プロンプト
プロンプト | 説明 |
| ガイド付きデータベース探索 — スキーマ、テーブル、リレーションシップ |
| EXPLAIN、インデックス、推奨事項を用いた低速クエリの分析 |
| 包括的なデータベース健全性評価 |
設定
CLIオプション
--connection-string PostgreSQL connection URL
--access-level readonly|readwrite|admin|unrestricted (default: readonly)
--row-limit Max rows returned per query (default: 500)
--schema Default schema filter (default: public)
--audit Enable query audit logging to stderr環境変数
変数 | 説明 |
| PostgreSQL接続URL |
| 代替接続URL |
| アクセスレベルのオーバーライド |
| 行制限のオーバーライド |
アクセスレベル
レベル | SELECT | INSERT/UPDATE/DELETE | CREATE/ALTER/DROP | TRUNCATE/DROP DATABASE |
| はい | いいえ | いいえ | いいえ |
| はい | はい | いいえ | いいえ |
| はい | はい | はい | いいえ |
| はい | はい | はい | はい |
デフォルトは readonly です。必要な最小限のレベルを使用してください。
監査ログ
--audit で有効にします。すべてのツール呼び出しをJSONとしてstderrに記録します:
{"timestamp":"2026-04-03T12:00:00.000Z","tool":"query","sql":"SELECT * FROM users","statementType":"select","accessLevel":"readonly","allowed":true,"durationMs":12,"rowCount":42}stderrをファイルにパイプしてキャプチャします:mcp-postgres --audit 2>audit.log
アーキテクチャ
src/
├── index.ts Entry point and CLI
├── server.ts MCP server setup
├── config.ts Configuration parsing
├── db/
│ ├── pool.ts Connection pool management
│ └── query.ts Query execution with timing
├── tools/
│ ├── schema.ts Schema exploration tools
│ ├── query.ts Query execution tools
│ └── performance.ts DBA and health tools
├── resources/
│ └── schema.ts Schema DDL resources
├── prompts/
│ └── index.ts Prompt templates
└── safety/
├── classifier.ts SQL statement classification
├── access.ts Access level enforcement
└── audit.ts Audit logging開発
npm install
npm test # run tests
npm run build # compile TypeScript
npm run dev -- --connection-string "postgres://..." # run in dev modeライセンス
MIT
This server cannot be deployed
Maintenance
Related MCP Connectors
Safe, read-only Postgres and MySQL access for AI agents. Audit log + column-level controls.
Query PostgreSQL databases in plain English — LLM-generated, safety-validated SQL.
Deterministic safety, correctness & cost gate that vets Postgres SQL before your AI agent runs it.
PostgreSQL, MySQL, OpenAPI/Swagger, and shared Agent Memory with scoped access.
Related MCP Servers
- AlicenseNot gradedqualityAmaintenanceEnables secure, AI-driven PostgreSQL database administration, observability, and querying with support for extensions like PostGIS and pgvector, connection pooling, and advanced tool filtering.72 npm12MIT
- AlicenseNot gradedqualityDmaintenanceEnables AI assistants to safely explore, analyze, and maintain PostgreSQL databases with read-only mode by default, SQL injection prevention, query performance analysis, and optional write operations.52 npmApache 2.0
- FlicenseNot gradedqualityDmaintenanceEnables AI assistants to safely interact with PostgreSQL databases, perform queries, inspect schemas, and analyze query performance.2-
- AlicenseNot gradedqualityBmaintenanceEnables AI assistants to securely interact with PostgreSQL databases, offering 30+ tools, role-based access control, and security guardrails.2MIT