postgres-mcp-server

postgres-mcp-server

MCP server for PostgreSQL that enables safe read-only database queries, table schema inspection, and query execution planning.

Category
访问服务器

README

PostgreSQL MCP Server

An enterprise-ready Model Context Protocol (MCP) server built with Node.js & TypeScript for PostgreSQL databases. Compatible with Antigravity, Claude Desktop, Cursor, Continue, and any standard MCP client.


🌟 Features & Tools Exposed

Tool Name Mode Description
list_tables Read-only Lists all schemas, tables, views, estimated row counts, and table disk sizes.
describe_table Read-only Details table schema: column types, nullability, defaults, primary keys, foreign keys, and indexes.
read_query Read-only Executes SELECT queries safely inside DB-level READ ONLY transactions with auto-limiting.
explain_query Read-only Generates PostgreSQL query execution plans (EXPLAIN / EXPLAIN ANALYZE) in JSON format.
get_db_stats Read-only Returns DB health stats: database size, top 10 largest tables, active connections, and cache hit ratios.
execute_query Write (Optional) Executes INSERT, UPDATE, DELETE, or DDL statements (disabled unless ALLOW_WRITE_QUERIES=true).

🛠️ Quick Start

1. Install Dependencies & Build

npm install
npm run build

2. Configure Environment Variables

Copy .env.example to .env and fill in your PostgreSQL credentials:

# Database Connection String
DATABASE_URL=postgresql://postgres:password@localhost:5432/mydb

# OR individual settings:
PGHOST=localhost
PGPORT=5432
PGUSER=postgres
PGPASSWORD=your_password
PGDATABASE=mydb

# Enable write capabilities if needed (Default: false)
ALLOW_WRITE_QUERIES=false

🚀 Registering with MCP Clients

This server uses standard stdio communication and can be added to any MCP-compliant application.

1. Antigravity Configuration

Add to your Antigravity MCP settings:

  • Global Config: %USERPROFILE%\.gemini\antigravity-cli\mcp_config.json (Windows) or ~/.gemini/antigravity-cli/mcp_config.json (Linux/macOS)
  • Workspace Config: .agents/mcp.json
{
  "mcpServers": {
    "postgres-db": {
      "command": "node",
      "args": [
        "D:/Projects/AI/MCP/postgres-connector/dist/index.js"
      ],
      "env": {
        "DATABASE_URL": "postgresql://your_user:your_password@localhost:5432/your_project_db",
        "ALLOW_WRITE_QUERIES": "false"
      }
    }
  }
}

2. Claude Desktop Configuration

Add to your Claude Desktop configuration file:

  • Windows: %APPDATA%\Claude\claude_desktop_config.json
  • macOS: ~/Library/Application Support/Claude/claude_desktop_config.json
{
  "mcpServers": {
    "postgres-db": {
      "command": "node",
      "args": [
        "D:/Projects/AI/MCP/postgres-connector/dist/index.js"
      ],
      "env": {
        "DATABASE_URL": "postgresql://your_user:your_password@localhost:5432/your_project_db",
        "ALLOW_WRITE_QUERIES": "false"
      }
    }
  }
}

3. Cursor / Continue / Standard MCP Clients

For Cursor, Continue, or any other MCP host supporting stdio servers:

{
  "mcpServers": {
    "postgres-db": {
      "command": "node",
      "args": [
        "D:/Projects/AI/MCP/postgres-connector/dist/index.js"
      ],
      "env": {
        "DATABASE_URL": "postgresql://your_user:your_password@localhost:5432/your_project_db",
        "ALLOW_WRITE_QUERIES": "false"
      }
    }
  }
}

Tip (Development Mode): You can also run the server directly with npx tsx without a build step:

"command": "npx",
"args": [
  "-y",
  "tsx",
  "D:/Projects/AI/MCP/postgres-connector/src/index.ts"
]

🧪 Testing & Local Debugging

Option 1: Interactive GUI Testing with MCP Inspector (Recommended)

The official MCP Inspector allows you to visually trigger tool calls (list_tables, read_query, describe_table, etc.) and view response payloads in your browser:

  • Quick development testing (no compilation step required):

    npx @modelcontextprotocol/inspector npx tsx src/index.ts
    
  • Production build testing:

    npm run build
    npx @modelcontextprotocol/inspector node dist/index.js
    

The command will automatically open the inspector in your default browser (e.g. http://localhost:<port>/?MCP_INSPECTOR_API_TOKEN=...), where you can inspect available tools, fill in parameters, and view responses in real time.

Option 2: Live Integration via MCP Hosts

Register the server in your AI host config (such as Antigravity or Claude Desktop) as detailed in Registering with MCP Clients, then restart your host application to test tool invocation through natural language prompts.

Option 3: Code Compilation & Type Check

Verify TypeScript types and esbuild bundle output:

npm run build

🔒 Security Best Practices

  • By default, all queries executed via read_query run inside BEGIN READ ONLY; ... COMMIT; transactions to prevent unexpected data mutations.
  • Write queries via execute_query are disabled by default unless ALLOW_WRITE_QUERIES=true.

📄 License & Attribution

This project is licensed under the [BSD 3-Clause License](file:///d:/Projects/AI/MCP/postgres-connector/LICENSE).

Mandatory Terms:

  • Attribution Required: You are free to use, modify, and distribute this software, provided that all copies retain the original copyright notice (Copyright (c) 2026, Rajath Kumar D) and license terms.
  • No Unauthorized Endorsement: You may not use the author's name (Rajath Kumar D) or contributors to endorse or promote derived products without prior written permission.

推荐服务器

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

官方
精选