postgres-mcp

postgres-mcp

Enables safe interaction with PostgreSQL databases through read-only queries, schema exploration, and performance analysis.

Category
访问服务器

README

postgres-mcp

!!! DO NOT USE IN PRODUCTION !!! !!! ONLY FOR LOCAL DEV & DB !!!

A PostgreSQL MCP (Model Context Protocol) server that enables Claude to safely interact with PostgreSQL databases via stdio/JSON-RPC 2.0.

Features

  • Execute queries — run parameterized SELECT statements with row limits
  • Explore structure — list tables/views and inspect columns, indexes, foreign keys
  • Analyze performance — generate EXPLAIN / EXPLAIN ANALYZE plans
  • Schema context — expose full DDL-like schema as an MCP resource
  • Security — read-only mode, schema whitelisting, SQL injection prevention

Requirements

  • Node.js ≥ 18 (ESM)
  • PostgreSQL database

Installation

npm install

Configuration

Copy .env.example to .env and fill in your values:

POSTGRES_URL=postgresql://user:password@localhost:5432/dbname
POSTGRES_SCHEMAS=public
POSTGRES_READONLY=true
POSTGRES_QUERY_TIMEOUT=30000
POSTGRES_MAX_ROWS=500
Variable Default Description
POSTGRES_URL (required) PostgreSQL connection string
POSTGRES_SCHEMAS public Comma-separated list of allowed schemas
POSTGRES_READONLY true Block any write statement
POSTGRES_QUERY_TIMEOUT 30000 Query timeout in milliseconds
POSTGRES_MAX_ROWS 500 Maximum rows returned per query

Usage

Integration with Claude Code

The server is designed to be run via npx directly from its local directory — no global install needed.

Add it to your Claude Code config (~/.claude/settings.json for global use, or .claude/settings.json at project level):

{
  "mcpServers": {
    "postgres": {
      "command": "npx",
      "args": ["/absolute/path/to/postgres_mcp"],
      "env": {
        "POSTGRES_URL": "postgresql://user:password@localhost:5432/dbname",
        "POSTGRES_SCHEMAS": "public",
        "POSTGRES_READONLY": "true"
      }
    }
  }
}

Replace /absolute/path/to/postgres_mcp with the actual path to this repository on your machine. All configuration is passed via env — no .env file required when using this approach.

Note: npx runs npm install automatically on first launch if node_modules is absent.

Testing with MCP Inspector

The MCP Inspector provides a web UI to test the server interactively without Claude Code.

POSTGRES_URL=postgresql://user:password@localhost:5432/dbname \
npx @modelcontextprotocol/inspector npx /absolute/path/to/postgres_mcp

Then open http://localhost:5173 in your browser. From there you can:

  • Call any tool with custom parameters and see the response
  • Read the postgres://schema resource
  • Inspect the raw JSON-RPC messages exchanged

Tools

query

Execute a SQL SELECT query.

{
  "sql": "SELECT * FROM users WHERE id = $1",
  "params": [42]
}

Returns: rows, rowCount, fields, truncated (if row limit was hit).

list_tables

List all accessible tables and views.

Returns an array of { schema, name, type } objects.

describe_table

Get the full structure of a table: columns, indexes, and foreign keys.

{
  "table": "users",
  "schema": "public"
}

Returns: columns (name, type, nullable, default, primary_key), indexes, foreign_keys.

explain_query

Generate an execution plan for a SQL query.

{
  "sql": "SELECT * FROM orders WHERE user_id = 1",
  "analyze": false
}

Set analyze: true to run EXPLAIN ANALYZE (actually executes the query).

Resource

postgres://schema

Auto-loaded resource exposing the full database schema in DDL-like format. Gives Claude upfront context about all accessible tables and their columns.

Security

  • Read-only mode (default): blocks INSERT, UPDATE, DELETE, DROP, CREATE, ALTER, TRUNCATE, GRANT, REVOKE, and other write operations via regex validation
  • Schema isolation: queries are restricted to schemas listed in POSTGRES_SCHEMAS
  • Parameterized queries: prevents SQL injection
  • Connection pooling: max 5 connections with idle/connection timeouts
  • Row and time limits: configurable caps prevent runaway queries

Tests

npm test

Tests use Vitest with mocked database calls — no live database required.

Project Structure

src/
├── index.js          # MCP server entry point
├── db.js             # Connection pool & configuration
├── security.js       # SQL and schema validation
├── tools/
│   ├── query.js
│   ├── list_tables.js
│   ├── describe_table.js
│   └── explain_query.js
└── resources/
    └── schema.js
tests/
├── security.test.js
├── query.test.js
├── list_tables.test.js
├── describe_table.test.js
├── explain_query.test.js
└── schema_resource.test.js

推荐服务器

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 模型以安全和受控的方式获取实时的网络信息。

官方
精选