Sponsored by Deepsite.site

Postgresql Mcp Ddz

Created By
fanzhenddz7 days ago
PostgreSQL MCP Server 一个基于模型上下文协议(MCP)的 PostgreSQL 数据库操作服务器,让 AI 助手能够通过标准化接口与 PostgreSQL 数据库交互。
Overview

PostgreSQL MCP Server

一个基于模型上下文协议(MCP)的 PostgreSQL 数据库操作服务器,让 AI 助手能够通过标准化接口与 PostgreSQL 数据库交互。

功能特性

  • 查询执行:执行带参数化的 SELECT 查询
  • 数据修改:执行 INSERT、UPDATE、DELETE 操作
  • 结构查询:列出数据库表、查看表结构
  • 性能分析:使用 EXPLAIN 分析查询性能
  • 连接管理:支持环境变量自动连接,安全可靠

安装

npm install postgresql-mcp-ddz

或直接使用 npx:

npx postgresql-mcp-ddz

配置

方式一:环境变量自动连接(推荐)

在 MCP 客户端配置中添加环境变量,服务器启动时会自动连接数据库:

{
  "mcpServers": {
    "postgresql-mcp-ddz": {
      "command": "npx",
      "args": ["postgresql-mcp-ddz"],
      "env": {
        "POSTGRES_HOST": "127.0.0.1",
        "POSTGRES_PORT": "5432",
        "POSTGRES_USER": "postgres",
        "POSTGRES_PASSWORD": "your_password",
        "POSTGRES_DB": "your_database"
      }
    }
  }
}

支持的环境变量:

变量名说明必填
POSTGRES_HOST数据库主机地址
POSTGRES_PORT数据库端口(默认 5432)
POSTGRES_USER数据库用户名
POSTGRES_PASSWORD数据库密码
POSTGRES_DB数据库名称

方式二:手动连接

不在配置中添加环境变量时,需要通过 connect_db 工具手动连接:

{
  "mcpServers": {
    "postgresql": {
      "command": "npx",
      "args": ["postgresql-mcp-ddz"]
    }
  }
}

可用工具

connect_db

连接到 PostgreSQL 数据库(环境变量配置后可省略此步骤)。

参数:

  • host(字符串,必填):数据库主机地址
  • port(数字,可选):数据库端口,默认 5432
  • user(字符串,必填):数据库用户名
  • password(字符串,必填):数据库密码
  • database(字符串,必填):数据库名称

示例:

{
  "host": "localhost",
  "port": 5432,
  "user": "postgres",
  "password": "password",
  "database": "mydb"
}

query

执行 SELECT 查询并返回结果。

参数:

  • sql(字符串,必填):SQL SELECT 查询语句
  • params(数组,可选):查询参数

示例:

{
  "sql": "SELECT * FROM users WHERE age > $1",
  "params": [18]
}

execute

执行 INSERT、UPDATE 或 DELETE 查询。

参数:

  • sql(字符串,必填):SQL 语句
  • params(数组,可选):查询参数

示例:

{
  "sql": "INSERT INTO users (name, email) VALUES ($1, $2)",
  "params": ["张三", "zhangsan@example.com"]
}

list_tables

列出当前数据库中的所有表。

describe_table

获取指定表的结构信息,包括列、主键和外键。

参数:

  • table(字符串,必填):表名

示例:

{
  "table": "users"
}

explain

使用 EXPLAIN 分析 SQL 查询性能。

参数:

  • sql(字符串,必填):要分析的 SQL 查询

示例:

{
  "sql": "SELECT * FROM users WHERE email = 'test@example.com'"
}

show_statement

执行 SHOW 语句。

参数:

  • sql(字符串,必填):SHOW SQL 语句

示例:

{
  "sql": "SHOW ALL"
}

安全说明

  • query 工具仅允许 SELECT、SHOW、EXPLAIN 和 WITH 查
  • 数据修改操作请使用 execute 工具
  • 所有查询支持参数化输入,防止 SQL 注入
  • 数据库凭据仅在运行时传递,不会被持久化存储

Server Config

{
  "mcpServers": {
    "postgresql-mcp-ddz": {
      "command": "npx",
      "args": [
        "postgresql-mcp-ddz"
      ],
      "env": {
        "POSTGRES_HOST": "127.0.0.1",
        "POSTGRES_PORT": "5432",
        "POSTGRES_USER": "postgres",
        "POSTGRES_PASSWORD": "your_password",
        "POSTGRES_DB": "your_database"
      }
    }
  }
}
Recommend Servers
TraeBuild with Free GPT-4.1 & Claude 3.7. Fully MCP-Ready.
Amap Maps高德地图官方 MCP Server
EdgeOne Pages MCPAn MCP service designed for deploying HTML content to EdgeOne Pages and obtaining an accessible public URL.
MCP AdvisorMCP Advisor & Installation - Use the right MCP server for your needs
CursorThe AI Code Editor
Serper MCP ServerA Serper MCP Server
Jina AI MCP ToolsA Model Context Protocol (MCP) server that integrates with Jina AI Search Foundation APIs.
DeepChatYour AI Partner on Desktop
BlenderBlenderMCP connects Blender to Claude AI through the Model Context Protocol (MCP), allowing Claude to directly interact with and control Blender. This integration enables prompt assisted 3D modeling, scene creation, and manipulation.
Zhipu Web SearchZhipu Web Search MCP Server is a search engine specifically designed for large models. It integrates four search engines, allowing users to flexibly compare and switch between them. Building upon the web crawling and ranking capabilities of traditional search engines, it enhances intent recognition capabilities, returning results more suitable for large model processing (such as webpage titles, URLs, summaries, site names, site icons, etc.). This helps AI applications achieve "dynamic knowledge acquisition" and "precise scenario adaptation" capabilities.
Tavily Mcp
WindsurfThe new purpose-built IDE to harness magic
Playwright McpPlaywright MCP server
Visual Studio Code - Open Source ("Code - OSS")Visual Studio Code
RedisA Model Context Protocol server that provides access to Redis databases. This server enables LLMs to interact with Redis key-value stores through a set of standardized tools.
Howtocook Mcp基于Anduin2017 / HowToCook (程序员在家做饭指南)的mcp server,帮你推荐菜谱、规划膳食,解决“今天吃什么“的世纪难题; Based on Anduin2017/HowToCook (Programmer's Guide to Cooking at Home), MCP Server helps you recommend recipes, plan meals, and solve the century old problem of "what to eat today"
MiniMax MCPOfficial MiniMax Model Context Protocol (MCP) server that enables interaction with powerful Text to Speech, image generation and video generation APIs.
Baidu Map百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。
AiimagemultistyleA Model Context Protocol (MCP) server for image generation and manipulation using fal.ai's Stable Diffusion model.
ChatWiseThe second fastest AI chatbot™
Y GuiA web-based graphical interface for AI chat interactions with support for multiple AI models and MCP (Model Context Protocol) servers.