Skip to main content
Glama

MCP Server for PostgreSQL

by asadudin

PostgreSQL 的 MCP 服务器

许可证:MIT Docker拉取

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

特征

  • 使用参数化输入执行 SQL 查询

  • 运行 INSERT/UPDATE/DELETE 操作

  • 创建新数据库

  • 创建或更新表架构

  • 调试 PostgreSQL 连接

  • 使用 Docker 进行容器化,易于部署

  • 基于环境的配置

Related MCP server: PostgreSQL MCP Server

先决条件

  • 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_PORT

5432

PostgreSQL 端口

PG_用户

PostgreSQL

PostgreSQL 用户名

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

Latest Blog Posts

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