writenotenow/postgres-mcp

By writenotenow

Updated 19 days ago

PostgreSQL MCP: 248 Tools in 1 Code Mode, Audit+Token Log, Tool Filtering, Pooling, HTTP/SSE, OAuth

Image
Developer tools
Data science
Databases & storage
1

4.1K

writenotenow/postgres-mcp repository overview

postgres-mcp

PostgreSQL MCP Server binding the Model Context Protocol to a secure PostgreSQL sandbox.

Features Code Mode — a revolutionary approach that provides access to all 248 tools through a secure, true V8 isolate (worker_threads), eliminating the massive token overhead of multi-step tool calls. Also includes schema introspection, migration tracking, smart tool filtering, deterministic error handling, connection pooling, HTTP/SSE transport, OAuth 2.1 authentication, and support for citext, ltree, pgcrypto, pg_cron, pg_stat_kcache, pgvector, PostGIS, and HypoPG.

248 Specialized Tools · 23 Resources · 20 AI-Powered Prompts

GitHub GitHub Release Docker Pulls License: MIT MCP npm Security Status TypeScript E2E Tests Coverage

GitHubnpm PackageMCP RegistryWikiTool ReferenceChangelog

🎯 What Sets Us Apart

FeatureDescription
Code Mode (V8 Isolate)Massive Token Savings: Execute complex, multi-step operations inside a secure, true V8 isolate (worker_threads). Stop burning tokens on back-and-forth tool calls and reduce your AI overhead by up to 90%.
Deterministic Error HandlingNo more cryptic database errors causing AI hallucinations. We intercept and translate raw SQL exceptions into clear, actionable advice so your agent knows exactly how to recover without guessing.
248 Token-Optimized ToolsThe largest PostgreSQL toolset on the MCP registry. Every query uses zero-cost token estimation and smart dataset truncation, ensuring agents always see the big picture without blowing their context windows.
OAuth 2.1 + Granular ControlReal enterprise security. Authenticate via OAuth 2.1 and control exactly who can read, write, or administer your database with precision scopes mapped down to the specific tool layer.
Audit Trails & Semantic DiffingTotal accountability. Track exactly what your AI is doing with detailed JSON logs, automatically snapshot schemas before mutations, and confidently review semantic row-by-row diffs before restoring data.
23 Resources & 20 PromptsInstant database meta-awareness. Agents automatically read real-time health, performance, and replication metrics, and can invoke built-in prompt workflows for query tuning and schema design.
Introspection & MigrationsPrevent costly mistakes. Let your AI simulate the cascade impact of schema changes, safely order foreign-key updates, and track migration history automatically.
8 Extension EcosystemsReady for advanced workloads. First-class API support for pgvector (AI search), PostGIS (geospatial), pg_cron, pgcrypto, and more—all strictly typed and validated out of the box.
Smart Tool FilteringGive your agent exactly what it needs without overflowing IDE limits. Dynamically compile your server with any combination of our 22 distinct tool groups.
Enterprise InfrastructureBuilt for production. Blazing fast (millions of ops/sec), protected against SQL injection, features high-performance connection pooling, and supports both Streamable HTTP and Legacy SSE protocols simultaneously.

Suggested Rule (Add to AGENTS.md, GEMINI.md, etc)

MCP TOKEN MANAGEMENT:

  • Token Visibility: When interacting with postgres-mcp, always monitor the _meta.tokenEstimate (or metrics.tokenEstimate in Code Mode) returned in tool responses.
  • Audit Resource: Use the postgres://audit resource to review session-level token consumption and identify high-cost operations.
  • Proactive Efficiency: If operations are consuming high token counts, prefer code mode and proactively use limit parameters.
Extension Support
ExtensionPurposeTools
pg_stat_statementsQuery performance trackingpg_stat_statements
pg_trgmText similaritypg_trigram_similarity
fuzzystrmatchFuzzy matchingpg_fuzzy_match
hypopgHypothetical indexespg_index_recommendations
pgvectorVector similarity search16 vector tools
PostGISGeospatial operations15 postgis tools
pg_cronJob scheduling8 cron tools
pg_partmanAutomated partition management10 partman tools
pg_stat_kcacheOS-level CPU/memory/I/O stats7 kcache tools
citextCase-insensitive text6 citext tools
ltreeHierarchical tree labels8 ltree tools
pgcryptoHashing, encryption, UUIDs9 pgcrypto tools

Extension tool counts include create_extension helpers but exclude Code Mode; the Tool Groups table below adds +1 per group for Code Mode.

MCP Resources (23)

Real-time database meta-awareness - AI accesses these automatically:

ResourcePurpose
postgres://schemaComplete schema with tables, columns, indexes
postgres://healthComprehensive health status
postgres://performanceQuery performance metrics
postgres://capabilitiesServer features and extensions
postgres://indexesIndex usage statistics
postgres://activityCurrent connections and active queries
postgres://auditAudit trail with token summary
postgres://help/{group}Group-specific help and workflow resources

Full resources list →

MCP Prompts (20)

Guided workflows for complex operations:

PromptPurpose
pg_performance_analysisStep-by-step query optimization
pg_index_tuningComprehensive index analysis
pg_database_health_checkFull health assessment
pg_setup_pgvectorComplete pgvector setup guide
pg_backup_strategyDesign backup strategy
pg_tool_indexCompact tool index reference

Full prompts list →

🚀 Quick Start (2 Minutes)

1. Pull the Image
docker pull writenotenow/postgres-mcp:latest
2. Add to MCP Config

Add this to your MCP client config (e.g., ~/.cursor/mcp.json for Cursor):

{
  "mcpServers": {
    "postgres-mcp": {
      "command": "docker",
      "args": [
        "run",
        "--rm",
        "-i",
        "-e",
        "POSTGRES_HOST",
        "-e",
        "POSTGRES_PORT",
        "-e",
        "POSTGRES_USER",
        "-e",
        "POSTGRES_PASSWORD",
        "-e",
        "POSTGRES_DATABASE",
        "writenotenow/postgres-mcp:latest",
        "--tool-filter",
        "codemode",
        "--audit-log",
        "/tmp/postgres-logs/audit.jsonl"
      ],
      "env": {
        "POSTGRES_HOST": "host.docker.internal",
        "POSTGRES_PORT": "5432",
        "POSTGRES_USER": "your_username",
        "POSTGRES_PASSWORD": "your_password",
        "POSTGRES_DATABASE": "your_database"
      }
    }
  }
}

Note for Docker: Use host.docker.internal to connect to PostgreSQL running on your host machine.

🔧 Configuration

Environment Variables

PostgreSQL Connection (required):

-e POSTGRES_HOST=localhost
-e POSTGRES_PORT=5432
-e POSTGRES_USER=your_user
-e POSTGRES_PASSWORD=your_password
-e POSTGRES_DATABASE=your_database

Or use a connection string:

-e POSTGRES_URL=postgres://user:pass@host:5432/database

Server & Tuning:

VariableDefaultDescription
POSTGRES_HOSTlocalhostDatabase host
POSTGRES_PORT5432Database port
POSTGRES_USERpostgresDatabase username
POSTGRES_PASSWORD(empty)Database password
POSTGRES_DATABASEpostgresDatabase name
POSTGRES_URLConnection string (overrides individual vars)
MCP_HOSTlocalhostServer bind host (0.0.0.0 for containers)
MCP_TRANSPORTstdioTransport type: stdio, http, sse
PORT3000HTTP port for http/sse transports
LOG_LEVELinfoLog level: debug, info, warning, error
METADATA_CACHE_TTL_MS30000Schema cache TTL (ms)
POSTGRES_TOOL_FILTERTool filter string (also MCP_TOOL_FILTER)
MCP_RATE_LIMIT_MAX100Rate limit per IP per 15min window
MCP_REQUEST_TIMEOUT300000HTTP request timeout (ms) for Slowloris protection
MCP_HEADERS_TIMEOUT60000HTTP headers timeout (ms)
MCP_AUTH_TOKENSimple bearer token for HTTP auth
TRUST_PROXYfalseTrust X-Forwarded-For for client IP
OAUTH_ENABLEDfalseEnable OAuth 2.1 authentication
OAUTH_ISSUERAuthorization server URL
OAUTH_AUDIENCEExpected token audience
OAUTH_JWKS_URI(auto)JWKS URI (auto-discovered from issuer)
OAUTH_CLOCK_TOLERANCE60Clock tolerance in seconds
AUDIT_LOG_PATHAudit log file path (stderr for container logs)
AUDIT_REDACTfalseOmit tool arguments from audit entries
AUDIT_BACKUPfalseEnable pre-mutation DDL snapshots
AUDIT_BACKUP_DATAfalseInclude sample data rows in snapshots
AUDIT_BACKUP_MAX_AGE30Maximum snapshot age in days
AUDIT_BACKUP_MAX_COUNT1000Maximum number of snapshots to retain
AUDIT_BACKUP_MAX_DATA_SIZE52428800Maximum table size for data capture (bytes)
AUDIT_READSfalseLog read-scoped tool calls (compact entries)
AUDIT_LOG_MAX_SIZE10485760Max log file size before rotation (bytes). Keeps up to 5 files.

Aliases: PGHOST, PGPORT, PGUSER, PGPASSWORD, PGDATABASE are also supported (standard PostgreSQL client env vars).

🔗 Database Connection Scenarios
ScenarioHost to UseExample Connection String
PostgreSQL on host machinelocalhost or host.docker.internalpostgres://user:pass@localhost:5432/db
PostgreSQL in DockerContainer name or networkpostgres://user:pass@postgres-container:5432/db
Remote/Cloud PostgreSQLHostname or IPpostgres://user:[email protected]:5432/db
ProviderExample Hostname
AWS RDS PostgreSQLyour-instance.xxxx.us-east-1.rds.amazonaws.com
Google Cloud SQLproject:region:instance (via Cloud SQL Proxy)
Azure PostgreSQLyour-server.postgres.database.azure.com
Supabasedb.xxxx.supabase.co
Neonep-xxx.us-east-1.aws.neon.tech
🛠️ Tool Filtering

Important

All tool groups include **Code Mode** (`pg_execute_code`) by default. To exclude it, add `-codemode` to your filter: `--tool-filter cron,pgcrypto,-codemode`

⭐ Code Mode (--tool-filter codemode) is the recommended configuration — it exposes pg_execute_code, a secure, true V8 isolate sandbox providing access to all 248 tools' worth of capability with up to 90% token savings. See Tool Filtering for alternatives.

  • Requires admin OAuth scope — execution is logged for audit

📖 See Full Installation Guide →

What Can You Filter?

The --tool-filter argument accepts groups or tool names — mix and match freely:

Filter PatternExampleDescription
Groups onlycore,jsonb,transactionsCombine individual groups
Tool namespg_read_query,pg_explainCustom tool selection
Group + Toolcore,+pg_stat_statementsExtend a group
Group - Toolcore,-pg_drop_tableRemove specific tools
Tool Groups (22 Available)
GroupToolsDescription
codemode1Code Mode (sandboxed code execution) 🌟 Recommended
core21Read/write queries, tables, indexes, convenience/drop tools
transactions9BEGIN, COMMIT, ROLLBACK, savepoints, status
jsonb21JSONB manipulation, queries, and pretty-print
text14Full-text search, fuzzy matching
performance25EXPLAIN, query analysis, optimization, diagnostics, anomaly detection
admin12VACUUM, ANALYZE, REINDEX, insights
monitoring12Database sizes, connections, status
backup13pg_dump, COPY, restore, audit backups
schema13Schemas, views, sequences, functions, triggers
introspection7Dependency graphs, cascade simulation, schema analysis
migration7Schema migration tracking and management
partitioning7Native partition management
stats20Statistical analysis, window functions, outlier detection
vector17pgvector (AI/ML similarity search)
postgis16PostGIS (geospatial)
cron9pg_cron (job scheduling)
partman11pg_partman (auto-partitioning)
kcache7pg_stat_kcache (OS-level stats)
citext7citext (case-insensitive text)
ltree9ltree (hierarchical data)
pgcrypto10pgcrypto (encryption, UUIDs)
Syntax Reference
PrefixTargetExampleEffect
(none)GroupcoreWhitelist Mode: Enable ONLY this group
(none)Toolpg_read_queryWhitelist Mode: Enable ONLY this tool
+Group+vectorAdd tools from this group to current set
-Group-adminRemove tools in this group from current set
+Tool+pg_explainAdd one specific tool
-Tool-pg_drop_tableRemove one specific tool

🌐 HTTP/SSE Transport (Remote Access)

For remote access, web-based clients, or HTTP-compatible MCP hosts:

docker run --rm -p 3000:3000 \
  -e POSTGRES_URL=postgres://user:pass@host:5432/db \
  writenotenow/postgres-mcp:latest \
  --transport http --port 3000

With simple bearer token authentication:

docker run --rm -p 3000:3000 \
  -e POSTGRES_URL=postgres://user:pass@host:5432/db \
  -e MCP_AUTH_TOKEN=my-secret-token \
  writenotenow/postgres-mcp:latest \
  --transport http --port 3000

With OAuth 2.1 (recommended for production):

docker run --rm -p 3000:3000 \
  -e POSTGRES_URL=postgres://user:pass@host:5432/db \
  -e OAUTH_ENABLED=true \
  -e OAUTH_ISSUER=http://keycloak:8080/realms/postgres-mcp \
  -e OAUTH_AUDIENCE=postgres-mcp-client \
  writenotenow/postgres-mcp:latest \
  --transport http --port 3000

Stateless mode (serverless, no sessions):

docker run --rm -p 3000:3000 \
  -e POSTGRES_URL=postgres://user:pass@host:5432/db \
  writenotenow/postgres-mcp:latest \
  --transport http --port 3000 --stateless

⚠️ Security: When using --transport http without --auth-token or OAuth, all clients have full unrestricted access. Always enable authentication for production HTTP deployments.

Priority: When both MCP_AUTH_TOKEN and OAUTH_ENABLED are set, OAuth 2.1 takes precedence. If neither is configured, the server warns and runs without authentication.

The server supports two MCP transport protocols simultaneously, enabling both modern and legacy clients to connect:

Modern protocol (MCP 2025-03-26) — single endpoint, session-based:

MethodEndpointPurpose
POST/mcpJSON-RPC requests (initialize, tools/list, etc.)
GET/mcpSSE stream for server notifications
DELETE/mcpSession termination

Sessions are managed via the Mcp-Session-Id header.

Docker Health Check: Built-in HEALTHCHECK is transport-aware and validates database and HTTP endpoint connectivity.

For Legacy SSE usage and utility endpoints, see the Wiki.

🛡️ Supply Chain Security

For reproducible builds, use SHA-pinned images:

docker pull writenotenow/postgres-mcp@sha256:<manifest-digest>

Find SHA tags here

📄 License

MIT License - See LICENSE

Tag summary

Content type

Image

Digest

sha256:a0a5352c5

Size

66.2 MB

Last updated

19 days ago

docker pull writenotenow/postgres-mcp:sha-7484817