Enhanced PostgreSQL MCP Server

by GarethCott
Verified

remote-capable server

The server can be hosted and run remotely because it primarily relies on remote services or has no dependency on the local environment.

Integrations

  • Supports running the PostgreSQL MCP server in a Docker container, with configuration options for connecting to PostgreSQL databases.

  • Provides both read and write access to PostgreSQL databases, allowing for data querying, data modification (insert, update, delete), and schema management (creating tables, functions, triggers, indexes).

PostgreSQL MCP 服务器(增强版)

提供对 PostgreSQL 数据库的读写访问权限的模型上下文协议 (MLM) 服务器。该服务器使 LLM 能够检查数据库架构、执行查询、修改数据以及创建/修改数据库架构对象。

**注意:**这是 Anthropic 原版PostgreSQL MCP 服务器的增强版。原版服务器提供只读访问权限,而增强版则增加了写入功能和架构管理。

成分

工具

数据查询

  • 询问
    • 对连接的数据库执行只读 SQL 查询
    • 输入: sql (字符串):要执行的 SQL 查询
    • 所有查询都在只读事务中执行

数据修改

  • 执行
    • 执行修改数据的 SQL 语句(INSERT、UPDATE、DELETE)
    • 输入: sql (字符串):要执行的 SQL 语句
    • 在具有正确 COMMIT/ROLLBACK 处理的事务内执行
  • 插入
    • 在表中插入一条新记录
    • 输入:
      • table (字符串):表名称
      • data (对象):键值对,其中键是列名,值是要插入的数据
  • 更新
    • 更新表中的记录
    • 输入:
      • table (字符串):表名称
      • data (对象):要更新的字段的键值对
      • where (字符串):用于识别要更新的记录的 WHERE 条件
  • 删除
    • 从表中删除记录
    • 输入:
      • table (字符串):表名称
      • where (字符串):用于标识要删除的记录的 WHERE 条件

模式管理

  • 创建表
    • 创建具有指定列和约束的新表
    • 输入:
      • tableName (字符串):表名称
      • columns (数组):具有名称、类型和可选约束的列定义数组
      • constraints (数组):表级约束的可选数组
  • 创建函数
    • 创建 PostgreSQL 函数/过程
    • 输入:
      • name (字符串):函数名称
      • parameters (字符串):函数参数
      • returnType (字符串):返回类型
      • language (字符串):语言(plpgsql、sql 等)
      • body (字符串):函数主体
      • options (字符串):可选的附加功能选项
  • 创建触发器
    • 在表上创建触发器
    • 输入:
      • name (字符串):触发器名称
      • tableName (字符串):应用触发器的表
      • functionName (字符串):要调用的函数
      • when (字符串):BEFORE、AFTER 或 INSTEAD OF
      • events (数组):事件数组(插入、更新、删除)
      • forEach (字符串):ROW 或 STATEMENT
      • condition (字符串):可选的 WHEN 条件
  • 创建索引
    • 在表上创建索引
    • 输入:
      • tableName (字符串):表名称
      • indexName (字符串):索引名称
      • columns (数组):要索引的列
      • unique (布尔值):索引是否唯一
      • type (字符串):可选索引类型(BTREE、HASH、GIN、GIST 等)
      • where (字符串):可选条件
  • 修改表
    • 更改表结构
    • 输入:
      • tableName (字符串):表名称
      • operation (字符串):操作(添加列、删除列等)
      • details (字符串):操作详细信息

资源

服务器为数据库中的每个表提供架构信息:

  • 表模式postgres://<host>/<table>/schema
    • 每个表的 JSON 架构信息
    • 包括列名和数据类型
    • 从数据库元数据中自动发现

与 Claude Desktop 一起使用

要将此服务器与 Claude Desktop 应用程序一起使用,请将以下配置添加到claude_desktop_config.json的“mcpServers”部分:

Docker

  • 在 macos 上运行 docker 时,如果服务器在主机网络上运行(例如 localhost),请使用 host.docker.internal
  • 可以将用户名/密码添加到 postgresql url,格式为postgresql://user:password@host:port/db-name
  • 如果需要绕过 SSL 证书验证,请添加?sslmode=no-verify
{ "mcpServers": { "postgres": { "command": "docker", "args": [ "run", "-i", "--rm", "mcp/postgres", "postgresql://host.docker.internal:5432/mydb"] } } }

NPX

{ "mcpServers": { "postgres": { "command": "npx", "args": [ "-y", "@modelcontextprotocol/server-postgres", "postgresql://localhost/mydb" ] } } }

/mydb替换为您的数据库名称。

示例用法

查询数据

/query SELECT * FROM users LIMIT 5

插入数据

/insert table="users", data={"name": "John Doe", "email": "john@example.com"}

更新数据

/update table="users", data={"status": "inactive"}, where="id='123'"

创建表

/createTable tableName="tasks", columns=[ {"name": "id", "type": "SERIAL", "constraints": "PRIMARY KEY"}, {"name": "title", "type": "VARCHAR(100)", "constraints": "NOT NULL"}, {"name": "created_at", "type": "TIMESTAMP", "constraints": "DEFAULT CURRENT_TIMESTAMP"} ]

创建函数和触发器

/createFunction name="update_timestamp", parameters="", returnType="TRIGGER", language="plpgsql", body="BEGIN NEW.updated_at = NOW(); RETURN NEW; END;" /createTrigger name="set_timestamp", tableName="tasks", functionName="update_timestamp", when="BEFORE", events=["UPDATE"], forEach="ROW"

建筑

Docker:

docker build -t mcp/postgres -f Dockerfile .

安全注意事项

  1. 所有数据修改操作都使用具有适当 COMMIT/ROLLBACK 处理的事务
  2. 每个操作都会返回执行的 SQL,以实现透明度
  3. 服务器使用参数化查询进行插入/更新操作,以防止 SQL 注入

执照

此 MCP 服务器采用 MIT 许可证。这意味着您可以自由使用、修改和分发该软件,但须遵守 MIT 许可证的条款和条件。更多详情,请参阅项目仓库中的 LICENSE 文件。

-
security - not tested
F
license - not found
-
quality - not tested

模型上下文协议服务器提供对 PostgreSQL 数据库的读写访问,使 LLM 能够查询数据、修改记录和管理数据库模式。

  1. Components
    1. Tools
    2. Resources
  2. Usage with Claude Desktop
    1. Docker
    2. NPX
  3. Example Usage
    1. Query Data
    2. Insert Data
    3. Update Data
    4. Create a Table
    5. Create a Function and Trigger
  4. Building
    1. Security Considerations
      1. License
        ID: up7bcmit1h