PostgreSQL MCP Server

PostgreSQL MCP Server

Enables direct execution of PostgreSQL queries through the Model Context Protocol. Supports both SELECT and non-SELECT operations with dual communication modes (stdio and HTTP REST API).

Category
Visit Server

README

🚀 PostgreSQL MCP Server

A lightweight Model Context Protocol (MCP) server for executing PostgreSQL queries.

This project enables an MCP-compatible client to run SQL queries (both SELECT and non-SELECT) directly on a PostgreSQL database. The server supports both standard input/output and HTTP (REST API) communication modes.


🧩 What is MCP (Model Context Protocol)?

MCP is a universal protocol for connecting external tools, data sources, and services to AI models in a standardized way.

  • Why MCP? MCP eliminates the need for custom integrations per tool, providing a common API for interacting with various resources.
  • How does this project fit in? This repo gives you an MCP server for PostgreSQL:
    • Send SQL queries from any MCP-compatible client.
    • Receive results in structured JSON.
    • Seamlessly use PostgreSQL as a knowledge base or app datastore.

✨ Features

  • Execute direct PostgreSQL queries via MCP.
  • Supports:
    • Data retrieval: SELECT queries return rows.
    • Database changes: INSERT, UPDATE, DELETE, CREATE, and more return success status.
  • Dual operation modes:
    • stdio (standard in/out)
    • http (REST API server)
  • Structured logging with loguru.
  • Built for educational, hackable use (not hardened for production).

⚡ Requirements

  • Python 3.9+
  • A running PostgreSQL database
  • Dependencies:
    • psycopg2 (SQL driver)
    • loguru (logging)
    • mcp (Model Context Protocol library)

📂 Project Structure

.
├── .venv/              # Local virtual environment (should be gitignored)
├── .gitignore          # Ignore rules for git
├── .python-version     # Python version file
├── code.txt            # (Optional: dev notes or scratch work)
├── main.py             # CLI entry point to start the server
├── pyproject.toml      # Project dependencies and metadata
├── README.md           # This documentation file
├── server.py           # PostgreSQL MCP server implementation
└── uv.lock             # Dependency lockfile for reproducible installs

🛠 Setup & Usage

1. Clone the repository

git clone https://github.com/<your-username>/<your-repo-name>.git
cd <your-repo-name>

2. Install dependencies

uv sync

3. Run the server

HTTP mode:

python server.py --mode http --port 9000

Stdio mode (for integration as a tool):

python server.py --mode stdio

🧰 Development Notes

  • All logic is contained in server.py.
  • main.py should be a minimal CLI entry point that parses arguments and starts the server.
  • Feel free to reorganize into a package structure if your codebase grows.

👨‍💻 Author

Created with ❤️ by Loki


Tip: Remember to update your .gitignore to exclude .venv/, __pycache__/, and other cache files for a clean repository.


Happy querying

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