postgres-mcp

postgres-mcp

3

The MCP PostgreSQL Demo is a FastMCP server designed for enabling language models to interact with PostgreSQL databases. It facilitates database schema exploration and table inspection while allowing query execution using the Model Context Protocol.

MCP PostgreSQL Demo

A FastMCP server that enables LLMs to connect and interact with PostgreSQL databases. This project demonstrates how to use the Model Context Protocol (MCP) to allow Language Models to query and explore database schemas and tables.

Features

  • Schema Exploration: Retrieve metadata about database schemas
  • Table Inspection: Get detailed information about table structures
  • Database Querying: Execute SQL queries against the database
  • YAML Formatting: Results are returned in YAML format for easy consumption by LLMs

Resources

The server exposes the following MCP resources:

  • database://{schema} - Get information about all tables in a schema
  • database://{schema}/tables/{table} - Get detailed information about a specific table

Tools

  • query_database - Execute SQL queries against the database (SELECT queries only)

Prerequisites

  • Python 3.12 or higher
  • PostgreSQL database
  • UV package manager (recommended)

Usage

  1. First, uncomment the run function in src/main.py by removing the comment from these lines at the bottom of the file:

  2. Start the FastMCP server. The server will be available for LLMs to connect to and query your PostgreSQL database. With the server running, the MCP can be loaded into client applications for interaction.

Client Configuration

To use this MCP in a client application, add the following configuration to your client's MCP configuration file.