mcp-postgres
High-performance MCP (Model Context Protocol) server for PostgreSQL, written in pure Rust with async/await.
Overview
mcp-postgres brings your PostgreSQL database into Claude and other MCP-compatible AI tools. Execute queries, manage schema, monitor performance, and handle bulk operations—all through a clean JSON-RPC interface.
- 46 PostgreSQL tools — query execution, schema inspection, DDL operations, batch operations, monitoring, maintenance, replication, and more
- Dual-protocol transport — TCP (port 3000) and HTTP/2 (port 3001) for flexibility
- Sub-10ms latency — optimized for interactive AI workflows
- Production-grade — connection pooling, health checks, input validation, SQL injection prevention
- Stateless HTTP — each request is independent (no transaction state across requests)
- Claude Desktop ready — works with stdio transport for seamless integration
Quick Start
Installation
See INSTALLATION.md for complete instructions covering:
- crates.io, source build, and Homebrew installation
- Configuration and verification
- Claude Desktop setup
- Troubleshooting
Quick start:
# From crates.io (easiest)
# Or from Homebrew (macOS)
Run
# TCP server (default, port 3000)
# HTTP/2 server (port 3001)
# Stdio mode for Claude Desktop
# Restricted (read-only) mode
Claude Desktop Configuration
Add to claude_desktop_config.json:
See guides/INSTALLATION.md for complete setup and troubleshooting.
Command-Line Options
Usage: mcp-postgres [OPTIONS]
Options:
-d, --database-url <URL> PostgreSQL connection string
-H, --host <HOST> Server host (TCP) [default: 127.0.0.1]
-p, --port <PORT> TCP server port [default: 3000]
--http-port <PORT> HTTP/2 server port [default: 3001]
--min-connections <N> Min pool connections [default: 5]
--max-connections <N> Max pool connections [default: 20]
--log-level <LEVEL> Log level [default: info]
--enable-metrics Enable Prometheus /metrics endpoint
--metrics-port <PORT> Metrics port [default: 9090]
--stdio Stdio mode (Claude Desktop)
--access-mode <MODE> unrestricted or restricted [default: unrestricted]
-h, --help Print help
-V, --version Print version
Protocol & API
All tools follow the MCP JSON-RPC 2.0 specification.
Request Format
Response Format (Success)
Response Format (Error)
Tools Reference (46 Total)
Query Execution (6 tools)
execute_query — Execute SELECT and return rows
→
execute_insert — Execute INSERT and return rows affected
→
execute_update — Execute UPDATE and return rows affected
→
execute_delete — Execute DELETE and return rows affected
→
explain_query — Show query execution plan
→
async_* variants — High-performance versions with temporary sync commit disabled
// for async_execute_insert, async_execute_update, async_execute_delete
Schema Inspection (8 tools)
list_tables — List all tables in database
→
list_schemas — List all schemas
→
list_columns — List columns in a table
→
list_indexes — List all indexes
→
list_triggers — List triggers on a table
→
list_views — List all views
→
list_sequences — List all sequences
→
describe_table — Get detailed table metadata
→
DDL Operations (16 tools)
Create, modify, and drop database objects safely.
create_table — Create a new table
drop_table — Drop a table
create_view — Create a view
drop_view — Drop a view
alter_view — Rename a view
create_schema — Create a schema
drop_schema — Drop a schema
create_index — Create an index
drop_index — Drop an index
alter_index — Rename an index
create_sequence — Create a sequence
drop_sequence — Drop a sequence
create_partition — Create table partition
delete_table_partition — Drop a partition
list_partitions — List partitions on a table
→
backup_table — Create a backup copy of a table
→ Creates table: backup_users with all data
Batch Operations (4 tools)
High-performance bulk DML. Max 1000 rows per request.
async_batch_insert — Insert multiple rows
→
async_batch_update — Update multiple rows with different conditions
→
async_batch_delete — Delete multiple rows
→
async_batch_insert_copy — Bulk insert with configurable batch size
→
Monitoring & Analysis (6 tools)
list_connections — Show active database connections
→
show_current_user — Show current user and database
→
analyze_table — Update table statistics
→
vacuum_table — Clean dead tuples and optimize
→
get_table_size — Get table size in bytes and human-readable format
→
get_database_size — Get total database size
→
Connection & Security (4 tools)
show_running_queries — Show all non-idle queries
→
list_users — List all database users
→
list_user_privileges — List privileges for a user
→
show_session_info — Show current session details
→
Configuration (2 tools)
show_all_settings — List all PostgreSQL settings
→
get_setting — Get a specific setting
→
Architecture
Dual-Protocol Design
┌─────────────────┐ ┌─────────────────┐
│ TCP Client │ │ HTTP Client │
│ (port 3000) │ │ (port 3001) │
└────────┬────────┘ └────────┬────────┘
│ │
└─────────────┬─────────────┘
│
┌────────┴────────┐
│ JSON-RPC 2.0 │
│ (MCP Protocol) │
└────────┬────────┘
│
┌────────────┴────────────┐
│ Tool Dispatcher │
│ (46 tools) │
└────────────┬────────────┘
│
┌──────────────┴──────────────┐
│ Connection Pool │
│ (deadpool-postgres) │
│ Min: 5, Max: 20 │
└────────────┬────────────────┘
│
┌────────┴────────┐
│ PostgreSQL DB │
└─────────────────┘
Key Design Decisions
- Stateless HTTP — Each request is independent. Transaction tools (BEGIN, COMMIT, ROLLBACK) not available over HTTP.
- Connection pooling — Deadpool maintains 5-20 connections with health checks and idle timeouts.
- Sub-10ms latency — Optimized for interactive AI workflows. TCP: < 10ms, HTTP: < 10ms (> 20ms is unacceptable).
- Input validation — All parameters validated at system boundary:
- SQL statements: max 10,000 characters
- Identifiers: max 255 characters
- Batch rows: max 1,000 per request
- SQL injection prevention via identifier validation
License
Apache-2.0
Support
For issues, questions, or tool requests: GitHub Issues