返回市场
麦普 postgres 服务器

麦普 postgres 服务器

作者:antonorlov7 星标更新:2025-04-14

项目介绍

MCP PostgreSQL Server

这是一个提供PostgreSQL数据库操作的Model Context Protocol服务器。该服务器使AI模型能够通过标准化接口与PostgreSQL数据库进行交互。

安装

手动安装

npm install mcp-postgres-server

或者直接运行:

npx mcp-postgres-server

配置

服务器需要以下环境变量:

{
  "mcpServers": {
    "postgres": {
      "type": "stdio",
      "command": "npx",
      "args": ["-y", "mcp-postgres-server"],
      "env": {
        "PG_HOST": "your_host",
        "PG_PORT": "5432",
        "PG_USER": "your_user",
        "PG_PASSWORD": "your_password",
        "PG_DATABASE": "your_database"
      }
    }
  }
}

可用工具

1. connect_db

使用提供的凭据连接到PostgreSQL数据库。

use_mcp_tool({
  server_name: "postgres",
  tool_name: "connect_db",
  arguments: {
    host: "localhost",
    port: 5432,
    user: "your_user",
    password: "your_password",
    database: "your_database"
  }
});

2. query

执行带有可选预处理语句参数的SELECT查询。支持PostgreSQL风格($1, $2)和MySQL风格(?)的参数占位符。

use_mcp_tool({
  server_name: "postgres",
  tool_name: "query",
  arguments: {
    sql: "SELECT * FROM users WHERE id = $1",
    params: [1]
  }
});

3. execute

执行带有可选预处理语句参数的INSERT、UPDATE或DELETE查询。支持PostgreSQL风格($1, $2)和MySQL风格(?)的参数占位符。

use_mcp_tool({
  server_name: "postgres",
  tool_name: "execute",
  arguments: {
    sql: "INSERT INTO users (name, email) VALUES ($1, $2)",
    params: ["John Doe", "john@example.com"]
  }
});

4. list_schemas

列出连接数据库中的所有模式。

use_mcp_tool({
  server_name: "postgres",
  tool_name: "list_schemas",
  arguments: {}
});

5. list_tables

列出连接数据库中的表。接受一个可选的模式参数(默认为'public')。

// 列出'public'模式下的表(默认)
use_mcp_tool({
  server_name: "postgres",
  tool_name: "list_tables",
  arguments: {}
});

// 列出特定模式下的表
use_mcp_tool({
  server_name: "postgres",
  tool_name: "list_tables",
  arguments: {
    schema: "my_schema"
  }
});

6. describe_table

获取特定表的结构。接受一个可选的模式参数(默认为'public')。

// 描述'public'模式下的表(默认)
use_mcp_tool({
  server_name: "postgres",
  tool_name: "describe_table",
  arguments: {
    table: "users"
  }
});

// 描述特定模式下的表
use_mcp_tool({
  server_name: "postgres",
  tool_name: "describe_table",
  arguments: {
    table: "users",
    schema: "my_schema"
  }
});

特性

  • 安全连接处理并自动清理
  • 支持查询参数的预处理语句
  • 支持PostgreSQL风格($1, $2)和MySQL风格(?)的参数占位符
  • 全面的错误处理和验证
  • 支持TypeScript
  • 自动连接管理
  • 支持PostgreSQL特有的语法和功能
  • 多模式支持用于数据库操作

安全

  • 使用预处理语句防止SQL注入
  • 通过环境变量支持安全密码处理
  • 在执行前验证查询
  • 当完成时自动关闭连接

错误处理

服务器为常见问题提供了详细的错误消息:

  • 连接失败
  • 无效查询
  • 缺失参数
  • 数据库错误

许可证

MIT