Connects to a PostgreSQL database to explore schemas, query data using natural language, analyze table statistics, and optionally perform write operations.
Click on "Install 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., "@PostgreSQL MCP Server for Claude DesktopDescribe the users table and show me 5 sample rows."
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.
π PostgreSQL MCP Server for Claude Desktop
Connect Claude Desktop to your PostgreSQL database using the Model Context Protocol (MCP). Ask Claude questions in plain English and it will query your database, explore schemas, and analyze data β no SQL required.
β¨ Features
Category | Tools |
ποΈ Database Info |
|
π Schema Exploration |
|
π Querying |
|
βοΈ Write Operations |
|
π Resources |
|
π¬ Prompts |
|
π Quick Start
1. Clone the Repository
git clone https://github.com/sarotechhub/Claude-Desktop-to-PostgreSQL.git
cd Cluade-MCP-PostgreSQL2. Create a Virtual Environment
# Windows
python -m venv .venv
.venv\Scripts\activate
# macOS / Linux
python -m venv .venv
source .venv/bin/activate3. Install Dependencies
pip install -r requirements.txt4. Configure Your Database
# Copy the example env file
cp .env.example .envEdit .env with your PostgreSQL credentials:
DB_HOST=localhost
DB_PORT=5432
DB_NAME=your_database_name
DB_USER=your_username
DB_PASSWORD=your_password
# Set to "true" to allow INSERT/UPDATE/DELETE
ALLOW_WRITE_OPERATIONS=falseAll credentials live only in β they are never put in the Claude Desktop config file.
5. Test the Server
python main.pyYou should see:
π Starting PostgreSQL MCP Server...
β
Database connection pool ready.Press Ctrl+C to stop.
6. Connect to Claude Desktop
Find your Claude Desktop config file:
OS | Path |
Windows |
|
macOS |
|
Linux |
|
Add the to the mcpServers section (update the path):
{
"mcpServers": {
"postgres": {
"command": "[PATH_TO_YOUR_VENV_PYTHON_EXE]",
"args": ["[PATH_TO_YOUR_PROJECT_ROOT]\\main.py"],
"cwd": "[PATH_TO_YOUR_PROJECT_ROOT]",
"env": {
"PYTHONPATH": "[PATH_TO_YOUR_PROJECT_ROOT]"
}
}
}
}Note: No DB credentials go in this file. The server reads them automatically from your
.envfile usingpython-dotenv.
Restart Claude Desktop after saving the config.
π¬ Example Conversations with Claude
Once connected, try asking Claude:
"List all tables in my database"
"Describe the structure of the users table"
"Show me 10 sample rows from the orders table"
"How many rows are in each table in the public schema?"
"Find all columns related to 'email' across all tables"
"What indexes exist on the products table?"
"Write a query to find the top 10 customers by total order value"
"Explain why this query might be slow: SELECT * FROM orders WHERE status = 'pending'"π§ Tool Reference
Database Info
Tool | Description |
| PostgreSQL version, DB size, connection count, server info |
| All databases on the server with sizes and encoding |
Schema Exploration
Tool | Description |
| All user-defined schemas in the connected database |
| Tables in a schema with row counts and sizes |
| Columns, types, nullable, defaults, PKs, FKs |
| Find tables/columns by keyword (case-insensitive) |
| Indexes on a table with type and columns |
Querying
Tool | Description |
| Read-only SELECT (enforced via read-only transaction) |
| Sample rows from a table (max 100) |
| Row counts, sizes, vacuum/analyze timestamps |
| EXPLAIN execution plan (no data modification) |
Write Operations
Tool | Description |
| INSERT/UPDATE/DELETE β requires |
π Security
Read-only by default β write operations require explicit opt-in in
.envCredentials in β never in the Claude Desktop config
Read-only transactions β SELECT queries run inside
readonly=TruetransactionsInput validation β table/schema names validated with regex to prevent injection
No DDL β DROP, CREATE, ALTER are always blocked even when writes are enabled
.envβ credentials never committed
π Project Structure
Cluade-MCP-PostgreSQL/
βββ main.py # π MCP server entry point (FastMCP + stdio)
βββ database.py # π Async connection pool (asyncpg)
βββ tools/
β βββ __init__.py
β βββ schema_tools.py # list_schemas, list_tables, describe_table, search_schema, list_indexes
β βββ query_tools.py # execute_query, get_table_sample, get_table_stats, explain_query
β βββ write_tools.py # execute_write (opt-in)
β βββ database_tools.py # get_database_info, list_databases
βββ .env # β
Your credentials (NOT committed to git)
βββ .env.example # Template for new users
βββ .gitignore # Excludes .env, .venv, __pycache__
βββ requirements.txt # mcp[cli], asyncpg, python-dotenv, pydantic, orjson
βββ claude_desktop_config.json # Example Claude Desktop config snippet
βββ README.mdβοΈ Environment Variables
All configuration is done via .env:
Variable | Default | Description |
|
| PostgreSQL host |
|
| PostgreSQL port |
| β | Database name |
| β | Database user |
| β | Database password |
|
| Min pool connections |
|
| Max pool connections |
|
| Enable INSERT/UPDATE/DELETE |
|
| Logging level (DEBUG/INFO/WARNING) |
π οΈ Troubleshooting
Claude doesn't see the MCP server
Fully quit and reopen Claude Desktop after editing the config
Check the
cwdpath β it must point to the project folderVerify the
.venvPython path is correct:.venv\Scripts\python.exe
Connection refused / authentication failed
Check your
.envcredentials match your PostgreSQL setupTest directly:
psql -h localhost -U your_user -d your_dbEnsure PostgreSQL is running:
pg_isready
mcp or asyncpg module not found
.venv\Scripts\pip install -r requirements.txtServer starts but tools don't appear in Claude
Open Claude Desktop β Settings β Developer β MCP Servers
Check for error messages next to the
postgresserver entryRun
python main.pymanually and check stderr for import errors
π Requirements
Python 3.10+
PostgreSQL 12+
Claude Desktop (with MCP support)
π License
MIT License β free to use, modify, and distribute.
π€ Contributing
Pull requests welcome! Please fork the repo, create a feature branch, and submit a PR with a clear description.