PostgreSQL多环境数据库服务

一个提供跨多环境读写PostgreSQL数据库能力的MCP服务器,支持数据库模式检查、查询执行、数据修改和模式对象管理。

  • by Pixelworlds
  • TypeScript
  • 本地部署
  • 更新于
在 GitHub 查看 pixelworlds/postgres-multi-mcp-server

使用说明

PostgreSQL多环境MCP服务器

一个模型上下文协议服务器,在多个环境中提供对PostgreSQL数据库的读写访问。此服务器使LLM能够检查数据库模式、执行查询、修改数据、创建/修改数据库模式对象,以及在不同的数据库环境(生产、测试、演示、开发等)之间切换。

注: 这是一个基于 增强型PostgreSQL MCP服务器 由Gareth Cottrell所著,其本身即基于原作 PostgreSQL MCP服务器 通过Anthropic。原始服务器提供对单个数据库的只读访问,增强版本添加了写入功能和模式管理,而此多环境版本添加了对具有运行时配置的无限数据库环境的支持。

组件

工具

数据查询

  • 怎么翻译
    • 对连接的数据库执行只读SQL查询
    • 输入: sql (string):要执行的SQL查询
    • 所有查询都在只读事务中执行

数据修改

  • 执行

    • 执行修改数据的SQL语句(INSERT、UPDATE、DELETE)
    • 输入: sql (string):要执行的SQL语句
    • 在具有适当COMMIT/ROLLBACK处理的事务中执行
  • 插入

    • 将新记录插入表中
    • 输入:
      • table (string):表名
      • data (object):键值对,其中键是列名,值是要插入的数据
  • 更新

    • 更新表中的记录
    • 输入:
      • table (string):表名
      • data (object):要更新的字段的键值对
      • where (string):用于标识要更新的记录的WHERE条件
  • 删除

    • 从表中删除记录
    • 输入:
      • table (string):表名
      • where (string):用于标识要删除的记录的WHERE条件

模式管理

  • 创建表

    • 使用指定的列和约束创建新表
    • 输入:
      • tableName (string):表名
      • columns (array):具有名称、类型和可选约束的列定义数组
      • constraints (array):表级约束的可选数组
  • createFunction

    • 创建PostgreSQL函数/过程
    • 输入:
      • name (string):函数名称
      • parameters (string):函数参数
      • returnType (string):返回类型
      • language (string):语言(plpgsql、sql等)
      • body (string):函数体
      • options (string):可选附加功能选项
  • createTrigger

    • 在桌子上创建触发器
    • 输入:
      • name (string):触发器名称
      • tableName (string):应用触发器的表
      • functionName (string):要调用的函数
      • when (string):之前、之后或代替
      • events (array):事件数组(INSERT、UPDATE、DELETE)
      • forEach (string):ROW或STATEMENT
      • condition (string):可选WHEN条件
  • createIndex

    • 在表上创建索引
    • 输入:
      • tableName (string):表名
      • indexName (string):索引名称
      • columns (数组):要索引的列
      • unique (boolean):索引是否唯一
      • type (string):可选索引类型(BTREE、HASH、GIN、GIST等)
      • where (string):可选条件
  • alterTable

    • 更改表结构
    • 输入:
      • tableName (string):表名
      • operation (string):操作(添加列、删除列等)
      • details (string):操作细节

资源

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

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

配置

服务器支持直接从MCP服务器配置传递的多个数据库配置。这允许您在不同的数据库(生产、测试、演示、开发或任何自定义环境)之间切换,而无需重新启动服务器。

配置结构

该配置支持使用标准化结构的任意数量的环境。配置通过命令行参数以JSON字符串的形式提供。

配置结构为:

{
  "environments": [
    {
      "name": "production",
      "displayName": "Production",
      "database": {
        "type": "postgres",
        "host": "prod-db.example.com",
        "port": 5432,
        "database": "prod_app",
        "username": "prod_user",
        "password": "prod_password",
        "ssl": true,
        "poolSize": 10
      }
    },
    {
      "name": "staging",
      "displayName": "Staging",
      "database": {
        "type": "postgres",
        "host": "staging-db.example.com",
        "port": 5432,
        "database": "staging_app",
        "username": "staging_user",
        "password": "staging_password",
        "ssl": true,
        "poolSize": 5
      }
    },
    {
      "name": "demo",
      "displayName": "Demo",
      "database": {
        "type": "postgres",
        "host": "demo-db.example.com",
        "port": 5432,
        "database": "demo_app",
        "username": "demo_user",
        "password": "demo_password",
        "ssl": true,
        "poolSize": 3
      }
    },
    {
      "name": "development",
      "displayName": "Development",
      "database": {
        "type": "postgres",
        "connectionString": "postgresql://dev_user:dev_password@localhost:5432/myapp_dev",
        "poolSize": 2
      }
    }
  ]
}

添加自定义环境

为了添加新的环境(例如。, testing, qa, preprod),只需将它们添加到 environments 数组:

{
  "name": "testing",
  "displayName": "Testing Environment",
  "database": {
    "type": "postgres",
    "host": "test-db.example.com",
    "port": 5432,
    "database": "test_app",
    "username": "test_user",
    "password": "test_password",
    "ssl": true,
    "poolSize": 2
  }
}

使用Claude Desktop

将配置作为JSON字符串参数传递:

{
  "mcpServers": {
    "postgres": {
      "command": "node",
      "args": [
        "dist/index.js",
        "{\"environments\":[{\"name\":\"production\",\"displayName\":\"Production\",\"database\":{\"type\":\"postgres\",\"host\":\"prod-db.example.com\",\"port\":5432,\"database\":\"prod_app\",\"username\":\"prod_user\",\"password\":\"prod_password\",\"ssl\":true,\"poolSize\":10}},{\"name\":\"staging\",\"displayName\":\"Staging\",\"database\":{\"type\":\"postgres\",\"host\":\"staging-db.example.com\",\"port\":5432,\"database\":\"staging_app\",\"username\":\"staging_user\",\"password\":\"staging_password\",\"ssl\":true,\"poolSize\":5}},{\"name\":\"development\",\"displayName\":\"Development\",\"database\":{\"type\":\"postgres\",\"connectionString\":\"postgresql://dev_user:dev_password@localhost:5432/myapp_dev\",\"poolSize\":2}}]}"
      ]
    }
  }
}

为了更好的可读性,配置JSON(格式化后)看起来像:

{
  "environments": [
    {
      "name": "production",
      "displayName": "Production", 
      "database": {
        "type": "postgres",
        "host": "prod-db.example.com",
        "port": 5432,
        "database": "prod_app",
        "username": "prod_user",
        "password": "prod_password",
        "ssl": true,
        "poolSize": 10
      }
    },
    {
      "name": "staging",
      "displayName": "Staging",
      "database": {
        "type": "postgres", 
        "host": "staging-db.example.com",
        "port": 5432,
        "database": "staging_app",
        "username": "staging_user",
        "password": "staging_password",
        "ssl": true,
        "poolSize": 5
      }
    },
    {
      "name": "development",
      "displayName": "Development",
      "database": {
        "type": "postgres",
        "connectionString": "postgresql://dev_user:dev_password@localhost:5432/myapp_dev",
        "poolSize": 2
      }
    }
  ]
}

码头工人

{
  "mcpServers": {
    "postgres": {
      "command": "docker",
      "args": [
        "run", 
        "-i", 
        "--rm",
        "mcp/postgres-multi",
        "{\"environments\":[{\"name\":\"production\",\"displayName\":\"Production\",\"database\":{\"type\":\"postgres\",\"host\":\"prod-db.example.com\",\"port\":5432,\"database\":\"prod_app\",\"username\":\"prod_user\",\"password\":\"prod_password\",\"ssl\":true,\"poolSize\":10}}]}"
      ]
    }
  }
}

示例用法

数据库管理

# List all available database configurations
/listDatabases

# Switch to a specific database
/switchDatabase database="production"
/switchDatabase database="staging"
/switchDatabase database="demo"
/switchDatabase database="development"
/switchDatabase database="testing"  # Custom environment

查询数据

/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-multi -f Dockerfile . 

安全考虑

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

许可证

此MCP服务器根据MIT许可证获得许可。这意味着您可以根据MIT许可证的条款和条件自由使用、修改和分发软件。有关更多详细信息,请参阅项目存储库中的LICENSE文件。