PostgreSQL SSH MCP Server

PostgreSQL SSH MCP Server

A secure PostgreSQL MCP server with built-in SSH tunneling that enables AI assistants to connect to databases through bastion hosts automatically, supporting both STDIO and Streamable HTTP transports.

Category
访问服务器

README

PostgreSQL SSH MCP Server

npm version npm downloads license

A secure PostgreSQL MCP server with built-in SSH tunneling. Connect to databases through bastion hosts automatically — no manual ssh -L required.

Features

  • Dual Transport — STDIO for Claude Desktop, Streamable HTTP for ChatGPT
  • SSH Tunneling — Built-in tunnel with auto-reconnect and TOFU (trust on first use)
  • Read-Only by Default — Safe for production; enable writes explicitly
  • OAuth Support — Auth0 integration for secure ChatGPT connections
  • Connection Pooling — Efficient resource management with configurable limits

Architecture

Architecture


Quick Start

Claude Desktop (STDIO)

Add to your Claude Desktop config:

Platform Config Location
macOS ~/Library/Application Support/Claude/claude_desktop_config.json
Windows %APPDATA%/Claude/claude_desktop_config.json
{
  "mcpServers": {
    "postgres": {
      "command": "npx",
      "args": ["-y", "@zlash65/postgresql-ssh-mcp"],
      "env": {
        "DATABASE_URI": "postgresql://user:password@localhost:5432/mydb"
      }
    }
  }
}

ChatGPT (Streamable HTTP)

DATABASE_URI="postgresql://user:pass@localhost:5432/mydb" npx @zlash65/postgresql-ssh-mcp-http

Then configure ChatGPT to connect to https://your-subdomain.example.com/mcp.

Note: ChatGPT requires HTTPS. Use a tunnel (ngrok, Cloudflare Tunnel) for local testing.


Available Tools

Query Tools

Tool Description
execute_query Execute SQL with parameterized queries. Results capped by MAX_ROWS.
explain_query Get EXPLAIN plans in text, JSON, YAML, or XML format. Supports ANALYZE.

Schema Tools

Tool Description
list_schemas List database schemas. Excludes system schemas by default.
list_tables List tables with row counts and sizes. Optionally include views.
describe_table Get columns, constraints, and indexes for a table.
list_databases List all databases with owner, encoding, and size.

Admin Tools

Tool Description
get_connection_status Get pool stats, tunnel state, and connection info.
list_active_connections Show active connections from pg_stat_activity.
list_long_running_queries Find queries running longer than a threshold.
get_database_version Get PostgreSQL server version.
get_database_size Get database size and largest tables.
get_table_stats Get vacuum/analyze stats and scan counts for a table.

Environment Variables

Database Connection

Variable Required Default Description
DATABASE_URI Yes* Full connection string (e.g., postgresql://user:pass@host:5432/db)
DATABASE_HOST Yes* Database hostname
DATABASE_PORT No 5432 Database port
DATABASE_NAME Yes* Database name
DATABASE_USER Yes* Database username
DATABASE_PASSWORD Yes* Database password

*Either DATABASE_URI or all individual connection variables are required.

SSL Configuration

Variable Default Description
DATABASE_SSL Auto true or false. Auto-enabled for non-localhost.
DATABASE_SSL_CA Path to CA certificate bundle
DATABASE_SSL_REJECT_UNAUTHORIZED true Set false to allow self-signed certificates

SSH Tunnel

Variable Required Default Description
SSH_ENABLED No false Set true to enable SSH tunneling
SSH_HOST Yes* SSH server hostname
SSH_PORT No 22 SSH server port
SSH_USER Yes* SSH username
SSH_PRIVATE_KEY_PATH Yes** Path to private key file
SSH_PRIVATE_KEY_PASSPHRASE No Passphrase for encrypted keys
SSH_PASSWORD Yes** SSH password (alternative to key)
SSH_STRICT_HOST_KEY No true Verify host key against known_hosts
SSH_TRUST_ON_FIRST_USE No true Auto-add unknown hosts (when strict is enabled)
SSH_KNOWN_HOSTS_PATH No ~/.ssh/known_hosts Custom known_hosts file
SSH_KEEPALIVE_INTERVAL No 10000 Keepalive interval in milliseconds
SSH_MAX_RECONNECT_ATTEMPTS No 5 Max reconnect attempts (-1 for unlimited)

*Required when SSH_ENABLED=true **Either SSH_PRIVATE_KEY_PATH or SSH_PASSWORD is required

Query Behavior

Variable Default Description
READ_ONLY true Block data modifications. Set false to allow writes.
MAX_ROWS 1000 Maximum rows returned per query
QUERY_TIMEOUT 30000 Query timeout in milliseconds
MAX_CONCURRENT_QUERIES 10 Maximum concurrent queries
POOL_DRAIN_TIMEOUT_MS 5000 Timeout for draining pool during reconnect

HTTP Server (Streamable HTTP only)

Variable Default Description
PORT 3000 HTTP server port
MCP_HOST 0.0.0.0 HTTP server bind address
MCP_AUTH_MODE none Authentication mode: none or oauth
MCP_STATELESS true Stateless mode (each request re-initializes)
MCP_SERVER_POOL_SIZE 4 Server instances for stateless mode
MCP_SESSION_TTL_MINUTES 30 Session TTL for stateful mode
MCP_SESSION_CLEANUP_INTERVAL_MS 300000 Session cleanup interval
MCP_ALLOWED_ORIGINS Comma-separated allowed CORS origins (* for any)
MCP_ALLOWED_HOSTS Comma-separated allowed Host headers

OAuth (Auth0)

Variable Required Description
AUTH0_DOMAIN Yes* Auth0 tenant domain (e.g., tenant.us.auth0.com)
AUTH0_AUDIENCE Yes* Auth0 API identifier / audience
MCP_RESOURCE_DOCUMENTATION No URL to API documentation (RFC 9728)

*Required when MCP_AUTH_MODE=oauth


Documentation

Guide Description
STDIO Setup Claude Desktop and local development
Streamable HTTP HTTP server setup and configuration
Server Setup Deploy to production with nginx and SSL
ChatGPT Setup Complete Auth0 OAuth setup for ChatGPT

Docker

STDIO Server

docker build --target runtime -t postgresql-mcp .
docker run -e DATABASE_URI="postgresql://..." postgresql-mcp

HTTP Server

docker build --target runtime-http -t postgresql-mcp-http .
docker run -p 3000:3000 -e DATABASE_URI="postgresql://..." postgresql-mcp-http

Development

# Install dependencies
npm install

# Build
npm run build

# Run tests
npm test

# Type check
npm run typecheck

# Lint
npm run lint

推荐服务器

Baidu Map

Baidu Map

百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。

官方
精选
JavaScript
Playwright MCP Server

Playwright MCP Server

一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。

官方
精选
TypeScript
Magic Component Platform (MCP)

Magic Component Platform (MCP)

一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。

官方
精选
本地
TypeScript
Audiense Insights MCP Server

Audiense Insights MCP Server

通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。

官方
精选
本地
TypeScript
VeyraX

VeyraX

一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。

官方
精选
本地
graphlit-mcp-server

graphlit-mcp-server

模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。

官方
精选
TypeScript
Kagi MCP Server

Kagi MCP Server

一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。

官方
精选
Python
e2b-mcp-server

e2b-mcp-server

使用 MCP 通过 e2b 运行代码。

官方
精选
Neon MCP Server

Neon MCP Server

用于与 Neon 管理 API 和数据库交互的 MCP 服务器

官方
精选
Exa MCP Server

Exa MCP Server

模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。

官方
精选