Enables comprehensive PostgreSQL database operations including query execution, schema exploration, table analysis, performance optimization with EXPLAIN plans, and browsable database structure as MCP resources. Provides read-only access by default with optional write operations, SQL injection prevention, and tools for searching columns, analyzing statistics, and generating data dictionaries.
PostgreSQL MCP Server
MCP server for PostgreSQL database operations. Works with Claude Code, Claude Desktop, and any MCP-compatible client.
Features
Query Execution: Execute SQL queries with read-only protection by default
Schema Exploration: List schemas, tables, views, and functions
Table Analysis: Describe structure, indexes, constraints, and statistics
Performance Tools: EXPLAIN queries and analyze table health
Security First: SQL injection prevention, credential protection, read-only by default
MCP Prompts: Guided workflows for exploration, query building, and documentation
MCP Resources: Browsable database structure as markdown
Quick Start
Full Installation Guide - Includes database permissions setup, remote connections, and troubleshooting.
Available Tools (14 total)
Query Execution
Tool | Description |
| Execute read-only SQL queries against the database |
| Execute write operations (INSERT/UPDATE/DELETE) when enabled |
| Get EXPLAIN plan for query optimization |
Schema Exploration
Tool | Description |
| List all schemas in the database |
| List tables in a specific schema |
| Get table structure (columns, types, constraints) |
| List views in a schema |
| Get view definition and columns |
| List functions and procedures |
Performance & Analysis
Tool | Description |
| Get table statistics (row count, size, bloat) |
| List indexes for a table |
| List constraints (PK, FK, UNIQUE, CHECK) |
Database Info
Tool | Description |
| Get database version and connection info |
| Search for columns by name across all tables |
MCP Prompts
Guided workflows that help Claude assist you effectively:
Prompt | Description |
| Comprehensive database exploration and overview |
| Help building efficient queries for a table |
| Analyze table performance and suggest optimizations |
| Generate documentation for a schema |
MCP Resources
Browsable database structure:
Resource URI | Description |
| List all schemas |
| Tables in a schema |
| Table details |
| Database connection info |
Example Usage
Once configured, ask Claude to:
Schema Exploration:
"List all tables in the public schema"
"Describe the users table structure"
"What views are available?"
Querying:
"Show me 10 rows from the orders table"
"Find all customers who placed orders last week"
"Count records grouped by status"
Performance Analysis:
"What indexes exist on the orders table?"
"Analyze the performance of the users table"
"Explain this query: SELECT * FROM orders WHERE created_at > '2024-01-01'"
Documentation:
"Generate a data dictionary for this database"
"What columns contain 'email' in their name?"
Security
This MCP server implements multiple security layers:
Read-Only by Default
Write operations (INSERT, UPDATE, DELETE) are blocked unless explicitly enabled via ALLOW_WRITE_OPERATIONS=true.
SQL Injection Prevention
All queries are validated before execution
Dangerous operations (DROP DATABASE, etc.) are always blocked
Multiple statements are not allowed
SQL comments are blocked
Credential Protection
Passwords stored using Pydantic's
SecretStrCredentials never appear in logs or error messages
Query Limits
Results limited by
MAX_ROWS(default: 1000)Query timeout configurable via
QUERY_TIMEOUT
Installation Options
From PyPI (Recommended)
From Source
Configuration
Claude Code CLI (Recommended)
Cursor IDE
Add to ~/.cursor/mcp.json:
Environment Variables
Variable | Required | Default | Description |
| Yes | localhost | Database host |
| No | 5432 | Database port |
| Yes | postgres | Database user |
| Yes | - | Database password |
| Yes | postgres | Database name |
| No | prefer | SSL mode |
| No | false | Enable write operations |
| No | 30 | Query timeout (seconds) |
| No | 1000 | Maximum rows returned |
Development
Requirements
Python 3.10+
uv for dependency management
PostgreSQL for integration tests
Setup
Running Tests
CI/CD Pipeline
The project uses GitHub Actions:
Every push to main: Runs tests on Python 3.10, 3.11, 3.12
Pull requests: Full test suite
Tags (: Tests, builds, and publishes to PyPI
To release a new version:
Troubleshooting
Connection Issues
Permission Denied
Ensure your database user has SELECT permissions:
MCP Server Not Connecting
Links
License
MIT