
PostgreSQL MCP Server
A Model Context Protocol server providing dual transport (HTTP and Stdio) access to PostgreSQL databases, allowing AI assistants to query databases and fetch schema information through natural language.
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:
- Either environment variables
# PostgreSQL Database Configuration
export POSTGRES_USERNAME=your_username
export POSTGRES_PASSWORD=your_password
export POSTGRES_HOST=localhost
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
- Or in the working directory (directory where
npx
command will be run): create a.env
file (the package usesdotenv
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
- Download node.js and npm from here
- 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
- 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
- Install podman from here
- Install
uv
from here - Install podman compose package:
uv add podman-compose
(oruv sync
to sync packages inpyproject.toml
)
#get the environment variables
set -a
source .env
set +a
podman machine start
make podman-up
Test using Claude Desktop
First, install node.js and npm, build the project following the above instructions.
Edit your claude_desktop_config.json
{
"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"
}
}
}
}
Check if MCP Server has been enabled
Verify from Claude Desktop Window
Using MCP Server from Claude Desktop
Prompt: Show sales
table from last year.
Test using MCP Inspector
First, install node.js and npm, build the project following the above instructions. Install MCP Inspector: instructions: here
Check Stdio MCP Server
npx @modelcontextprotocol/inspector npx @ahmedmustahid/postgres-mcp-server stdio
Check Streamable HTTP MCP Server
First, run the server (shell where environment has been configured):
npx @ahmedmustahid/postgres-mcp-server
Run the mcp inspector from another terminal
npx @modelcontextprotocol/inspector
After selecting Streamable HTTP
from drop down menu, insert http://localhost:3000/mcp
(default) into URL.
MCP tools:
MCP Resource:
Configuration
Environment Variables
You have to specify these inside the .env file.
Variable | Description | Default | Required |
---|---|---|---|
POSTGRES_USERNAME |
PostgreSQL username | - | Yes |
POSTGRES_PASSWORD |
PostgreSQL password | - | Yes |
POSTGRES_HOST |
PostgreSQL host | - | Yes |
POSTGRES_DATABASE |
PostgreSQL database name | - | Yes |
PORT |
HTTP server port | 3000 | No |
HOST |
HTTP server host | 0.0.0.0 | No |
CORS_ORIGIN |
Allowed CORS origins (comma-separated) | localhost:8080,localhost:3000 | No |
NODE_ENV |
Environment mode | development | No |
Resources
Hello World (hello://world
)
A simple greeting message for testing.
Database Tables (database://tables
)
Lists all tables in the public schema with their schema URIs.
Database Schema (database://tables/{tableName}/schema
)
Returns column information for a specific table.
Tools
query
Execute read-only SQL queries against the database.
Parameters:
sql
(string): The SQL query to execute
Transport Differences
Feature | HTTP Transport | Stdio Transport |
---|---|---|
Session Management | ✅ Stateful sessions | ❌ Stateless |
Concurrent Connections | ✅ Multiple clients | ❌ Single process |
Web Integration | ✅ REST API compatible | ❌ CLI only |
Interactive Use | ✅ Via HTTP clients | ✅ Direct stdio |
Docker Deployment | ✅ Web service | ✅ CLI container |
Health Checks
The HTTP server includes a basic health check endpoint accessible at the /health
endpoint with a GET request (returns 405 Method Not Allowed, confirming the server is responsive).
Troubleshooting
Common Issues
-
Database Connection Errors
# Check your database credentials in .env # Ensure PostgreSQL is running and accessible
-
Port Already in Use
# Change PORT in .env or stop conflicting services lsof -i :3000
-
Docker Build Issues
# Clean Docker cache npm run docker:clean docker system prune -a
-
Session Management (HTTP)
# Sessions are stored in memory and will reset on server restart # For production, consider implementing persistent session storage
Development
Adding New Resources
- Create a new file in
src/resources/
- Implement the resource registration function
- Add it to
src/server/server.ts
Adding New Tools
- Create a new file in
src/tools/
- Implement the tool registration function
- Add it to
src/server/server.ts
License
MIT
Contributing
Please read the contributing guidelines and submit pull requests to the main repository.
推荐服务器

Baidu Map
百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。
Playwright MCP Server
一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。
Magic Component Platform (MCP)
一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。
Audiense Insights MCP Server
通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。

VeyraX
一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。
graphlit-mcp-server
模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。
Kagi MCP Server
一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。

e2b-mcp-server
使用 MCP 通过 e2b 运行代码。
Neon MCP Server
用于与 Neon 管理 API 和数据库交互的 MCP 服务器
Exa MCP Server
模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。