PostgreSQL MCP Server

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.

Category
Visit Server

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

  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)
#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

Claude Desktop Window

Using MCP Server from Claude Desktop

Prompt: Show sales table from last year.

Result

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

Stdio in MCP Inspector

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 Tools in MCP Inspector

MCP Resource: MCP Resource in MCP Inspector

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

  1. Database Connection Errors

    # Check your database credentials in .env
    # Ensure PostgreSQL is running and accessible
    
  2. Port Already in Use

    # Change PORT in .env or stop conflicting services
    lsof -i :3000
    
  3. Docker Build Issues

    # Clean Docker cache
    npm run docker:clean
    docker system prune -a
    
  4. 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

  1. Create a new file in src/resources/
  2. Implement the resource registration function
  3. Add it to src/server/server.ts

Adding New Tools

  1. Create a new file in src/tools/
  2. Implement the tool registration function
  3. Add it to src/server/server.ts

License

MIT

Contributing

Please read the contributing guidelines and submit pull requests to the main repository.

Recommended Servers

playwright-mcp

playwright-mcp

A Model Context Protocol server that enables LLMs to interact with web pages through structured accessibility snapshots without requiring vision models or screenshots.

Official
Featured
TypeScript
Magic Component Platform (MCP)

Magic Component Platform (MCP)

An AI-powered tool that generates modern UI components from natural language descriptions, integrating with popular IDEs to streamline UI development workflow.

Official
Featured
Local
TypeScript
Audiense Insights MCP Server

Audiense Insights MCP Server

Enables interaction with Audiense Insights accounts via the Model Context Protocol, facilitating the extraction and analysis of marketing insights and audience data including demographics, behavior, and influencer engagement.

Official
Featured
Local
TypeScript
VeyraX MCP

VeyraX MCP

Single MCP tool to connect all your favorite tools: Gmail, Calendar and 40 more.

Official
Featured
Local
graphlit-mcp-server

graphlit-mcp-server

The Model Context Protocol (MCP) Server enables integration between MCP clients and the Graphlit service. Ingest anything from Slack to Gmail to podcast feeds, in addition to web crawling, into a Graphlit project - and then retrieve relevant contents from the MCP client.

Official
Featured
TypeScript
Kagi MCP Server

Kagi MCP Server

An MCP server that integrates Kagi search capabilities with Claude AI, enabling Claude to perform real-time web searches when answering questions that require up-to-date information.

Official
Featured
Python
E2B

E2B

Using MCP to run code via e2b.

Official
Featured
Neon Database

Neon Database

MCP server for interacting with Neon Management API and databases

Official
Featured
Exa Search

Exa Search

A Model Context Protocol (MCP) server lets AI assistants like Claude use the Exa AI Search API for web searches. This setup allows AI models to get real-time web information in a safe and controlled way.

Official
Featured
Qdrant Server

Qdrant Server

This repository is an example of how to create a MCP server for Qdrant, a vector search engine.

Official
Featured