PostgreSQL MCP Server
Provides tools for exploring database schemas, executing read-only queries, and optionally performing write operations on a PostgreSQL database.
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., "@PostgreSQL MCP ServerShow me the schema of the users table"
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 |
|
Related MCP server: My Credentials MCP Server
š 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
.envā 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 postgres block 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
.envonly ā never in the Claude Desktop configRead-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
.envexcluded from git ā 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.
Claude-Desktop-to-PostgreSQL
This server cannot be deployed
Maintenance
Related MCP Connectors
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.
Query your Postgres from ChatGPT or Claude without exposing the database or handing over credentials. Run npx boltschema connect next to your database and it dials out over HTTPS ā no inbound firewall rule, no open port, works with localhost and VPC-private databases. Read-only is enforced by a SQL guard, a Postgres READ ONLY transaction, and a scoped role generated for you.
Connect your team's living knowledge base ā docs, data, issues, CRM ā to Claude and ChatGPT.
Related MCP Servers
- FlicenseNot gradedqualityDmaintenanceProvides Claude Desktop with secure access to multiple database connections, allowing users to query MySQL, PostgreSQL, SQLite, and SQL Server databases directly through natural language.-
- AlicenseNot gradedqualityDmaintenanceEnables secure chat-based interaction with PostgreSQL databases through Claude Desktop. Features GitHub OAuth authentication, role-based access control, and enterprise-grade security for database queries and operations.1MIT
- FlicenseNot gradedqualityDmaintenanceEnables Claude Desktop to interact with PostgreSQL databases through natural language for schema exploration, data analysis, and query execution. Users can search schemas, describe tables, and perform read or write operations without needing to write manual SQL.-
- FlicenseNot gradedqualityDmaintenanceEnables Claude Desktop to query a PostgreSQL brand database through MCP. Supports local stdio and remote HTTP/SSE deployments with API key authentication for secure database access.-