PostgreSQL MCP Server for Claude Desktop

PostgreSQL MCP Server for Claude Desktop

Enables Claude Desktop to interact with PostgreSQL databases through natural language for schema exploration, data analysis, and query execution. Users can search schemas, describe tables, and perform read or write operations without needing to write manual SQL.

Category
访问服务器

README

🐘 PostgreSQL MCP Server for Claude Desktop

Connect Claude Desktop to your PostgreSQL database using the Model Context Protocol (MCP). Ask Claude questions in plain English and it will query your database, explore schemas, and analyze data — no SQL required.


✨ Features

Category Tools
🗄️ Database Info get_database_info, list_databases
📂 Schema Exploration list_schemas, list_tables, describe_table, search_schema, list_indexes
🔍 Querying execute_query, get_table_sample, get_table_stats, explain_query
✏️ Write Operations execute_write (disabled by default — opt-in via .env)
📄 Resources schema://overview, table://{schema}/{table}
💬 Prompts analyze_table, write_sql_query

🚀 Quick Start

1. Clone the Repository

git clone https://github.com/sarotechhub/Claude-Desktop-to-PostgreSQL.git
cd Cluade-MCP-PostgreSQL

2. Create a Virtual Environment

# Windows
python -m venv .venv
.venv\Scripts\activate

# macOS / Linux
python -m venv .venv
source .venv/bin/activate

3. Install Dependencies

pip install -r requirements.txt

4. Configure Your Database

# Copy the example env file
cp .env.example .env

Edit .env with your PostgreSQL credentials:

DB_HOST=localhost
DB_PORT=5432
DB_NAME=your_database_name
DB_USER=your_username
DB_PASSWORD=your_password

# Set to "true" to allow INSERT/UPDATE/DELETE
ALLOW_WRITE_OPERATIONS=false

All credentials live only in .env — they are never put in the Claude Desktop config file.

5. Test the Server

python main.py

You should see:

🚀 Starting PostgreSQL MCP Server...
✅ Database connection pool ready.

Press Ctrl+C to stop.

6. Connect to Claude Desktop

Find your Claude Desktop config file:

OS Path
Windows %APPDATA%\Claude\claude_desktop_config.json
macOS ~/Library/Application Support/Claude/claude_desktop_config.json
Linux ~/.config/Claude/claude_desktop_config.json

Add the postgres block to the mcpServers section (update the path):

{
  "mcpServers": {
    "postgres": {
      "command": "[PATH_TO_YOUR_VENV_PYTHON_EXE]",
      "args": ["[PATH_TO_YOUR_PROJECT_ROOT]\\main.py"],
      "cwd": "[PATH_TO_YOUR_PROJECT_ROOT]",
      "env": {
        "PYTHONPATH": "[PATH_TO_YOUR_PROJECT_ROOT]"
      }
    }
  }
}

Note: No DB credentials go in this file. The server reads them automatically from your .env file using python-dotenv.

Restart Claude Desktop after saving the config.


💬 Example Conversations with Claude

Once connected, try asking Claude:

"List all tables in my database"
"Describe the structure of the users table"
"Show me 10 sample rows from the orders table"
"How many rows are in each table in the public schema?"
"Find all columns related to 'email' across all tables"
"What indexes exist on the products table?"
"Write a query to find the top 10 customers by total order value"
"Explain why this query might be slow: SELECT * FROM orders WHERE status = 'pending'"

🔧 Tool Reference

Database Info

Tool Description
get_database_info PostgreSQL version, DB size, connection count, server info
list_databases All databases on the server with sizes and encoding

Schema Exploration

Tool Description
list_schemas() All user-defined schemas in the connected database
list_tables(schema) Tables in a schema with row counts and sizes
describe_table(table, schema) Columns, types, nullable, defaults, PKs, FKs
search_schema(keyword) Find tables/columns by keyword (case-insensitive)
list_indexes(table, schema) Indexes on a table with type and columns

Querying

Tool Description
execute_query(sql, limit) Read-only SELECT (enforced via read-only transaction)
get_table_sample(table, schema, limit) Sample rows from a table (max 100)
get_table_stats(table, schema) Row counts, sizes, vacuum/analyze timestamps
explain_query(sql) EXPLAIN execution plan (no data modification)

Write Operations

Tool Description
execute_write(sql, confirm) INSERT/UPDATE/DELETE — requires ALLOW_WRITE_OPERATIONS=true in .env AND confirm=True

🔒 Security

  • Read-only by default — write operations require explicit opt-in in .env
  • Credentials in .env only — never in the Claude Desktop config
  • Read-only transactions — SELECT queries run inside readonly=True transactions
  • Input validation — table/schema names validated with regex to prevent injection
  • No DDL — DROP, CREATE, ALTER are always blocked even when writes are enabled
  • .env excluded from git — credentials never committed

📁 Project Structure

Cluade-MCP-PostgreSQL/
├── main.py                    # 🚀 MCP server entry point (FastMCP + stdio)
├── database.py                # 🔌 Async connection pool (asyncpg)
├── tools/
│   ├── __init__.py
│   ├── schema_tools.py        # list_schemas, list_tables, describe_table, search_schema, list_indexes
│   ├── query_tools.py         # execute_query, get_table_sample, get_table_stats, explain_query
│   ├── write_tools.py         # execute_write (opt-in)
│   └── database_tools.py     # get_database_info, list_databases
├── .env                       # ✅ Your credentials (NOT committed to git)
├── .env.example               # Template for new users
├── .gitignore                 # Excludes .env, .venv, __pycache__
├── requirements.txt           # mcp[cli], asyncpg, python-dotenv, pydantic, orjson
├── claude_desktop_config.json # Example Claude Desktop config snippet
└── README.md

⚙️ Environment Variables

All configuration is done via .env:

Variable Default Description
DB_HOST localhost PostgreSQL host
DB_PORT 5432 PostgreSQL port
DB_NAME Database name
DB_USER Database user
DB_PASSWORD Database password
DB_MIN_CONNECTIONS 1 Min pool connections
DB_MAX_CONNECTIONS 10 Max pool connections
ALLOW_WRITE_OPERATIONS false Enable INSERT/UPDATE/DELETE
LOG_LEVEL INFO Logging level (DEBUG/INFO/WARNING)

🛠️ Troubleshooting

Claude doesn't see the MCP server

  • Fully quit and reopen Claude Desktop after editing the config
  • Check the cwd path — it must point to the project folder
  • Verify the .venv Python path is correct: .venv\Scripts\python.exe

Connection refused / authentication failed

  • Check your .env credentials match your PostgreSQL setup
  • Test directly: psql -h localhost -U your_user -d your_db
  • Ensure PostgreSQL is running: pg_isready

mcp or asyncpg module not found

.venv\Scripts\pip install -r requirements.txt

Server starts but tools don't appear in Claude

  • Open Claude Desktop → Settings → Developer → MCP Servers
  • Check for error messages next to the postgres server entry
  • Run python main.py manually and check stderr for import errors

📋 Requirements

  • Python 3.10+
  • PostgreSQL 12+
  • Claude Desktop (with MCP support)

📄 License

MIT License — free to use, modify, and distribute.


🤝 Contributing

Pull requests welcome! Please fork the repo, create a feature branch, and submit a PR with a clear description.

Claude-Desktop-to-PostgreSQL

推荐服务器

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 模型以安全和受控的方式获取实时的网络信息。

官方
精选