Back to MCP directory
publicPublicdnsLocal runtime

mcp-postgres-server

MCP PostgreSQL服务器是一个标准化的接口服务,允许AI模型通过工具调用与PostgreSQL数据库进行交互,支持连接管理、查询执行、表结构查看等功能。

article

README

🚀 MCP PostgreSQL 服务器

MCP PostgreSQL 服务器是一个提供 PostgreSQL 数据库操作的模型上下文协议(MCP)服务器。它为 AI 模型提供了标准化接口,使其能够与 PostgreSQL 数据库进行交互。

🚀 快速开始

MCP PostgreSQL 服务器可助力 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"
  }
});

✨ 主要特性

  • 安全连接处理,带有自动清理功能
  • 支持预处理语句以防止 SQL 注入
  • 支持 PostgreSQL 样式($1, $2)和 MySQL 样式 (?) 参数占位符
  • 全面的错误处理和验证
  • TypeScript 支持
  • 自动化连接管理
  • 支持 PostgreSQL 特定语法和功能
  • 多架构支持

🔒 安全性

  • 使用预处理语句防止 SQL 注入
  • 通过环境变量支持密码等敏感信息的安全存储
  • 强化的身份验证机制

🛠️ 错误处理

  • 明确的错误返回机制
  • 详细的错误日志记录
  • 可自定义的错误处理逻辑

📄 许可证

``` (原文档此处许可证内容为空,故保留原样)
help

Runtime guide

cloud

Hosted runtime

Hosted servers run from a provider-managed environment. You usually connect the MCP client to the hosted endpoint or follow the provider's authorization flow, without keeping a local process alive

  1. Open provider connection page
  2. Authorize or copy endpoint
  3. Connect from your MCP client
terminal

Local runtime / other methods

Local servers run on your own machine or infrastructure. You normally copy the server_config into your MCP client, install the required package, and provide env variables from env_schema when needed

  1. Copy server_config
  2. Install required package
  3. Fill env variables and restart client