-
Notifications
You must be signed in to change notification settings - Fork 0
MCP Configuration
Configure PostgreSQL MCP Server with Claude Desktop, Cursor, and other MCP clients.
- PostgreSQL Database (version 13-18)
- MCP Client (Claude Desktop, Cursor, etc.)
-
Environment Variable:
DATABASE_URI
Edit your Claude Desktop configuration file:
macOS: ~/Library/Application Support/Claude/claude_desktop_config.json
Windows: %APPDATA%\Claude\claude_desktop_config.json
{
"mcpServers": {
"postgres-mcp": {
"command": "docker",
"args": [
"run",
"-i",
"--rm",
"-e",
"DATABASE_URI=postgresql://username:password@host:5432/database",
"neverinfamous/postgres-mcp:latest",
"--access-mode=restricted"
]
}
}
}Using Python installation:
{
"mcpServers": {
"postgres-mcp": {
"command": "postgres-mcp",
"args": ["--access-mode=restricted"],
"env": {
"DATABASE_URI": "postgresql://username:password@host:5432/database"
}
}
}
}Add to your Cursor settings:
File: Settings → Features → Model Context Protocol
{
"mcpServers": {
"postgres-mcp": {
"command": "docker",
"args": [
"run",
"-i",
"--rm",
"-e",
"DATABASE_URI=postgresql://username:password@host:5432/database",
"neverinfamous/postgres-mcp:latest",
"--access-mode=restricted"
]
}
}
}For any MCP-compatible client:
{
"servers": {
"postgres-mcp": {
"command": "docker",
"args": [
"run", "-i", "--rm",
"-e", "DATABASE_URI=postgresql://user:pass@host:5432/db",
"neverinfamous/postgres-mcp:latest"
]
}
}
}Recommended for production databases.
{
"args": [
"docker", "run", "-i", "--rm",
"-e", "DATABASE_URI=...",
"neverinfamous/postgres-mcp:latest",
"--access-mode=restricted"
]
}Features:
- ✅ Read-only operations
- ✅ Advanced SQL validation
- ✅ Query timeout protection
- ✅ Parameter binding required
Use only in development/testing environments.
{
"args": [
"docker", "run", "-i", "--rm",
"-e", "DATABASE_URI=...",
"neverinfamous/postgres-mcp:latest",
"--access-mode=unrestricted"
]
}Features:
⚠️ Full read/write access- ✅ Parameter binding protection
⚠️ DDL operations allowed⚠️ Use with caution
PostgreSQL connection string format:
postgresql://username:password@hostname:port/database?options
Examples:
# Local database
export DATABASE_URI="postgresql://postgres:password@localhost:5432/mydb"
# Remote database with SSL
export DATABASE_URI="postgresql://user:pass@db.example.com:5432/prod?sslmode=require"
# Cloud database (AWS RDS)
export DATABASE_URI="postgresql://admin:pass@mydb.region.rds.amazonaws.com:5432/database"
# With connection pooling
export DATABASE_URI="postgresql://user:pass@localhost:5432/db?pool_size=20"Connection Options:
-
sslmode=require- Require SSL/TLS -
connect_timeout=10- Connection timeout (seconds) -
application_name=myapp- Application identifier -
pool_size=20- Connection pool size
postgresql://username:password@hostname:port/database?options
│ │ │ │ │ │
│ │ │ │ │ └─ Optional parameters
│ │ │ │ └─ Database name
│ │ │ └─ Port (default: 5432)
│ │ └─ Hostname or IP
│ └─ Password
└─ Username
Start your MCP client and try:
list_schemas()Expected: List of database schemas
get_top_queries(sort_by="total_time", limit=5)Expected: Query statistics (requires pg_stat_statements)
In Restricted Mode, this should fail:
execute_sql(sql="DROP TABLE test")Expected error: "Operation not allowed in restricted mode"
Create .env file:
DATABASE_URI=postgresql://user:pass@host:5432/db
ACCESS_MODE=restrictedRun with env file:
docker run -i --rm --env-file .env neverinfamous/postgres-mcp:latestCreate docker-compose.yml:
version: '3.8'
services:
postgres-mcp:
image: neverinfamous/postgres-mcp:latest
stdin_open: true
environment:
- DATABASE_URI=postgresql://user:pass@postgres:5432/db
command: ["--access-mode=restricted"]
depends_on:
- postgres
postgres:
image: postgres:16
environment:
- POSTGRES_PASSWORD=password
- POSTGRES_DB=mydb
volumes:
- pgdata:/var/lib/postgresql/data
volumes:
pgdata:# Check PostgreSQL is running
pg_isready -h localhost -p 5432
# Verify firewall rules
telnet hostname 5432# Test connection manually
psql "postgresql://user:pass@host:5432/db"
# Check pg_hba.conf for access rules-- Install required extensions
CREATE EXTENSION IF NOT EXISTS pg_stat_statements;
CREATE EXTENSION IF NOT EXISTS pg_trgm;
CREATE EXTENSION IF NOT EXISTS fuzzystrmatch;See Extension Setup for details.
-
Check Docker container is running:
docker ps | grep postgres-mcp -
View container logs:
docker logs <container_id>
-
Verify environment variable:
docker exec <container_id> env | grep DATABASE_URI
Add to DATABASE_URI:
postgresql://user:pass@host:5432/db?pool_size=20&max_overflow=10
For production databases:
postgresql://user:pass@host:5432/db?sslmode=require&sslcert=/path/to/cert
Prevent hanging connections:
postgresql://user:pass@host:5432/db?connect_timeout=10&statement_timeout=30000
- Quick Start - Initial setup
- Extension Setup - PostgreSQL extensions
- Security Best Practices - Secure configuration
- Troubleshooting - Common issues
See Home for more tool categories.