MCP server for PostgreSQL database operations - query, explore schemas, and analyze tables
Project description
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
# Install
pipx install postgresql-mcp
# Configure Claude Code
claude mcp add postgres -s user \
-e POSTGRES_HOST=localhost \
-e POSTGRES_USER=your_user \
-e POSTGRES_PASSWORD=your_password \
-e POSTGRES_DB=your_database \
-- postgresql-mcp
Full Installation Guide - Includes database permissions setup, remote connections, and troubleshooting.
Available Tools (14 total)
Query Execution
| Tool | Description |
|---|---|
query |
Execute read-only SQL queries against the database |
execute |
Execute write operations (INSERT/UPDATE/DELETE) when enabled |
explain_query |
Get EXPLAIN plan for query optimization |
Schema Exploration
| Tool | Description |
|---|---|
list_schemas |
List all schemas in the database |
list_tables |
List tables in a specific schema |
describe_table |
Get table structure (columns, types, constraints) |
list_views |
List views in a schema |
describe_view |
Get view definition and columns |
list_functions |
List functions and procedures |
Performance & Analysis
| Tool | Description |
|---|---|
table_stats |
Get table statistics (row count, size, bloat) |
list_indexes |
List indexes for a table |
list_constraints |
List constraints (PK, FK, UNIQUE, CHECK) |
Database Info
| Tool | Description |
|---|---|
get_database_info |
Get database version and connection info |
search_columns |
Search for columns by name across all tables |
MCP Prompts
Guided workflows that help Claude assist you effectively:
| Prompt | Description |
|---|---|
explore_database |
Comprehensive database exploration and overview |
query_builder |
Help building efficient queries for a table |
performance_analysis |
Analyze table performance and suggest optimizations |
data_dictionary |
Generate documentation for a schema |
MCP Resources
Browsable database structure:
| Resource URI | Description |
|---|---|
postgres://schemas |
List all schemas |
postgres://schemas/{schema}/tables |
Tables in a schema |
postgres://schemas/{schema}/tables/{table} |
Table details |
postgres://database |
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
SecretStr - Credentials 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)
pipx install postgresql-mcp
# or
pip install postgresql-mcp
From Source
git clone https://github.com/JaviMaligno/postgres_mcp.git
cd postgres_mcp
uv sync
Configuration
Claude Code CLI (Recommended)
claude mcp add postgres -s user \
-e POSTGRES_HOST=localhost \
-e POSTGRES_PORT=5432 \
-e POSTGRES_USER=your_user \
-e POSTGRES_PASSWORD=your_password \
-e POSTGRES_DB=your_database \
-- postgresql-mcp
Cursor IDE
Add to ~/.cursor/mcp.json:
{
"mcpServers": {
"postgres": {
"command": "postgresql-mcp",
"env": {
"POSTGRES_HOST": "localhost",
"POSTGRES_PORT": "5432",
"POSTGRES_USER": "your_user",
"POSTGRES_PASSWORD": "your_password",
"POSTGRES_DB": "your_database"
}
}
}
}
Environment Variables
| Variable | Required | Default | Description |
|---|---|---|---|
POSTGRES_HOST |
Yes | localhost | Database host |
POSTGRES_PORT |
No | 5432 | Database port |
POSTGRES_USER |
Yes | postgres | Database user |
POSTGRES_PASSWORD |
Yes | - | Database password |
POSTGRES_DB |
Yes | postgres | Database name |
POSTGRES_SSLMODE |
No | prefer | SSL mode |
ALLOW_WRITE_OPERATIONS |
No | false | Enable write operations |
QUERY_TIMEOUT |
No | 30 | Query timeout (seconds) |
MAX_ROWS |
No | 1000 | Maximum rows returned |
Development
Requirements
- Python 3.10+
- uv for dependency management
- PostgreSQL for integration tests
Setup
git clone https://github.com/JaviMaligno/postgres_mcp.git
cd postgres_mcp
uv sync
Running Tests
# Unit tests (no database required)
uv run pytest tests/test_security.py tests/test_settings.py tests/test_models.py -v
# All tests (requires PostgreSQL)
uv run pytest -v --cov=postgres_mcp
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 (
v*): Tests, builds, and publishes to PyPI
To release a new version:
# 1. Update version in postgres_mcp/__version__.py
# 2. Commit and push
git add -A && git commit -m "release: v0.2.0"
git push origin main
# 3. Create and push tag (triggers PyPI publish)
git tag v0.2.0
git push origin v0.2.0
Troubleshooting
Connection Issues
# Verify PostgreSQL is running
pg_isready -h localhost -p 5432
# Test connection with psql
psql -h localhost -U your_user -d your_database
Permission Denied
Ensure your database user has SELECT permissions:
GRANT SELECT ON ALL TABLES IN SCHEMA public TO your_user;
MCP Server Not Connecting
# Check server status
claude mcp get postgres
# Test server directly
postgresql-mcp # Should wait for MCP messages
Links
License
MIT
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file postgresql_mcp-0.2.0.tar.gz.
File metadata
- Download URL: postgresql_mcp-0.2.0.tar.gz
- Upload date:
- Size: 30.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
185aea3e3b4eeddb2f0f7c37ead4cc80dc3e7142aeb0c4a61ab9cd39d6332d70
|
|
| MD5 |
47aa45ad85ccd3eac37306dd0f3f0ffe
|
|
| BLAKE2b-256 |
586ae0f0b7450bd0ee00e8c2f68dff6e9273238ffe126579400a2bab3bd12b64
|
Provenance
The following attestation bundles were made for postgresql_mcp-0.2.0.tar.gz:
Publisher:
publish.yml on JaviMaligno/postgres_mcp
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
postgresql_mcp-0.2.0.tar.gz -
Subject digest:
185aea3e3b4eeddb2f0f7c37ead4cc80dc3e7142aeb0c4a61ab9cd39d6332d70 - Sigstore transparency entry: 765396824
- Sigstore integration time:
-
Permalink:
JaviMaligno/postgres_mcp@89dd80ed3de50f2f1a934d27afd062a2f5104636 -
Branch / Tag:
refs/tags/v0.2.0 - Owner: https://github.com/JaviMaligno
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@89dd80ed3de50f2f1a934d27afd062a2f5104636 -
Trigger Event:
push
-
Statement type:
File details
Details for the file postgresql_mcp-0.2.0-py3-none-any.whl.
File metadata
- Download URL: postgresql_mcp-0.2.0-py3-none-any.whl
- Upload date:
- Size: 22.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
82da2db70d1b3a2ddb32676645e90593f521e335f21c6ab9b04499c167c4dd7c
|
|
| MD5 |
9cc5448ca272afea6c35a7dbe2bc8264
|
|
| BLAKE2b-256 |
bf388020988afa61e8ae47a1d937bb0f82d84a8c841fd3ad4a67f9ad6a3c07af
|
Provenance
The following attestation bundles were made for postgresql_mcp-0.2.0-py3-none-any.whl:
Publisher:
publish.yml on JaviMaligno/postgres_mcp
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
postgresql_mcp-0.2.0-py3-none-any.whl -
Subject digest:
82da2db70d1b3a2ddb32676645e90593f521e335f21c6ab9b04499c167c4dd7c - Sigstore transparency entry: 765396831
- Sigstore integration time:
-
Permalink:
JaviMaligno/postgres_mcp@89dd80ed3de50f2f1a934d27afd062a2f5104636 -
Branch / Tag:
refs/tags/v0.2.0 - Owner: https://github.com/JaviMaligno
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@89dd80ed3de50f2f1a934d27afd062a2f5104636 -
Trigger Event:
push
-
Statement type: