MCP Server for PostgreSQL

MCP Server for PostgreSQL

A Model Context Protocol server implementation that provides a simple interface to interact with PostgreSQL databases, enabling SQL queries, database operations, and schema management through MCP.

Category
Visit Server

README

MCP Server for PostgreSQL

License: MIT Docker Pulls

A Model Context Protocol (MCP) server implementation for PostgreSQL, providing a simple interface to interact with PostgreSQL databases through MCP.

Features

  • Execute SQL queries with parameterized inputs
  • Run INSERT/UPDATE/DELETE operations
  • Create new databases
  • Create or update table schemas
  • Debug PostgreSQL connections
  • Containerized with Docker for easy deployment
  • Environment-based configuration

Prerequisites

  • Python 3.8+
  • PostgreSQL 10+
  • Docker (optional, for containerized deployment)
  • Docker Compose (optional, for development)

Installation

Using Docker (Recommended)

  1. Clone the repository:

    git clone https://github.com/asadudin/mcp-server-postgres.git
    cd mcp-server-postgres
    
  2. Copy the example environment file:

    cp .env.example .env
    
  3. Update the .env file with your PostgreSQL credentials:

    PG_HOST=postgres
    PG_PORT=5432
    PG_USER=postgres
    PG_PASSWORD=your_password
    PG_DATABASE=your_database
    HOST=0.0.0.0
    PORT=8056
    
  4. Start the service using Docker Compose:

    docker-compose up -d
    

Manual Installation

  1. Clone the repository:

    git clone https://github.com/asadudin/mcp-server-postgres.git
    cd mcp-server-postgres
    
  2. Create and activate a virtual environment:

    python -m venv venv
    source venv/bin/activate  # On Windows: .\venv\Scripts\activate
    
  3. Install dependencies:

    pip install -r requirements.txt
    
  4. Copy the example environment file and update it:

    cp .env.example .env
    # Edit .env with your configuration
    
  5. Run the server:

    python mcp_server_postgres.py
    

Usage

The MCP server provides the following endpoints:

sql_query

Run a SELECT query and return results as JSON.

Parameters:

  • query: SQL query string
  • params: Optional JSON string of query parameters (list or dict)

Example:

{
  "query": "SELECT * FROM users WHERE id = $1",
  "params": [1]
}

sql_execute

Execute an INSERT/UPDATE/DELETE statement.

Parameters:

  • query: SQL statement
  • params: Optional JSON string of query parameters (list or dict)

Example:

{
  "query": "INSERT INTO users (name, email) VALUES ($1, $2)",
  "params": ["John Doe", "john@example.com"]
}

create_database

Create a new PostgreSQL database.

Parameters:

  • database_name: Name of the database to create

create_or_update_table

Create or update a table schema.

Parameters:

  • sql: CREATE TABLE or ALTER TABLE SQL statement

debug_postgres_connection

Debug the PostgreSQL connection.

Environment Variables

Variable Default Description
PG_HOST localhost PostgreSQL host
PG_PORT 5432 PostgreSQL port
PG_USER postgres PostgreSQL username
PG_PASSWORD PostgreSQL password
PG_DATABASE postgres Default database name
HOST 0.0.0.0 Host to bind the MCP server to
PORT 8056 Port to run the MCP server on

Development

Running Tests

# Install test dependencies
pip install -r requirements-dev.txt

# Run tests
pytest

Building the Docker Image

docker build -t mcp-server-postgres .

API Documentation

For detailed API documentation, refer to the OpenAPI specification.

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Acknowledgments

  • FastMCP - The MCP server framework
  • asyncpg - PostgreSQL client for Python
  • FastAPI - Modern, fast web framework for building APIs

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