About MCP PostgreSQL Demo
MCP PostgreSQL Demo is a FastMCP server for LLMs that need to explore and query PostgreSQL databases. It exposes schema and table details as resources, permits SELECT queries, and returns results in YAML.
Key features
- 01Explore database schema metadata
- 02Inspect table structures
- 03Run SELECT-only queries
- 04Return results in YAML
- 05Provide schema, table, and query prompts
Use cases
- 01Let an LLM inspect schemas and table structures
- 02Query data in PostgreSQL tables
- 03Read schema or table details through resources
MCP support
The MCP server exposes schema and table details as resources, a tool restricted to SELECT queries, and prompts for describing schemas, tables, and requested data.
Configuration
Local server config is the same for every client. Copy it into your client’s MCP config file.
We haven’t collected a package or remote endpoint for this server. Follow the instructions in the project README.
Tools
Local servers expose tools only once running on your machine, and the maintainer has not provided a tool list.
Details
From the project READMEMCP 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 schemadatabase://{schema}/tables/{table}- Get detailed information about a specific table
Tools
query_database- Execute SQL queries against the database (SELECT queries only)
Prompts
The server includes the following predefined prompts:
prompt_schema_description- Ask for a description of a database schemaprompt_table_description- Ask for a description of a specific tableprompt_query_database- Ask for data from a specific table
Prerequisites
- Python 3.12 or higher
- PostgreSQL database
- UV package manager (recommended)
Installation
-
Clone the repository:
git clone <repository-url> cd mcp-demo -
Create a virtual environment:
python -m venv .venv source .venv/bin/activate # On Windows: .venv\Scripts\activate -
Install UV (if not already installed):
pip install uv -
Install dependencies with UV:
uv sync -
Configure environment variables:
- Copy
.env.exampleto.env - Update the values according to your PostgreSQL configuration
- Copy
Configuration
The application is configured using environment variables:
| Variable | Description | Default |
|---|---|---|
| APP_NAME | Application name | mcp-demo |
| DB_HOST | PostgreSQL host | localhost |
| DB_PORT | PostgreSQL port | 5432 |
| DB_USER | PostgreSQL username | postgres |
| DB_PASSWORD | PostgreSQL password | postgres |
| DB_NAME | PostgreSQL database name | postgres |
Usage
-
First, uncomment the run function in
src/main.pyby removing the comment from these lines at the bottom of the file:# if __name__ == "__main__": # print("Starting FastMCP server...") # mcp.run() -
Start the FastMCP server:
python -m src.main -
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 (e.g., .cursor/mcp.json):
{
"mcpServers": {
"postgres-mcp-server": {
"command": "/path/to/your/venv/bin/mcp",
"args": ["run", "/path/to/your/postgres-mcp/src/main.py"],
"env": {
"APP_NAME": "mcp-demo",
"DB_HOST": "localhost",
"DB_PORT": "5432",
"DB_USER": "postgres",
"DB_PASSWORD": "postgres",
"DB_NAME": "postgres"
}
}
}
}
Be sure to replace the paths with the actual paths to your virtual environment and project directory, and update the environment variables to match your PostgreSQL configuration.
Development
Install development dependencies with UV:
uv pip install -e ".[dev]"
Development tools included:
- JupyterLab for notebooks
- Pyright for type checking
- Ruff for linting
Docker
To run the application with Docker:
-
Build the Docker image:
docker build -t mcp-demo . -
Run the container:
docker run --env-file .env.docker -p 8000:8000 mcp-demo
Example Usage
Get Schema Information
from mcp.client import get_client
client = get_client("http://localhost:8000")
schema_info = client.get_resource("database://public")
print(schema_info)
Get Table Details
table_info = client.get_resource("database://public/tables/users")
print(table_info)
Execute a Query
result = client.invoke_tool("query_database", {"query": "SELECT * FROM users LIMIT 10"})
print(result)
License
[Add your license information here]
Contributors
- Ricardo Santos [email protected]
Comments
New to MCP?
From what the protocol is to installing your first server, these answers cover most first questions.
An open protocol that defines how AI applications connect to external tools and data in a uniform way. Clients talk to the model; MCP servers provide tools, resources and prompt templates. They exchange standard messages, so one server can be reused by many clients.