postgres-mcp-server

postgres-mcp-server

Enables AI assistants to securely interact with PostgreSQL databases through a standardized MCP interface, supporting SQL queries, schema inspection, and database management.

Category
访问服务器

README

PostgreSQL MCP Server

A production-ready Model Context Protocol server for PostgreSQL database operations. Enables AI assistants to securely interact with PostgreSQL databases through a standardized interface.

Features

  • 11 MCP Tools - list_tables, describe_table, execute_select, execute_query, list_schemas, list_views, explain_query, database_info, health_check, connection_status, metrics
  • Security First - SQL injection detection, blocked dangerous commands, read-only mode, rate limiting
  • Async Everywhere - asyncpg connection pooling with automatic reconnect and retry
  • Schema Caching - TTL-based caching for metadata queries
  • Structured Logging - structlog with JSON or console output
  • Metrics - query counts, execution times, security violations
  • Type Safe - Pydantic v2 models, strict mypy typing
  • Production Ready - graceful shutdown, signal handling, connection health checks

Prerequisites

  • Python 3.12+
  • uv package manager
  • PostgreSQL 14+

Installation

# Clone and enter the project
cd postgres-mcp-server

# Create environment and install dependencies
uv sync

# Configure environment
cp .env.example .env
# Edit .env with your PostgreSQL connection details

Configuration

Variable Default Description
POSTGRES_HOST localhost PostgreSQL host
POSTGRES_PORT 5432 PostgreSQL port
POSTGRES_DATABASE postgres Database name
POSTGRES_USER postgres Database user
POSTGRES_PASSWORD Database password
POSTGRES_MIN_CONNECTIONS 1 Min pool connections
POSTGRES_MAX_CONNECTIONS 10 Max pool connections
READ_ONLY true Restrict to SELECT only
ALLOW_WRITE false Enable write operations
MAX_ROWS 1000 Max rows returned per query
QUERY_TIMEOUT 30 Query timeout in seconds
RATE_LIMIT 60 Max requests per window
RATE_WINDOW 60 Rate limit window in seconds
LOG_LEVEL INFO Log level (DEBUG, INFO, WARNING, ERROR)
LOG_JSON false Output logs as JSON
CACHE_TTL 300 Schema cache TTL in seconds
RETRY_MAX_ATTEMPTS 3 Max connection retries
METRICS_ENABLED true Enable metrics collection

Running the Server

# Start with stdio transport (default for MCP)
uv run python -m src.server

# Or directly
uv run src/server.py

Connecting with MCP Clients

Claude Desktop

Add to your claude_desktop_config.json:

{
  "mcpServers": {
    "postgres": {
      "command": "uv",
      "args": [
        "--directory",
        "/path/to/postgres-mcp-server",
        "run",
        "src/server.py"
      ],
      "env": {
        "POSTGRES_HOST": "localhost",
        "POSTGRES_PORT": "5432",
        "POSTGRES_DATABASE": "yourdb",
        "POSTGRES_USER": "youruser",
        "POSTGRES_PASSWORD": "yourpassword"
      }
    }
  }
}

Cursor

In Cursor settings, add a new MCP server:

{
  "mcpServers": {
    "postgres": {
      "command": "uv",
      "args": [
        "--directory",
        "/path/to/postgres-mcp-server",
        "run",
        "src/server.py"
      ],
      "env": {
        "POSTGRES_HOST": "localhost",
        "POSTGRES_PORT": "5432",
        "POSTGRES_DATABASE": "yourdb",
        "POSTGRES_USER": "youruser",
        "POSTGRES_PASSWORD": "yourpassword"
      }
    }
  }
}

OpenCode

In opencode.json:

{
  "mcpServers": {
    "postgres": {
      "command": "uv",
      "args": [
        "--directory",
        "/path/to/postgres-mcp-server",
        "run",
        "src/server.py"
      ],
      "env": {
        "POSTGRES_HOST": "localhost",
        "POSTGRES_PORT": "5432",
        "POSTGRES_DATABASE": "yourdb",
        "POSTGRES_USER": "youruser",
        "POSTGRES_PASSWORD": "yourpassword"
      }
    }
  }
}

VS Code MCP Extension

In VS Code settings (settings.json):

{
  "mcp.server": {
    "postgres": {
      "command": "uv",
      "args": [
        "--directory",
        "/path/to/postgres-mcp-server",
        "run",
        "src/server.py"
      ],
      "env": {
        "POSTGRES_HOST": "localhost",
        "POSTGRES_PORT": "5432",
        "POSTGRES_DATABASE": "yourdb",
        "POSTGRES_USER": "youruser",
        "POSTGRES_PASSWORD": "yourpassword"
      }
    }
  }
}

Available Tools

Tool Description
list_tables List all tables, optionally filtered by schema
describe_table Get table schema with columns, types, constraints, indexes
execute_select Run SELECT queries with parameters and pagination
execute_query Run INSERT/UPDATE/DELETE (requires write mode)
list_schemas List all database schemas
list_views List all views, optionally filtered by schema
explain_query Get PostgreSQL execution plan for a query
database_info Get database version, encoding, timezone
health_check Check database connection health
connection_status Get connection pool status
metrics Get server metrics

Example Usage

# List all tables
result = await list_tables()

# Describe a table
result = await describe_table(schema="public", table="users")

# Execute a SELECT query
result = await execute_select(
    query="SELECT * FROM users WHERE active = $1",
    params=[True],
    limit=50
)

# Get database info
result = await database_info()

# Explain a query
result = await explain_query("SELECT * FROM users JOIN orders ON users.id = orders.user_id")

# Check health
result = await health_check()

# Get metrics
result = await metrics()

Security

  • Blocked commands: DROP, ALTER, TRUNCATE, CREATE DATABASE, DROP DATABASE, COPY, VACUUM, GRANT, REVOKE, CREATE EXTENSION
  • SQL injection detection: Pattern-based detection of common injection vectors
  • Rate limiting: Configurable request throttling
  • Query timeout: Automatic cancellation of long-running queries
  • Row limits: Maximum rows returned per query
  • Read-only mode: Restrict all write operations

Project Structure

postgres-mcp-server/
├── src/
│   ├── __init__.py
│   ├── server.py      # FastMCP server entry point
│   ├── config.py      # Environment configuration
│   ├── database.py    # asyncpg connection pool & queries
│   ├── tools.py       # MCP tool implementations
│   ├── security.py    # SQL validation & security
│   ├── models.py      # Pydantic data models
│   ├── utils.py       # Utility functions
│   └── logger.py      # structlog setup
├── tests/
│   ├── __init__.py
│   ├── test_security.py
│   ├── test_models.py
│   ├── test_database.py
│   └── test_utils.py
├── .env.example
├── pyproject.toml
└── README.md

Development

# Install dev dependencies
uv sync --dev

# Run tests
uv run pytest

# Run linting
uv run ruff check src/

# Run type checking
uv run mypy src/

# Run formatting check
uv run black --check src/

Troubleshooting

Connection refused

Ensure PostgreSQL is running and accessible from the server host.

Authentication failed

Verify POSTGRES_USER and POSTGRES_PASSWORD in .env.

Timeout errors

Increase QUERY_TIMEOUT or optimize your queries.

Security violations

Queries containing blocked commands or suspicious patterns are rejected. Use execute_select for SELECT queries.

Pool exhaustion

Increase POSTGRES_MAX_CONNECTIONS if you see pool acquisition timeouts.

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

官方
精选