npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

postgresql-mcp-ddz

v1.0.1

Published

PostgreSQL MCP Server - Model Context Protocol server for PostgreSQL database operations

Readme

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": ["-y","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": ["-y","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": ["张三", "[email protected]"]
}

list_tables

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

describe_table

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

参数:

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

示例:

{
  "table": "users"
}

explain

使用 EXPLAIN 分析 SQL 查询性能。

参数:

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

示例:

{
  "sql": "SELECT * FROM users WHERE email = '[email protected]'"
}

show_statement

执行 SHOW 语句。

参数:

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

示例:

{
  "sql": "SHOW ALL"
}

安全说明

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