Skip to main content
Glama

MCP PostgreSQL Server

MCP PostgreSQL 服务器

提供 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. 连接数据库

使用提供的凭据建立与 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. 查询

使用可选的预处理语句参数执行 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. 执行

使用可选的预处理语句参数执行 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. 列表模式

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

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

5. 列表表

列出已连接数据库中的表。接受可选的 schema 参数(默认为“public”)。

// List tables in the 'public' schema (default) use_mcp_tool({ server_name: "postgres", tool_name: "list_tables", arguments: {} }); // List tables in a specific schema use_mcp_tool({ server_name: "postgres", tool_name: "list_tables", arguments: { schema: "my_schema" } });

6. describe_table

获取特定表的结构。接受可选的 schema 参数(默认为“public”)。

// Describe a table in the 'public' schema (default) use_mcp_tool({ server_name: "postgres", tool_name: "describe_table", arguments: { table: "users" } }); // Describe a table in a specific schema use_mcp_tool({ server_name: "postgres", tool_name: "describe_table", arguments: { table: "users", schema: "my_schema" } });

特征

  • 通过自动清理来安全处理连接
  • 查询参数的预处理语句支持
  • 支持 PostgreSQL 样式 ($1、$2) 和 MySQL 样式 (?) 参数占位符
  • 全面的错误处理和验证
  • TypeScript 支持
  • 自动连接管理
  • 支持 PostgreSQL 特定的语法和功能
  • 数据库操作的多模式支持

安全

  • 使用准备好的语句来防止 SQL 注入
  • 支持通过环境变量进行安全密码处理
  • 执行前验证查询
  • 完成后自动关闭连接

错误处理

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

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

执照

麻省理工学院

Install Server
A
security – no known vulnerabilities
A
license - permissive license
A
quality - confirmed to work

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.

使 AI 模型能够通过标准化接口与 PostgreSQL 数据库交互,支持查询、表操作和模式检查等操作。

  1. 安装
    1. 手动安装
  2. 配置
    1. 可用工具
      1. 1. 连接数据库
      2. 2. 查询
      3. 3. 执行
      4. 4. 列表模式
      5. 5. 列表表
      6. 6. describe_table
    2. 特征
      1. 安全
        1. 错误处理
          1. 执照

            Related MCP Servers

            • A
              security
              A
              license
              A
              quality
              Allows AI assistants to list tables, read data, and execute SQL queries through a controlled interface, making database exploration and analysis safer and more structured.
              Last updated -
              1
              713
              Python
              MIT License
              • Linux
              • Apple
            • A
              security
              F
              license
              A
              quality
              Enables AI models to perform MySQL database operations through a standardized interface, supporting secure connections, query execution, and comprehensive schema management.
              Last updated -
              7
              46
              25
              JavaScript
            • -
              security
              F
              license
              -
              quality
              A server that allows AI models to interact with PostgreSQL databases through a standardized protocol, providing database schema information and SQL query execution capabilities.
              Last updated -
              1
              JavaScript
            • -
              security
              A
              license
              -
              quality
              Enables AI agents to interact with PostgreSQL databases through the Model Context Protocol, providing database schema exploration, table structure inspection, and SQL query execution capabilities.
              Last updated -
              11
              Python
              MIT License
              • Linux
              • Apple

            View all related MCP servers

            MCP directory API

            We provide all the information about MCP servers via our MCP API.

            curl -X GET 'https://glama.ai/api/mcp/v1/servers/antonorlov/mcp-postgres-server'

            If you have feedback or need assistance with the MCP directory API, please join our Discord server