Sponsored by Deepsite.site

PostgreSQL MCP Server

Created By
tobecrazy8 months ago
A postgresql-mcp server
Content

PostgreSQL MCP Server

A Model Context Protocol (MCP) server that provides CRUD operations for PostgreSQL database tables.

Overview

This MCP server allows you to interact with PostgreSQL databases through a set of tools that provide Create, Read, Update, and Delete (CRUD) operations on specified tables. The server uses the FastMCP library and runs in stdio mode, making it compatible with various MCP clients.

Features

  • Connect to PostgreSQL databases
  • Perform CRUD operations on specified tables
  • Table and column-level access control
  • Schema inspection
  • Custom SQL query execution
  • Configuration via YAML file

Installation

  1. Clone this repository:

    git clone https://github.com/yourusername/postgresql-mcp.git
    cd postgresql-mcp
    
  2. Install the required dependencies:

    pip install -r requirements.txt
    

Configuration

The server is configured using the config.yaml file. This file contains:

  1. Database connection details
  2. Table configurations, including:
    • Which tables are accessible
    • Which columns are allowed for operations
    • Which operations (create, read, update, delete) are allowed

Example configuration:

database:
  host: localhost
  port: 5432
  dbname: postgres
  user: postgres
  password: postgres
  
tables:
  - name: users
    allowed_columns:
      - id
      - name
      - email
      - created_at
    allowed_operations:
      - create
      - read
      - update
      - delete
  
  - name: products
    allowed_columns:
      - id
      - name
      - price
      - description
      - category
    allowed_operations:
      - create
      - read
      - update
      - delete

Usage

Run the MCP server:

python postgresql_mcp_server.py

The server will start in stdio mode, ready to receive commands from an MCP client.

Available MCP Tools

list_tables

Lists all tables available in the configuration.

response = list_tables()

create_record

Creates a new record in the specified table.

response = create_record(
    table_name="users",
    data={
        "name": "John Doe",
        "email": "john@example.com"
    }
)

read_records

Reads records from the specified table with optional filtering.

response = read_records(
    table_name="users",
    filters={"name": "John Doe"},
    limit=10,
    offset=0
)

update_record

Updates a record in the specified table.

response = update_record(
    table_name="users",
    record_id=1,
    data={"email": "newemail@example.com"},
    id_column="id"
)

delete_record

Deletes a record from the specified table.

response = delete_record(
    table_name="users",
    record_id=1,
    id_column="id"
)

execute_query

Executes a custom SQL query.

response = execute_query(
    query="SELECT * FROM users WHERE age > %s",
    params=[18]
)

get_table_schema

Gets the schema information for a specific table.

response = get_table_schema(table_name="users")

Response Format

All tools return responses in a standard format:

{
  "status": "success",
  "message": "Optional message",
  "records": [...],  // For read operations
  "record": {...},   // For create/update operations
  "count": 10        // For read operations
}

Or in case of an error:

{
  "status": "error",
  "message": "Error message"
}

Security Considerations

  • The execute_query tool allows arbitrary SQL execution, which could be a security risk. Consider restricting its use or implementing additional validation.
  • Database credentials are stored in plain text in the config.yaml file. Consider using environment variables or a secure secret management solution in production.
  • The server validates table and column access based on the configuration, but it's important to ensure that the configuration itself is secure.

License

MIT License

Recommend Servers
TraeBuild with Free GPT-4.1 & Claude 3.7. Fully MCP-Ready.
AiimagemultistyleA Model Context Protocol (MCP) server for image generation and manipulation using fal.ai's Stable Diffusion model.
MCP AdvisorMCP Advisor & Installation - Use the right MCP server for your needs
ChatWiseThe second fastest AI chatbot™
TimeA Model Context Protocol server that provides time and timezone conversion capabilities. This server enables LLMs to get current time information and perform timezone conversions using IANA timezone names, with automatic system timezone detection.
Jina AI MCP ToolsA Model Context Protocol (MCP) server that integrates with Jina AI Search Foundation APIs.
DeepChatYour AI Partner on Desktop
CursorThe AI Code Editor
MiniMax MCPOfficial MiniMax Model Context Protocol (MCP) server that enables interaction with powerful Text to Speech, image generation and video generation APIs.
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"
Visual Studio Code - Open Source ("Code - OSS")Visual Studio Code
WindsurfThe new purpose-built IDE to harness magic
Serper MCP ServerA Serper MCP Server
Tavily Mcp
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.
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.
EdgeOne Pages MCPAn MCP service designed for deploying HTML content to EdgeOne Pages and obtaining an accessible public URL.
Playwright McpPlaywright MCP server
Baidu Map百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。
Context7Context7 MCP Server -- Up-to-date code documentation for LLMs and AI code editors
Amap Maps高德地图官方 MCP Server