PostgreSQL MCP Server

PostgreSQL MCP Server

An open-source MCP server for PostgreSQL schema introspection and guarded read-only queries. It enables MCP clients to discover schemas, tables, columns, indexes, relationships, and safe queryable data from a configured PostgreSQL database.

Category
访问服务器

README

PostgreSQL MCP Server

An open-source Model Context Protocol (MCP) server for PostgreSQL schema introspection and guarded read-only queries. It helps MCP clients discover schemas, tables, columns, indexes, relationships, and safe queryable data from one configured PostgreSQL database.

Works with standard PostgreSQL providers, including Supabase-hosted Postgres.

Features

  • Schema tools for tables, views, columns, primary keys, foreign keys, indexes, and approximate table statistics
  • PostgreSQL schema support with DB_SCHEMA defaulting to public
  • Table and column search with simple * and ? wildcards
  • Relationship discovery for join-path exploration
  • Optional read-only execute_query tool with SELECT-only validation, access control, and automatic LIMIT
  • Internal src/core boundary for code that can later be shared with other database-specific MCP servers

Requirements

  • Node.js 18+
  • PostgreSQL 13+ or a compatible hosted Postgres service
  • A PostgreSQL role with read access to the configured database/schema

Setup

Fast path for users installing from npm:

npx -y @sigma4life/postgres-mcp-server init

Then edit .env and check the connection:

npx -y @sigma4life/postgres-mcp-server doctor

Local development from this repository:

npm install
cp .env.example .env
npm run build
npm start

Configure .env:

DATABASE_URL=postgresql://app_user:app_user_password@localhost:5432/app_db
DB_SCHEMA=public
SCHEMA_ONLY_MODE=true

You can also use individual DB_HOST, DB_PORT, DB_NAME, DB_USER, DB_PASSWORD, and DB_SSL variables. Individual variables override DATABASE_URL.

This server connects to one configured database per process. Tool inputs may include database for compatibility, but it must match the configured database. Tool inputs may include schema; otherwise DB_SCHEMA or public is used.

Least-Privilege PostgreSQL User

CREATE ROLE app_user LOGIN PASSWORD 'app_user_password';
GRANT CONNECT ON DATABASE app_db TO app_user;
GRANT USAGE ON SCHEMA public TO app_user;
GRANT SELECT ON ALL TABLES IN SCHEMA public TO app_user;
ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT SELECT ON TABLES TO app_user;

Use a stronger host restriction and password in production.

MCP Client Example

Client-specific docs:

{
  "mcpServers": {
    "postgres": {
      "command": "npx",
      "args": ["-y", "@sigma4life/postgres-mcp-server"],
      "env": {
        "DATABASE_URL": "postgresql://app_user:app_user_password@localhost:5432/app_db",
        "DB_SCHEMA": "public",
        "SCHEMA_ONLY_MODE": "true"
      }
    }
  }
}

To enable execute_query, set SCHEMA_ONLY_MODE=false and provide QUERY_ACCESS_CONFIG.

Access Control

execute_query is blocked unless QUERY_ACCESS_CONFIG points to a JSON policy file. Example:

{
  "requireExplicitColumns": true,
  "databases": {
    "app_db": {
      "schemas": {
        "public": {
          "tables": {
            "mode": "whitelist",
            "list": ["customers", "orders", "products", "order_items"],
            "columnAccess": {
              "customers": {
                "mode": "exclusion",
                "columns": ["password_hash", "api_token"]
              }
            }
          }
        }
      }
    }
  }
}

Tools

  • get_schema
  • get_table_info
  • find_tables
  • search_objects
  • get_relationships
  • validate_objects
  • get_accessible_schema
  • get_accessible_table_info
  • execute_query when schema-only mode is disabled

Example prompts:

  • "Show me the schema for the customers and orders tables."
  • "Find tables with a column matching *email*."
  • "Show relationships from orders to customers."
  • "Run SELECT id, email FROM customers ORDER BY id LIMIT 20."

Development

npm run build
npm test
npm run lint

The first implementation is PostgreSQL-specific. Shared logic lives under src/core so future database-specific repos can extract or reuse it without carrying PostgreSQL catalog code.

推荐服务器

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

官方
精选