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.
- 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
postgres-mcp-server
├── Dockerfile # Main Docker container configuration for the MCP server
├── Makefile # Build automation and common development tasks
├── README.md # Project documentation and setup instructions
├── docker-compose.yml # Production Docker Compose configuration
├── package.json # Node.js project configuration and dependencies
├── pyproject.toml # Python project configuration
├── tsconfig.json # TypeScript compiler configuration
└── src/
├── config/
│ ├── database.ts # Database connection configuration and setup
│ └── env.ts # Environment variable handling and validation
├── http/
│ ├── Dockerfile # Docker configuration specific to HTTP server mode
│ ├── app.ts # HTTP application setup and Express.js configuration
│ └── httpHandler.ts # HTTP request handlers for MCP protocol over HTTP
├── index.ts # Main entry point for the MCP server
├── resources/
│ ├── databaseSchema.ts # MCP resource for exposing database schema information
│ ├── databaseTables.ts # MCP resource for listing and describing database tables
│ └── helloWorld.ts # Example/test resource implementation
├── server/
│ └── server.ts # Core MCP server implementation and protocol handling
├── stdio/
│ ├── Dockerfile # Docker configuration for stdio communication mode
│ └── stdioServer.ts # MCP server configured for stdio communication
├── stdioIndex.ts # Entry point for stdio-based MCP server
└── tools/
└── queryTool.ts # MCP tool for executing PostgreSQL queries
Copy environment template
cp .env.example .envEdit your database credentials
nano .envDownload node.js and npm from here
# Install dependencies
npm install
# Build the project
npm run buildRun HTTP server in development
npm run dev:httpRun Stdio server in development
npm run dev:stdioBuild and start HTTP server
npm run build
npm run start:httpOr start Stdio server
npm run build
npm run start:stdio- Install podman from here
- Install
uvfrom here - Install podman compose package:
uv add podman-compose(oruv syncto sync packages inpyproject.toml)
#get the environment variables
set -a
source .env
set +a
podman machine start
make podman-upInstall MCP Inspector: instructions: here
cd postgres-mcp-server/ #path to project directory
#from project directory
npx @modelcontextprotocol/inspector npx tsx src/stdioIndex.ts- Install podman from here
- Install
uvfrom here - Install podman compose package:
uv sync(will sync packages inpyproject.toml)
#get the environment variables
set -a
source .env
set +a
#start podman container (if running for the first time)
make podman-up #will construct the container of the MCP server
#if already exists: podman start <name_of_the_container>
npx @modelcontextprotocol/inspectorAfter selecting Streamable HTTP from drop down menu, insert http://localhost:3000/mcp into URL.
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 |
A simple greeting message for testing.
Lists all tables in the public schema with their schema URIs.
Returns column information for a specific table.
Execute read-only SQL queries against the database.
Parameters:
sql(string): The SQL query to execute
| 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 |
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).
-
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
- Create a new file in
src/resources/ - Implement the resource registration function
- Add it to
src/server/server.ts
- Create a new file in
src/tools/ - Implement the tool registration function
- Add it to
src/server/server.ts
MIT
Please read the contributing guidelines and submit pull requests to the main repository.


