mcp-postgres-db-claude
Provides tools for interacting with a PostgreSQL database, including listing tables, describing table schemas, and running read-only SQL queries with configurable result limits and statement timeouts.
Click on "Deploy Server".
Wait a few minutes for the server to deploy. Once ready, it will show a "Started" state.
In the chat, type
@followed by the MCP server name and your instructions, e.g., "@mcp-postgres-db-claudeWhich customer generated the most paid revenue?"
That's it! The server will respond to your query, and you can continue using it as needed.
Here is a step-by-step guide with screenshots.
I Gave Claude Access to My PostgreSQL Database
A practical MCP project for The Curious Architect.
The project connects:
Claude → MCP → Python MCP Server → PostgreSQL
Claude can inspect the database schema and ask the MCP server to run read-only SQL queries.
What this demonstrates
MCP tools
MCP resources
Streamable HTTP transport
Python MCP SDK v2
PostgreSQL with
psycopgSchema discovery
AI-generated SQL
Read-only database access
A dedicated PostgreSQL read-only user
Query result limiting and statement timeout
Related MCP server: postgres-mcp-server
Project structure
mcp-postgres-claude/
├── server.py
├── pyproject.toml
├── .env.example
├── docker-compose.yml
├── db/
│ └── init.sql
└── README.md1. Start PostgreSQL
Make sure Docker is installed, then:
docker compose up -dThe demo database contains:
customersorders
Check it:
docker compose ps2. Create the Python environment
Python 3.10+ is required.
Using uv:
python3 -m venv .venv
source .venv/bin/activate
uv pip install -e .Or with pip:
python -m venv .venv
source .venv/bin/activate
pip install -e .The current official MCP Python SDK is v2, and pip install mcp now installs the v2 line. The server therefore uses from mcp.server import MCPServer rather than the old v1 FastMCP import.
3. Configure the database
cp .env.example .envThe example points to the Docker PostgreSQL instance:
DATABASE_URL=postgresql://mcp_readonly:mcp_readonly_password@localhost:5432/companyThe database user is intentionally read-only.
4. Start the MCP server
python server.pyThe server starts at:
http://127.0.0.1:8000/mcpYou can also use the MCP CLI during development:
mcp dev server.py5. Test with MCP Inspector
For the HTTP server, connect the Inspector to:
http://127.0.0.1:8000/mcpTry:
list_tables
What tables are available?describe_table
Describe the orders table.query_database
SELECT
c.country,
COUNT(*) AS orders,
SUM(o.amount) AS revenue
FROM customers c
JOIN orders o ON o.customer_id = c.id
WHERE o.status = 'paid'
GROUP BY c.country
ORDER BY revenue DESCThe model can generate this query from a natural-language request such as:
Show me revenue by country for paid orders.
6. Connect Claude
Expose the local MCP endpoint through a secure tunnel if your Claude setup requires a public HTTPS endpoint.
For example, with ngrok:
ngrok config add-authtoken <TOKEN>
ngrok http 8000Use the HTTPS MCP endpoint:
https://YOUR-NGROK-DOMAIN/mcpThen add it as an MCP connector in the Claude environment you are using.
Do not expose a production database directly to the public internet.
Available MCP primitives
Tools
list_tables
Returns tables in the public schema.
describe_table
Returns column names, data types, nullability, and defaults.
query_database
Runs one read-only SELECT or WITH query and returns structured rows.
Resource
postgres://schema
Provides a compact representation of the database schema so an MCP host can understand the available tables and columns.
Safety design
This project is intentionally designed as a demo with multiple read-only boundaries.
1. Database user
The MCP server connects as:
mcp_readonlyThat user receives SELECT permission only.
2. Read-only transaction
Every query is executed inside a PostgreSQL read-only transaction.
3. SQL validation
The MCP tool only accepts queries beginning with SELECT or WITH and blocks common write/administrative operations.
4. Row limit
Results are capped by MAX_ROWS, defaulting to 50.
5. Statement timeout
Queries are limited by STATEMENT_TIMEOUT_MS, defaulting to 5000 ms.
These are demo safeguards, not a complete production security model. For production, add authentication/authorization, stronger SQL policy enforcement, auditing, least-privilege schemas, network controls, and carefully designed domain-specific tools.
Good Claude demo prompts
Try these in the video:
What tables do I have?How many customers are in each country?Which customer generated the most paid revenue?Show me monthly paid revenue for 2026.What is the average paid order value?Find customers who have never placed an order.A stronger demo is:
Analyze the database and tell me the top 3 countries by paid revenue.
Explain how you calculated it and show the SQL query you used.Architecture
Natural language
│
▼
┌───────────┐
│ Claude │
└─────┬─────┘
│ MCP
▼
┌─────────────────────┐
│ Python MCP Server │
│ │
│ list_tables │
│ describe_table │
│ query_database │
│ postgres://schema │
└──────────┬──────────┘
│ psycopg
▼
┌─────────────────────┐
│ PostgreSQL │
│ │
│ customers │
│ orders │
└─────────────────────┘Video positioning
The point of this project is not to teach PostgreSQL.
The story is:
What happens when an AI assistant can actually understand and query a real database?
MCP is the bridge that exposes controlled database capabilities to Claude.
Next project ideas
This can naturally lead into:
MCP + PostgreSQL + authentication
MCP + PostgreSQL + charts
MCP + multiple databases
MCP + APIs + PostgreSQL
Build an AI Data Analyst with MCP
This server cannot be deployed
Maintenance
Related MCP Connectors
- dataOAuthco.thinair
PostgreSQL, MySQL, and SQL Server in one session. 26 read-only MCP tools for AI agents.
Query your org's data in natural language — read-only MCP access to SQL, NoSQL, files & warehouses.
Query 40 databases from Claude, ChatGPT, or Cursor — on any device. Read-only, encrypted, audited.
Query PostgreSQL databases in plain English — LLM-generated, safety-validated SQL.
Related MCP Servers
- FlicenseNot gradedqualityDmaintenanceEnables interaction with PostgreSQL databases through MCP, allowing users to explore database structures, inspect table schemas, and execute read-only SQL queries.-
- AlicenseNot gradedqualityCmaintenanceEnables inspecting database schemas and executing read-only SQL queries on a PostgreSQL database via MCP tools.2 npmMIT
- FlicenseNot gradedqualityCmaintenanceEnables AI assistants to execute SQL queries and inspect PostgreSQL database schemas via MCP tools.-
- AlicenseNot gradedqualityCmaintenanceEnables read-only, SELECT-only querying of any Postgres database through MCP-compatible clients like Claude, with schema introspection and guarded SQL execution.MIT