postgres-mcp

postgres-mcp

MCP server for PostgreSQL database management, enabling AI-assisted schema exploration, stored procedure analysis, test data generation, and safe transaction management via Claude Desktop.

Category
访问服务器

README

PostgreSQL Manager + MCP Server

A complete Python application for PostgreSQL database management with two modes:

  • Desktop GUI (Tkinter) — for direct human use
  • MCP Server — for AI-assisted database analysis via Claude Desktop

What It Does

🔌 Connection Management

  • Save multiple named PostgreSQL connection profiles in config.ini
  • Test connections before saving
  • Fields: host, port, database, username, password, SSL mode

🌲 Object Explorer

  • Browse: Schemas → Tables → Columns (with types, nullability, PK markers)
  • Browse: Schemas → Views
  • Double-click a table → auto-generates SELECT * FROM ... LIMIT 100
  • Right-click → Select Top 100, Show Columns

📝 SQL Query Editor

  • Syntax highlighting for SQL keywords
  • Press F5 to execute (or click Run)
  • Query history (session-based, navigate with ↑/↓ buttons)
  • Select specific text to run only that portion

📊 Results Grid

  • Scrollable table with column headers
  • Row count + elapsed time display
  • Export to CSV with one click

🧪 SP Analyzer + Test Data Generator

Upload a .sql file containing a stored procedure or function:

  1. Analyse — extracts SELECT queries, detects referenced tables, infers WHERE conditions
  2. Generate Preview — creates Faker-based test data respecting types, FKs, enums, UNIQUE
  3. Insert Test Data — inserts into an open transaction (NOT committed)
  4. Run your SP manually in the query editor to verify
  5. Commit or Rollback — on-demand, manual control

🤖 MCP Server (AI-Powered Mode)

When connected to Claude Desktop, Claude can:

  • Fetch SP bodies directly from pg_proc
  • Execute SELECT queries found in the SP
  • Recursively follow SQL strings stored inside DB column values (the killer feature)
  • Understand schema, FKs, enums, CHECK constraints
  • Generate and insert test data in FK-dependency order
  • Wait for you to verify, then commit or rollback

Prerequisites

Mandatory

Requirement Version Check Command
Python 3.10+ python --version
pip any pip --version
PostgreSQL 10+ (any server) psql --version

Optional (for MCP mode)

Requirement Purpose
Claude Desktop AI client that connects to our MCP server

⚠️ Python must be in your system PATH. Run python --version in a terminal to verify.


Installation

Option 1: Run the installer script (Windows)

install.bat

This checks for Python, creates a virtual environment, and installs all dependencies.

Option 2: Manual

pip install -r requirements.txt

Dependencies installed:

  • psycopg2-binary — PostgreSQL driver
  • sqlparse — SQL statement parser
  • Faker — realistic test data generation
  • mcp[cli] — Anthropic's MCP SDK for the AI server

Running the Application

Desktop GUI

run.bat

Or manually:

python main.py

MCP Server (for Claude Desktop)

python mcp_server.py

With auto-connect:

python mcp_server.py --host localhost --port 5432 --db mydb --user postgres --password secret
python mcp_server.py --profile local
python mcp_server.py --dsn "postgresql://user:pass@host:5432/db"

Configuring Claude Desktop

  1. Copy claude_desktop_config.json to %APPDATA%\Claude\claude_desktop_config.json (or merge into your existing config)
  2. Restart Claude Desktop
  3. The postgres-mcp tools will appear automatically

Example config:

{
  "mcpServers": {
    "postgres-mcp": {
      "command": "python",
      "args": ["C:/Balaji/MyProjects/postgres-mcp/mcp_server.py"],
      "cwd": "C:/Balaji/MyProjects/postgres-mcp"
    }
  }
}

Configuration — config.ini

[app]
theme = clam
font_size = 11
row_limit = 1000
default_test_rows = 10

[profile_local]
name = Local PostgreSQL
host = localhost
port = 5432
database = postgres
username = postgres
password =
ssl_mode = prefer

Add more profiles by duplicating the [profile_xxx] section with a unique name.


MCP Tools Reference

Tool Description
connect_to_postgres Connect with host/port/db/user/password
get_connection_status Check if connected
get_sp_body Fetch SP/function source from pg_proc
list_sps List all SPs/functions in a schema
list_tables List base tables
list_views List views
get_view_definition Get view SQL (discover real tables behind views)
get_table_schema Full metadata: columns, PKs, FKs, UNIQUEs, CHECKs
get_fk_reference_values Read valid values from FK parent tables
get_enum_values List valid enum labels
sample_table_data Sample existing rows
execute_query Run any SELECT/read SQL
execute_in_transaction Run INSERT/UPDATE/DELETE inside safe test tx
generate_test_data Faker-based row generation
begin_test_transaction Open a write transaction
insert_rows Insert row dicts into the open transaction
commit_test_data Commit permanently
rollback_test_data Roll back all test inserts
transaction_status Check if a test transaction is open

Troubleshooting

Issue Solution
python not found Add Python to system PATH
psycopg2 install fails Install Visual C++ Build Tools, or use psycopg2-binary (already in requirements)
MCP server not showing in Claude Check %APPDATA%\Claude\claude_desktop_config.json path is correct
Connection refused Verify PostgreSQL is running and accepting connections on the configured host:port
ModuleNotFoundError: mcp Run pip install "mcp[cli]>=1.27,<2"

License

MIT

推荐服务器

Baidu Map

Baidu Map

百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。

官方
精选
JavaScript
Playwright MCP Server

Playwright MCP Server

一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。

官方
精选
TypeScript
Magic Component Platform (MCP)

Magic Component Platform (MCP)

一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。

官方
精选
本地
TypeScript
Audiense Insights MCP Server

Audiense Insights MCP Server

通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。

官方
精选
本地
TypeScript
VeyraX

VeyraX

一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。

官方
精选
本地
graphlit-mcp-server

graphlit-mcp-server

模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。

官方
精选
TypeScript
Kagi MCP Server

Kagi MCP Server

一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。

官方
精选
Python
e2b-mcp-server

e2b-mcp-server

使用 MCP 通过 e2b 运行代码。

官方
精选
Neon MCP Server

Neon MCP Server

用于与 Neon 管理 API 和数据库交互的 MCP 服务器

官方
精选
Exa MCP Server

Exa MCP Server

模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。

官方
精选