Skip to main content
Glama

MCP Server for PostgreSQL

by asadudin

PostgreSQL 的 MCP 服务器

PostgreSQL 的模型上下文协议 (MCP) 服务器实现,提供通过 MCP 与 PostgreSQL 数据库交互的简单接口。

特征

  • 使用参数化输入执行 SQL 查询
  • 运行 INSERT/UPDATE/DELETE 操作
  • 创建新数据库
  • 创建或更新表架构
  • 调试 PostgreSQL 连接
  • 使用 Docker 进行容器化,易于部署
  • 基于环境的配置

先决条件

  • Python 3.8+
  • PostgreSQL 10+
  • Docker(可选,用于容器化部署)
  • Docker Compose(可选,用于开发)

安装

使用 Docker(推荐)

  1. 克隆存储库:
    git clone https://github.com/asadudin/mcp-server-postgres.git cd mcp-server-postgres
  2. 复制示例环境文件:
    cp .env.example .env
  3. 使用您的 PostgreSQL 凭证更新.env文件:
    PG_HOST=postgres PG_PORT=5432 PG_USER=postgres PG_PASSWORD=your_password PG_DATABASE=your_database HOST=0.0.0.0 PORT=8056
  4. 使用 Docker Compose 启动服务:
    docker-compose up -d

手动安装

  1. 克隆存储库:
    git clone https://github.com/asadudin/mcp-server-postgres.git cd mcp-server-postgres
  2. 创建并激活虚拟环境:
    python -m venv venv source venv/bin/activate # On Windows: .\venv\Scripts\activate
  3. 安装依赖项:
    pip install -r requirements.txt
  4. 复制示例环境文件并更新:
    cp .env.example .env # Edit .env with your configuration
  5. 运行服务器:
    python mcp_server_postgres.py

用法

MCP 服务器提供以下端点:

sql_query

运行 SELECT 查询并以 JSON 形式返回结果。

参数:

  • query :SQL 查询字符串
  • params :查询参数的可选 JSON 字符串(列表或字典)

例子:

{ "query": "SELECT * FROM users WHERE id = $1", "params": [1] }

sql_execute

执行 INSERT/UPDATE/DELETE 语句。

参数:

  • query :SQL语句
  • params :查询参数的可选 JSON 字符串(列表或字典)

例子:

{ "query": "INSERT INTO users (name, email) VALUES ($1, $2)", "params": ["John Doe", "john@example.com"] }

create_database

创建一个新的 PostgreSQL 数据库。

参数:

  • database_name :要创建的数据库的名称

create_or_update_table

创建或更新表模式。

参数:

  • sql :CREATE TABLE 或 ALTER TABLE SQL 语句

debug_postgres_connection

调试 PostgreSQL 连接。

环境变量

多变的默认描述
PG_HOST本地主机PostgreSQL 主机
PG_PORT5432PostgreSQL 端口
PG_用户PostgreSQLPostgreSQL 用户名
PG_密码PostgreSQL 密码
PG_数据库PostgreSQL默认数据库名称
主持人0.0.0.0绑定 MCP 服务器的主机
港口8056运行 MCP 服务器的端口

发展

运行测试

# Install test dependencies pip install -r requirements-dev.txt # Run tests pytest

构建 Docker 镜像

docker build -t mcp-server-postgres .

API 文档

有关详细的 API 文档,请参阅OpenAPI 规范

贡献

欢迎贡献代码!欢迎提交 Pull 请求。

执照

该项目根据 MIT 许可证获得许可 - 有关详细信息,请参阅LICENSE文件。

致谢

  • FastMCP - MCP 服务器框架
  • asyncpg - Python 的 PostgreSQL 客户端
  • FastAPI - 用于构建 API 的现代、快速 Web 框架
-
security - not tested
F
license - not found
-
quality - not tested

hybrid server

The server is able to function both locally and remotely, depending on the configuration or use case.

模型上下文协议服务器实现提供了一个简单的接口来与 PostgreSQL 数据库交互,通过 MCP 实现 SQL 查询、数据库操作和模式管理。

  1. 特征
    1. 先决条件
      1. 安装
        1. 使用 Docker(推荐)
        2. 手动安装
      2. 用法
        1. sql_query
        2. sql_execute
        3. create_database
        4. create_or_update_table
        5. debug_postgres_connection
      3. 环境变量
        1. 发展
          1. 运行测试
          2. 构建 Docker 镜像
        2. API 文档
          1. 贡献
            1. 执照
              1. 致谢

                Related MCP Servers

                • -
                  security
                  F
                  license
                  -
                  quality
                  A Model Context Protocol server providing both read and write access to PostgreSQL databases, enabling LLMs to query data, modify records, and manage database schemas.
                  Last updated -
                  141
                  7
                  JavaScript
                • A
                  security
                  A
                  license
                  A
                  quality
                  A Model Context Protocol server that enables powerful PostgreSQL database management capabilities including analysis, schema management, data migration, and monitoring through natural language interactions.
                  Last updated -
                  18
                  438
                  89
                  TypeScript
                  AGPL 3.0
                  • Linux
                  • Apple
                • -
                  security
                  F
                  license
                  -
                  quality
                  A Model Context Protocol server that enables performing PostgreSQL database operations (create, read, update, delete) on User and Post entities through MCP tools.
                  Last updated -
                  TypeScript
                • -
                  security
                  A
                  license
                  -
                  quality
                  A Model Context Protocol server that enables interaction with PostgreSQL databases to list tables, retrieve schemas, and execute read-only SQL queries.
                  Last updated -
                  35
                  JavaScript
                  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/asadudin/mcp-server-postgres'

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