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 "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., "@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: PostgreSQL MCP Server
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 installed
Maintenance
Resources
Unclaimed servers have limited discoverability.
Looking for Admin?
If you are the server author, to access and configure the admin panel.
Latest Blog Posts
- Your AI Chatbot Just Exposed Your CEO's Salary to an InternBy Om-Shree-0709 on .Agent IdentityMCP SecurityOAuth Delegation
- Why MCP Servers Need Execution Sandboxing (And Why Your Current Stack Isn't Enough)By Om-Shree-0709 on .Agentic AiPrompt InjectionWebAssembly
MCP directory API
We provide all the information about MCP servers via our MCP API.
curl -X GET 'https://glama.ai/api/mcp/v1/servers/anilsrml/postgres-mcp-server'
If you have feedback or need assistance with the MCP directory API, please join our Discord server