PostgreSQL 的 MCP 服务器
PostgreSQL 的模型上下文协议 (MCP) 服务器实现,提供通过 MCP 与 PostgreSQL 数据库交互的简单接口。
特征
使用参数化输入执行 SQL 查询
运行 INSERT/UPDATE/DELETE 操作
创建新数据库
创建或更新表架构
调试 PostgreSQL 连接
使用 Docker 进行容器化,易于部署
基于环境的配置
先决条件
Python 3.8+
PostgreSQL 10+
Docker(可选,用于容器化部署)
Docker Compose(可选,用于开发)
安装
使用 Docker(推荐)
克隆存储库:
git clone https://github.com/asadudin/mcp-server-postgres.git cd mcp-server-postgres复制示例环境文件:
cp .env.example .env使用您的 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使用 Docker Compose 启动服务:
docker-compose up -d
手动安装
克隆存储库:
git clone https://github.com/asadudin/mcp-server-postgres.git cd mcp-server-postgres创建并激活虚拟环境:
python -m venv venv source venv/bin/activate # On Windows: .\venv\Scripts\activate安装依赖项:
pip install -r requirements.txt复制示例环境文件并更新:
cp .env.example .env # Edit .env with your configuration运行服务器:
python mcp_server_postgres.py
用法
MCP 服务器提供以下端点:
sql_query
运行 SELECT 查询并以 JSON 形式返回结果。
参数:
query
:SQL 查询字符串params
:查询参数的可选 JSON 字符串(列表或字典)
例子:
sql_execute
执行 INSERT/UPDATE/DELETE 语句。
参数:
query
:SQL语句params
:查询参数的可选 JSON 字符串(列表或字典)
例子:
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 服务器的端口 |
发展
运行测试
构建 Docker 镜像
API 文档
有关详细的 API 文档,请参阅OpenAPI 规范。
贡献
欢迎贡献代码!欢迎提交 Pull 请求。
执照
该项目根据 MIT 许可证获得许可 - 有关详细信息,请参阅LICENSE文件。
致谢
This server cannot be installed
hybrid server
The server is able to function both locally and remotely, depending on the configuration or use case.
模型上下文协议服务器实现提供了一个简单的接口来与 PostgreSQL 数据库交互,通过 MCP 实现 SQL 查询、数据库操作和模式管理。
Related MCP Servers
- -securityFlicense-qualityA 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 -5837
- AsecurityAlicenseAqualityA 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 -181,016119AGPL 3.0
- -securityFlicense-qualityA Model Context Protocol server that enables performing PostgreSQL database operations (create, read, update, delete) on User and Post entities through MCP tools.Last updated -
- -securityAlicense-qualityA Model Context Protocol server that enables interaction with PostgreSQL databases to list tables, retrieve schemas, and execute read-only SQL queries.Last updated -26MIT License