postgres-mcp-server
Provides tools for querying and modifying PostgreSQL databases, including schema analysis, natural language querying, and secure write operations with two-phase approval.
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., "@postgres-mcp-servershow me the schema of the orders 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 DBQA with MCP (Model Context Protocol)
This project acts as a secure bridge connecting your PostgreSQL database to Artificial Intelligence (AI) models (like Claude, Cursor, etc.).
By leveraging the Model Context Protocol (MCP), it enables AI models to understand your database schema, securely query it (SELECT), and perform controlled data modifications (INSERT, UPDATE, DELETE).
Features
Schema Analysis: Automatically introduces database tables, columns, and relationships to the AI.
Natural Language Querying: AI translates natural language questions into SQL and retrieves results.
Secure Write Operations:
Supports
INSERT,UPDATE,DELETEoperations.Two-Phase Approval: AI first previews the affected rows using
modify_data, then confirms the modification withconfirm_modification.Kill Switch: Write permissions can be instantly disabled with
WRITE_ENABLED=false.
Related MCP server: Postgres Scout MCP
7-Layer Security Model
This server employs a multi-layered protection system to ensure data safety:
DDL Blocking: Structural modification commands like
DROP,TRUNCATE,ALTER,CREATEare always strictly forbidden.WHERE Clause Enforcement:
UPDATEandDELETEqueries are prevented from running without aWHEREclause.Row Limit: The maximum number of rows affected by a single query is limited (Default: 100).
Table Whitelist: Write permission is granted only to tables specified in the
.envfile.Two-Phase Transaction: Write operations first run in "Dry-Run" (preview) mode to calculate the number of affected rows.
Separate Isolation: Distinct connection managers and validators are used for read and write operations.
Audit Log: All operations are logged in detail.
Installation (Docker)
Docker image oluşturun:
docker build -t postgres-mcp-server .Bir
.envdosyası oluşturun (.env.example'dan kopyalayabilirsiniz):copy .env.example .env # Windows cp .env.example .env # Linux/Mac.envdosyasında veritabanı URI'nizi ayarlayın:# Bağlantı URI - tek satırda tüm bilgiler DATABASE_URI=postgresql://username:password@host:port/dbname # Yazma İşlemleri WRITE_ENABLED=true WRITABLE_TABLES=customers,orders,products # Boş = tüm tablolar MAX_WRITE_ROWS=100
Usage
AI Client Configuration (Claude Desktop / Cursor)
Add the following to your AI client config (e.g., claude_desktop_config.json or Cursor MCP settings):
{
"mcpServers": {
"postgres-dbq": {
"command": "docker",
"args": [
"run", "-i", "--rm", "--network", "host",
"-e", "DATABASE_URI",
"-e", "WRITE_ENABLED",
"-e", "WRITABLE_TABLES",
"-e", "MAX_WRITE_ROWS",
"postgres-mcp-server"
],
"env": {
"DATABASE_URI": "postgresql://postgres:your_password@localhost:5432/your_database",
"WRITE_ENABLED": "true",
"WRITABLE_TABLES": "",
"MAX_WRITE_ROWS": "100"
}
}
}
}Note:
--network hostallows the container to accesslocalhostservices (e.g., PostgreSQL running on the host). On Windows/Mac Docker Desktop, you may also usehost.docker.internalin the URI instead oflocalhost.
Local Development (without Docker)
pip install -r requirements.txt
python mcp_server.pyProject Structure
mcp_server.py: Main MCP server file. Tool definitions are located here.src/database/:executor.py: SQL execution engine (preview_writeandexecute_writemethods).schema_manager.py: Module analyzing the database schema.
src/validation/:sql_validator.py: SQL security checks and validation logic.rules.py: Forbidden keywords and limit definitions.
src/config.py: Pydantic-based configuration management.Dockerfile: Docker image definition.
Environment Variables
Variable | Required | Default | Description |
| ✅ | — | PostgreSQL connection URI ( |
| ❌ |
| Enable write operations |
| ❌ |
| Comma-separated list of writable tables |
| ❌ |
| Max rows affected per write query |
| ❌ |
| Query timeout in seconds |
| ❌ |
| Max rows returned per SELECT |
Security Warning
This tool possesses powerful capabilities. When WRITE_ENABLED=true is set, your AI model can make changes to the database.
Configure
WRITABLE_TABLESstrictly before using in a Production environment.Ensure you have regular backups of your critical data.
This server cannot be deployed
Maintenance
Related MCP Connectors
Query PostgreSQL databases in plain English — LLM-generated, safety-validated SQL.
Safe, read-only Postgres and MySQL access for AI agents. Audit log + column-level controls.
Deterministic safety, correctness & cost gate that vets Postgres SQL before your AI agent runs it.
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.
Related MCP Servers
- FlicenseAqualityDmaintenanceEnables AI assistants to interact with PostgreSQL databases using natural language queries, providing secure read-only access to database schemas and SQL translation capabilities.612 npm-
- AlicenseNot gradedqualityDmaintenanceEnables AI assistants to safely explore, analyze, and maintain PostgreSQL databases with read-only mode by default, SQL injection prevention, query performance analysis, and optional write operations.37 npmApache 2.0
- AlicenseNot gradedqualityDmaintenanceEnables AI agents to interact with PostgreSQL databases through schema intelligence, query execution, and DBA tooling including index analysis and health monitoring. Features configurable access levels and audit logging for secure database operations.300 npmMIT
- AlicenseNot gradedqualityDmaintenanceEnables secure, read-only PostgreSQL database interaction through natural language, with automatic database discovery and connection management.2MIT