Sign inSign up

chornthorn/postgresql-mcp-server

By chornthorn

•Updated 3 months ago

MCP server exposing a PostgreSQL database to LLM hosts via the Model Context Protocol

Image
Languages & frameworks
Machine learning & AI
Developer tools
0

479

chornthorn/postgresql-mcp-server repository overview

⁠PostgreSQL MCP Server

A Model Context Protocol⁠ server that exposes a PostgreSQL database to LLM hosts (Claude Desktop, Cursor, VS Code, Claude Code).

Read-only by default. Writes are rejected by PostgreSQL itself unless explicitly opted in.


⁠Quick start

docker run -i --rm \
  -e POSTGRES_DSN="postgresql://user:[email protected]:5432/db" \
  chornthorn/postgresql-mcp-server

Then connect your MCP host (Claude Desktop, etc.) to this container as a stdio subprocess.


⁠Environment variables

VariableDefaultRequiredPurpose
POSTGRES_DSN—Yes*Connection string, e.g. postgresql://user:[email protected]:5432/db. Falls back to libpq env vars (PGHOST, PGPORT, PGUSER, PGPASSWORD, PGDATABASE)
PG_MCP_READ_ONLYtrueNoWhen true, every connection is set read-only at the PostgreSQL level
PG_MCP_MAX_ROWS100NoHard cap on rows returned by execute_sql
PG_MCP_STATEMENT_TIMEOUT_MS30000NoPer-statement timeout, enforced by PostgreSQL
PG_MCP_POOL_MIN1NoConnection pool minimum
PG_MCP_POOL_MAX5NoConnection pool maximum
PG_MCP_LOG_LEVELINFONoDEBUG / INFO / WARNING / ERROR / CRITICAL
PG_MCP_TRANSPORTstdioNostdio (local hosts) or http (streamable-http)
PG_MCP_PORT8000NoPort for HTTP transport

* Either POSTGRES_DSN or the standard libpq variables (PGHOST, PGPORT, PGUSER, PGPASSWORD, PGDATABASE) must be set.

💡 Host machine database connection tip: When connecting to a database running on your host machine (outside of Docker):

  • macOS / Windows: Use host.docker.internal as the database hostname in your connection string (e.g. host.docker.internal:5432).
  • Linux: Use localhost:5432 and run your container with the --network=host flag.

⁠Usage

⁠stdio transport (default)

Use this mode when your MCP host launches the container as a subprocess:

docker run -i --rm \
  -e POSTGRES_DSN="postgresql://user:[email protected]:5432/db" \
  chornthorn/postgresql-mcp-server

The container reads from stdin and writes to stdout — the stdio protocol wire. Logs go to stderr. The -i flag is required to keep stdin open.

⁠HTTP transport
docker run -d --rm -p 8000:8000 \
  -e POSTGRES_DSN="postgresql://user:[email protected]:5432/db" \
  -e PG_MCP_TRANSPORT=http \
  chornthorn/postgresql-mcp-server

Clients connect to http://127.0.0.1:8000/mcp.

⁠Connecting from an MCP host

⁠Claude Desktop
{
  "mcpServers": {
    "postgresql": {
      "command": "docker",
      "args": [
        "run", "-i", "--rm",
        "-e", "POSTGRES_DSN=postgresql://user:[email protected]:5432/db",
        "chornthorn/postgresql-mcp-server"
      ]
    }
  }
}
⁠Zed

Add to your global ~/.config/zed/settings.json:

{
  "mcp_servers": {
    "postgresql": {
      "command": "docker",
      "args": [
        "run", "-i", "--rm",
        "-e", "POSTGRES_DSN=postgresql://user:[email protected]:5432/db",
        "chornthorn/postgresql-mcp-server"
      ]
    }
  }
}

Replace command and args as needed for Cursor, VS Code, or Claude Code — the pattern is the same.


⁠What the server exposes

Tools (the model calls these):

ToolWhat it does
list_schemasList user schemas (excludes pg_catalog)
list_tablesTables, views, materialized views in a schema, with row estimates
describe_tableColumns, primary key, foreign keys, indexes for one table
list_viewsViews and their definitions
list_functionsFunctions and procedures with argument/return types
execute_sqlRun a parameterized, row-capped, read-only SQL statement
explain_queryEXPLAIN (optionally EXPLAIN ANALYZE) a statement

⁠Safety model

  • Read-only enforced at PostgreSQL via default_transaction_read_only = on on every pooled connection. Set PG_MCP_READ_ONLY=false to allow writes.
  • Parameterized queries only — values are bound via %s placeholders; no string interpolation.
  • Statement timeout enforced server-side via statement_timeout.
  • Row cap on execute_sql results (PG_MCP_MAX_ROWS); truncated results are flagged.
  • Fail-fast startup — if no database is configured, the container exits immediately with an actionable error message.

Tag summary

Content type

Image

Digest

sha256:92ccea7ea…

Size

32.1 MB

Last updated

3 months ago

docker pull chornthorn/postgresql-mcp-server