Sponsored by Deepsite.site

PostgreSQL MCP Server

Created By
aadarshjaina year ago
MCP Server (STDIO) for PostgreSQL Database
Content

PostgreSQL MCP Server

A Model Context Protocol (MCP) server implementation for PostgreSQL database exploration and querying.

Table of Contents

Overview

This project provides a Model Context Protocol (MCP) server that connects to a PostgreSQL database and exposes various data discovery and querying capabilities as MCP tools. These tools allow you to explore database structure, query data, and get database statistics through a standardized interface.

Features

  • Connect to any PostgreSQL database using a standard connection string
  • Execute custom SELECT queries with results returned in a structured format
  • List all tables in the connected database
  • Get detailed schema information for any table
  • Filter database records with simple equality conditions
  • Get database statistics including size, table count, and largest tables

Requirements

  • Python 3.7+
  • Required Python packages (listed in requirements.txt):
    • mcp
    • pg8000
    • sqlalchemy

Installation

  1. Clone this repository or download the source code
  2. Create a virtual environment (recommended)
  3. Install the required packages:
pip install -r requirements.txt

Usage

Basic Usage

Run the server by providing a PostgreSQL connection string:

python server.py postgresql://username:password@hostname:port/database

Using with Claude Desktop

To integrate with Claude Desktop, add the following configuration to your Claude Desktop config file:

{
  "mcpServers": {
    "postgres": {
      "command": "python",
      "args": [
        "/ABOSLUTE/PATH/TO/postgres-mcp-server/server.py",
        "postgresql://user:password@host:port/database"
      ]
    }
  }
}

After adding or making any changes to above config file, make sure to relaunch the claude desktop by force quitting it.

Configuration File Location

The Claude Desktop configuration file is typically located at:

  • macOS: ~/Library/Application\ Support/Claude/claude_desktop_config.json
  • Windows: %APPDATA%\Claude\claude_desktop_config.json
  • Linux: ~/.config/Claude/claude_desktop_config.json

Available Tools

The server provides the following MCP tools:

Tools

execute_query

Execute a custom SELECT SQL query and return the results.

list_tables

List all tables in the current PostgreSQL database.

get_table_schema

Get the schema definition for a specified table.

filter_instances

Filter database records based on specified criteria.

get_database_stats

Get general statistics and metadata about the PostgreSQL database.

Security Considerations

  • This server only allows SELECT queries to prevent database modifications
  • Connection credentials are provided via command line and not stored
  • Consider using environment variables or a secure configuration method in production environments

Example Interaction in Claude

Once connected to Claude Desktop, you can ask questions like:

  • "What tables are available in the database?"
  • "Show me the schema for the customers table"
  • "How many records are in the orders table?"
  • "What's the total size of the database?"

Output

Claude will use the appropriate MCP tools to answer these questions by querying your PostgreSQL database.

Troubleshooting

  • If connection fails, verify your PostgreSQL connection string is correct

  • Check that your PostgreSQL server allows connections from your client machine

  • Server Not Found: Ensure the paths in your configuration file are correct and absolute

    Use absolute path for command by running where python

    For example - "command": "python" should be written a "command": "/Users/username/miniconda3/bin/python"

  • Permission Issues: Make sure the Claude Desktop app has permission to execute the script

  • Server Crashes: Check the server logs for errors and ensure all dependencies are installed

Contact

Feel free to raise issue on this repository.

Recommend Servers
TraeBuild with Free GPT-4.1 & Claude 3.7. Fully MCP-Ready.
WindsurfThe new purpose-built IDE to harness magic
Jina AI MCP ToolsA Model Context Protocol (MCP) server that integrates with Jina AI Search Foundation APIs.
ChatWiseThe second fastest AI chatbot™
Playwright McpPlaywright MCP server
MiniMax MCPOfficial MiniMax Model Context Protocol (MCP) server that enables interaction with powerful Text to Speech, image generation and video generation APIs.
Tavily Mcp
Howtocook Mcp基于Anduin2017 / HowToCook (程序员在家做饭指南)的mcp server,帮你推荐菜谱、规划膳食,解决“今天吃什么“的世纪难题; Based on Anduin2017/HowToCook (Programmer's Guide to Cooking at Home), MCP Server helps you recommend recipes, plan meals, and solve the century old problem of "what to eat today"
MCP AdvisorMCP Advisor & Installation - Use the right MCP server for your needs
RedisA Model Context Protocol server that provides access to Redis databases. This server enables LLMs to interact with Redis key-value stores through a set of standardized tools.
Serper MCP ServerA Serper MCP Server
Zhipu Web SearchZhipu Web Search MCP Server is a search engine specifically designed for large models. It integrates four search engines, allowing users to flexibly compare and switch between them. Building upon the web crawling and ranking capabilities of traditional search engines, it enhances intent recognition capabilities, returning results more suitable for large model processing (such as webpage titles, URLs, summaries, site names, site icons, etc.). This helps AI applications achieve "dynamic knowledge acquisition" and "precise scenario adaptation" capabilities.
CursorThe AI Code Editor
Visual Studio Code - Open Source ("Code - OSS")Visual Studio Code
AiimagemultistyleA Model Context Protocol (MCP) server for image generation and manipulation using fal.ai's Stable Diffusion model.
BlenderBlenderMCP connects Blender to Claude AI through the Model Context Protocol (MCP), allowing Claude to directly interact with and control Blender. This integration enables prompt assisted 3D modeling, scene creation, and manipulation.
DeepChatYour AI Partner on Desktop
EdgeOne Pages MCPAn MCP service designed for deploying HTML content to EdgeOne Pages and obtaining an accessible public URL.
Amap Maps高德地图官方 MCP Server
Baidu Map百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。
Y GuiA web-based graphical interface for AI chat interactions with support for multiple AI models and MCP (Model Context Protocol) servers.