AP

ahmedmustahid/postgres-mcp-server

开发工具

本页由 AI 翻译,技术细节请以原文为准。 查看原文

31 stars 16 forks 质量 56 趋势 0

ahmedmustahid/postgres-mcp-server 是从 https://github.com/ahmedmustahid/postgres-mcp-server. 导入的 MCP 服务器候选

概览

ahmedmustahid/postgres-mcp-server 是从 https://github.com/ahmedmustahid/postgres-mcp-server. 导入的 MCP 服务器候选

概述(AI 翻译)

一个模型上下文协议(MCP)服务器,提供HTTP和Stdio传输,用于与PostgreSQL数据库交互。该服务器通过两种传输方式展示数据库资源和工具,允许在不同环境中灵活集成。

特色

  • 双重传输支持:同时支持 HTTP(可流式HTTPServerTransport)和 Stdio(StdioServerTransport)
  • 数据库资源:列表表并检索模式信息
  • 查询工具:执行只读SQL查询
  • 有状态会话:HTTP传输支持会话管理
  • Docker 支持:两种传输的容器化部署
  • 生产准备:优雅关机、错误处理和日志记录

快速启动

环境设置

数据库凭证必须传递为:

  1. 任一环境变量

  2. 或在工作目录(该目录中将执行npx命令): 创建一个 .env 文件(包使用 dotenv 包)

使用 npx 跑步

  1. 从这里下载node.js和NPM

  2. 运行该软件包。默认情况下,它会在3000端口上运行可流式HTTP:

  3. 用于市级交通:

环境设置

复制环境模板

本页由 AI 翻译,技术细节请以原文为准。

README

以下为原文 README 节选,完整内容请查看原文。

MCP PostgreSQL Server (Stateful and Dual Transport)

A Model Context Protocol (MCP) server that provides both HTTP and Stdio transports for interacting with PostgreSQL databases. This server exposes database resources and tools through both transport methods, allowing for flexible integration in different environments.

Features

  • Dual Transport Support: Both HTTP (StreamableHTTPServerTransport) and Stdio (StdioServerTransport)
  • Database Resources: List tables and retrieve schema information
  • Query Tool: Execute read-only SQL queries
  • Stateful Sessions: HTTP transport supports session management
  • Docker Support: Containerized deployments for both transports
  • Production Ready: Graceful shutdown, error handling, and logging

Quick Start

Environment Setup

The database credentials must be passed as:

  1. Either environment variables
# PostgreSQL Database Connection String
export POSTGRES_URL=your_connection_string

# PostgreSQL Database Configuration if POSTGRES_URL is not provided
export POSTGRES_USERNAME=your_username
export POSTGRES_PASSWORD=your_password
export POSTGRES_HOST=localhost
export POSTGRES_PORT=5432 # this is the default
export POSTGRES_DATABASE=your_database

# HTTP Server Configuration
# Following are the default values
export PORT=3000
export HOST=0.0.0.0

# CORS Configuration (comma-separated list of allowed origins)
# Following are the default values
export CORS_ORIGIN=http://localhost:8080,http://localhost:3000

# Environment
# Following are the default values
export NODE_ENV=development

  1. Or in the working directory (directory where npx command will be run): create a .env file (the package uses dotenv package)
# .env.example
# PostgreSQL Database Configuration
POSTGRES_USERNAME=your_username
POSTGRES_PASSWORD=your_password
POSTGRES_HOST=localhost
POSTGRES_DATABASE=your_database

# HTTP Server Configuration
PORT=3000
HOST=0.0.0.0

# CORS Configuration (comma-separated list of allowed origins)
CORS_ORIGIN=http://localhost:8080,http://localhost:3000

# Environment
NODE_ENV=development

Run using npx

  1. Download node.js and npm from here
  2. Run the package. By default, it will run streamable http on port 3000:
npx @ahmedmustahid/postgres-mcp-server
# or npx @ahmedmustahid/postgres-mcp-server --port 3000 --verbose
  1. For stdio transport:
npx @ahmedmustahid/postgres-mcp-server stdio
# npx @ahmedmustahid/postgres-mcp-server stdio --verbose

Environment Setup

Copy environment template

cp .env.example .env

Edit your database credentials

nano .env

Podman(or Docker) Usage

  1. Install podman from here
  2. Install uv from here
  3. Install podman compose package: uv add podman-compose (or uv sync to sync packages in pyproject.toml)
在 GitHub 查看完整 README

安装

npx @ahmedmustahid/postgres-mcp-server stdio
新手?先看 mcpskill.org 教程 →

配置

{ "mcpServers": { "postgres-mcp-server": { "command": "npx", "args": [ "@ahmedmustahid/postgres-mcp-server", "stdio" ], "env": { "POSTGRES_USERNAME": "your-username", "POSTGRES_PASSWORD": "your-password", "POSTGRES_HOST": "hostname", "POSTGRES_DATABASE": "database-name" } } } }