|
1 |
| -# PostgreSQL MCP Server |
| 1 | +# 🔄 PostgreSQL MCP Server – AI-Powered PostgreSQL Management & Monitoring |
2 | 2 |
|
3 |
| -A comprehensive Model Context Protocol (MCP) server for PostgreSQL database integration. This server provides extensive tools, resources, and prompts for complete PostgreSQL database management through AI assistants like Claude. |
| 3 | +> A powerful, AI-integrated PostgreSQL Model Context Protocol (MCP) server for **automated database operations, monitoring, security, diagnostics, and optimization**. Seamlessly manage PostgreSQL with tools and prompts designed for **AI assistants like Claude and ChatGPT**. |
4 | 4 |
|
5 |
| -## Features |
| 5 | +[](https://python.org) |
| 6 | +[](https://opensource.org/licenses/MIT) |
| 7 | +[](https://www.postgresql.org/) |
| 8 | +[]() |
6 | 9 |
|
7 |
| -This server offers over 200 specialized PostgreSQL tools covering all aspects of database administration, monitoring, and optimization. |
| 10 | +**GitHub Repository**: [https://github.com/mukul975/postgres-mcp-server.git](https://github.com/mukul975/postgres-mcp-server.git) |
| 11 | + |
| 12 | +--- |
| 13 | + |
| 14 | +## 📌 Why Use This? |
| 15 | + |
| 16 | +* 🔍 Explore, diagnose & optimize PostgreSQL databases with **over 200 specialized tools**. |
| 17 | +* 💬 Interact naturally using **AI prompts** for SQL generation, health checks, and performance reviews. |
| 18 | +* 🛡️ Built-in security, permission validation, and error handling. |
| 19 | +* ⚙️ Developer-ready integration for **Claude**, **ChatGPT**, and custom agents. |
| 20 | + |
| 21 | +--- |
| 22 | + |
| 23 | +## 🔑 Features at a Glance |
8 | 24 |
|
9 | 25 | ### 🔧 Core Database Operations
|
10 |
| -- **Database Management**: Create, list, and manage databases |
11 |
| -- **Schema Operations**: Create, drop, and manage schemas |
12 |
| -- **Table Operations**: List, describe, analyze tables with detailed statistics |
13 |
| -- **Query Execution**: Safe SELECT queries and controlled UPDATE/INSERT/DELETE operations |
14 |
| -- **Index Management**: Create, drop, analyze index usage and effectiveness |
15 |
| - |
16 |
| -### 📊 Advanced Analytics & Monitoring |
17 |
| -- **Performance Analysis**: Query performance, slow query analysis, execution plan examination |
18 |
| -- **Resource Monitoring**: Buffer cache analysis, memory usage, I/O statistics |
19 |
| -- **Lock Analysis**: Deadlock detection, lock contention monitoring, blocking queries |
20 |
| -- **Replication Monitoring**: Replication status, lag analysis, slot management |
21 |
| -- **Vacuum & Maintenance**: Autovacuum monitoring, table bloat analysis, maintenance recommendations |
22 |
| - |
23 |
| -### 🛡️ Security & Administration |
24 |
| -- **User Management**: Create, drop, manage users and roles with detailed permissions |
25 |
| -- **Privilege Management**: Grant/revoke permissions, security auditing |
26 |
| -- **Connection Management**: Monitor active connections, analyze connection patterns |
27 |
| -- **Backup Analysis**: Backup status monitoring and recovery readiness assessment |
28 |
| - |
29 |
| -### 🚀 Performance Optimization |
30 |
| -- **Index Optimization**: Unused index detection, redundancy analysis, effectiveness metrics |
31 |
| -- **Query Optimization**: Plan cache analysis, query complexity assessment |
32 |
| -- **Buffer Pool Tuning**: Hit ratio analysis, cache efficiency metrics |
33 |
| -- **Configuration Tuning**: Parameter recommendations and system health checks |
34 |
| - |
35 |
| -### 📈 Advanced Diagnostics |
36 |
| -- **Transaction Analysis**: Long-running transactions, wraparound monitoring |
37 |
| -- **Checkpoint Monitoring**: Checkpoint efficiency and timing analysis |
38 |
| -- **WAL Analysis**: Write-ahead log generation and archiving status |
39 |
| -- **Extension Management**: Installed extensions and usage analysis |
40 |
| - |
41 |
| -### 📊 Resources |
42 |
| -- **postgres://tables/{schema}**: Get table information for a schema |
43 |
| -- **postgres://table/{schema}/{table_name}**: Get detailed table structure |
44 |
| - |
45 |
| -### 💬 Prompts |
46 |
| -- **analyze_table**: Generate analysis prompts for database tables |
47 |
| -- **query_builder**: Help build SQL queries with proper guidance |
48 |
| - |
49 |
| -## Installation |
50 |
| - |
51 |
| -1. Create a virtual environment (recommended): |
52 |
| -```bash |
53 |
| -# Create virtual environment |
54 |
| -python -m venv venv |
55 | 26 |
|
56 |
| -# Activate virtual environment |
57 |
| -# On Windows: |
58 |
| -venv\Scripts\activate |
59 |
| -# On macOS/Linux: |
60 |
| -source venv/bin/activate |
61 |
| -``` |
| 27 | +* ✅ Create/list databases & schemas |
| 28 | +* ✅ Describe tables, analyze indexes |
| 29 | +* ✅ Safe SELECT, UPDATE, DELETE execution |
| 30 | + |
| 31 | +### 📊 Monitoring & Performance |
| 32 | + |
| 33 | +* 📈 Query performance & execution plan insights |
| 34 | +* 🔒 Lock analysis, replication lag checks |
| 35 | +* 🚀 Index, query, and buffer pool optimization |
| 36 | + |
| 37 | +### 🛡️ Security & Access Control |
| 38 | + |
| 39 | +* 👥 User creation, role auditing |
| 40 | +* 🔐 Privilege granting & connection monitoring |
| 41 | + |
| 42 | +### ⚙️ Diagnostic Utilities |
| 43 | + |
| 44 | +* 🧪 Table bloat check, autovacuum insights |
| 45 | +* 💽 WAL logs, checkpoints, long transaction alerts |
| 46 | + |
| 47 | +--- |
| 48 | + |
| 49 | +## 🚀 Quick Start |
| 50 | + |
| 51 | +### 1. Installation |
62 | 52 |
|
63 |
| -2. Install dependencies: |
64 | 53 | ```bash
|
| 54 | +python -m venv venv |
| 55 | +source venv/bin/activate # or venv\Scripts\activate on Windows |
65 | 56 | pip install -r requirements.txt
|
66 | 57 | ```
|
67 | 58 |
|
68 |
| -3. Set up your database connection: |
69 |
| -```bash |
70 |
| -# Copy the example environment file |
71 |
| -copy .env.example .env |
| 59 | +### 2. Configuration |
72 | 60 |
|
73 |
| -# Edit .env with your PostgreSQL connection details |
74 |
| -DATABASE_URL=postgresql://username:password@localhost:5432/database_name |
| 61 | +```bash |
| 62 | +cp .env.example .env # or use copy on Windows |
| 63 | +# Update with: |
| 64 | +DATABASE_URL=postgresql://username:password@localhost:5432/db |
75 | 65 | ```
|
76 | 66 |
|
77 |
| -## Usage |
| 67 | +### 3. Run the Server |
78 | 68 |
|
79 |
| -### Running the Server |
80 |
| - |
81 |
| -#### Direct execution: |
82 | 69 | ```bash
|
83 | 70 | python postgres_server.py
|
84 | 71 | ```
|
85 | 72 |
|
86 |
| -#### With MCP development tools (if mcp CLI is installed): |
| 73 | +> ✅ For **MCP CLI users**: |
| 74 | +
|
87 | 75 | ```bash
|
88 | 76 | mcp dev postgres_server.py
|
89 | 77 | ```
|
90 | 78 |
|
91 |
| -### Configuration with Claude Desktop |
| 79 | +--- |
| 80 | + |
| 81 | +## 🧠 Integrate with Claude Desktop |
| 82 | + |
| 83 | +#### Example config (`claude_desktop_config.json`): |
92 | 84 |
|
93 |
| -Add the following to your Claude Desktop configuration file (`claude_desktop_config.json`): |
| 85 | +<details> |
| 86 | +<summary>Windows</summary> |
94 | 87 |
|
95 |
| -**Windows:** |
96 | 88 | ```json
|
97 | 89 | {
|
98 | 90 | "mcpServers": {
|
99 | 91 | "postgres": {
|
100 | 92 | "command": "python",
|
101 |
| - "args": [ |
102 |
| - "postgres_server.py" |
103 |
| - ], |
| 93 | + "args": ["postgres_server.py"], |
104 | 94 | "cwd": "C:\\path\\to\\postgres-mcp-server",
|
105 | 95 | "env": {
|
106 |
| - "DATABASE_URL": "postgresql://username:password@localhost:5432/database_name" |
| 96 | + "DATABASE_URL": "postgresql://username:password@localhost:5432/database" |
107 | 97 | }
|
108 | 98 | }
|
109 | 99 | }
|
110 | 100 | }
|
111 | 101 | ```
|
112 | 102 |
|
113 |
| -**macOS/Linux:** |
| 103 | +</details> |
| 104 | + |
| 105 | +<details> |
| 106 | +<summary>macOS/Linux</summary> |
| 107 | + |
114 | 108 | ```json
|
115 | 109 | {
|
116 | 110 | "mcpServers": {
|
117 | 111 | "postgres": {
|
118 | 112 | "command": "python",
|
119 |
| - "args": [ |
120 |
| - "postgres_server.py" |
121 |
| - ], |
| 113 | + "args": ["postgres_server.py"], |
122 | 114 | "cwd": "/absolute/path/to/postgres-mcp-server",
|
123 | 115 | "env": {
|
124 |
| - "DATABASE_URL": "postgresql://username:password@localhost:5432/database_name" |
| 116 | + "DATABASE_URL": "postgresql://username:password@localhost:5432/database" |
125 | 117 | }
|
126 | 118 | }
|
127 | 119 | }
|
128 | 120 | }
|
129 | 121 | ```
|
130 | 122 |
|
131 |
| -## Tool Categories |
132 |
| - |
133 |
| -### Database Structure & Metadata |
134 |
| -- `PostgreSQL_list_tables` - List all tables in a schema |
135 |
| -- `PostgreSQL_describe_table` - Get detailed table column information |
136 |
| -- `PostgreSQL_list_schemas` - List all database schemas |
137 |
| -- `PostgreSQL_list_indexes` - Show indexes for a table |
138 |
| -- `PostgreSQL_get_foreign_keys` - Display foreign key relationships |
139 |
| -- `PostgreSQL_get_table_constraints` - Show all table constraints |
140 |
| - |
141 |
| -### Query Execution & Analysis |
142 |
| -- `PostgreSQL_execute_select_query` - Execute SELECT queries safely |
143 |
| -- `PostgreSQL_execute_update_query` - Execute INSERT/UPDATE/DELETE queries |
144 |
| -- `PostgreSQL_explain_query` - Show query execution plans |
145 |
| -- `PostgreSQL_get_slow_queries` - Analyze slow running queries |
146 |
| -- `PostgreSQL_analyze_query_complexity` - Assess query performance characteristics |
147 |
| - |
148 |
| -### Performance Monitoring |
149 |
| -- `PostgreSQL_get_cache_hit_ratios` - Buffer cache performance metrics |
150 |
| -- `PostgreSQL_get_index_usage_stats` - Index utilization analysis |
151 |
| -- `PostgreSQL_get_table_statistics` - Comprehensive table statistics |
152 |
| -- `PostgreSQL_check_long_running_queries` - Monitor active long queries |
153 |
| -- `PostgreSQL_get_blocking_locks` - Identify blocking transactions |
154 |
| - |
155 |
| -### Maintenance & Optimization |
156 |
| -- `PostgreSQL_get_bloated_tables` - Find tables needing maintenance |
157 |
| -- `PostgreSQL_get_unused_indexes` - Identify unused indexes |
158 |
| -- `PostgreSQL_vacuum_analyze_table` - Perform table maintenance |
159 |
| -- `PostgreSQL_get_autovacuum_activity` - Monitor autovacuum operations |
160 |
| -- `PostgreSQL_analyze_database` - Update table statistics |
161 |
| - |
162 |
| -### Security & User Management |
163 |
| -- `PostgreSQL_create_user` - Create database users |
164 |
| -- `PostgreSQL_grant_privileges` - Assign user permissions |
165 |
| -- `PostgreSQL_get_active_connections` - Monitor user connections |
166 |
| -- `PostgreSQL_list_users_and_roles` - Display all users and roles |
167 |
| -- `PostgreSQL_get_table_permissions` - Show table-level permissions |
168 |
| - |
169 |
| -### Replication & Backup |
170 |
| -- `PostgreSQL_get_replication_status` - Monitor replication health |
171 |
| -- `PostgreSQL_get_replication_slots` - Manage replication slots |
172 |
| -- `PostgreSQL_get_backup_status` - Check backup configuration |
173 |
| -- `PostgreSQL_get_wal_stats` - Write-ahead log statistics |
174 |
| - |
175 |
| -## Example Interactions |
176 |
| - |
177 |
| -### Database Exploration |
178 |
| -``` |
179 |
| -"Show me all tables in the public schema" |
180 |
| -"What's the structure of the users table?" |
181 |
| -"List all foreign key relationships for the orders table" |
182 |
| -``` |
| 123 | +</details> |
183 | 124 |
|
184 |
| -### Performance Analysis |
185 |
| -``` |
186 |
| -"Analyze the performance of my database and suggest optimizations" |
187 |
| -"Show me the slowest queries from the last hour" |
188 |
| -"Which indexes are not being used and can be dropped?" |
189 |
| -"What's the buffer cache hit ratio?" |
190 |
| -``` |
| 125 | +--- |
191 | 126 |
|
192 |
| -### Monitoring & Diagnostics |
193 |
| -``` |
194 |
| -"Are there any blocking queries right now?" |
195 |
| -"Show me tables with high bloat that need maintenance" |
196 |
| -"Check the replication lag status" |
197 |
| -"What connections are currently active?" |
198 |
| -``` |
| 127 | +## 🧰 Tooling Highlights |
199 | 128 |
|
200 |
| -### Maintenance Operations |
201 |
| -``` |
202 |
| -"Run VACUUM ANALYZE on the products table" |
203 |
| -"Update statistics for the entire database" |
204 |
| -"Show me autovacuum activity for today" |
205 |
| -``` |
| 129 | +| Category | Tools | |
| 130 | +| --------------- | ---------------------------------------------------- | |
| 131 | +| 🧱 Structure | `list_tables`, `describe_table`, `get_foreign_keys` | |
| 132 | +| 📄 Queries | `execute_select_query`, `analyze_query_complexity` | |
| 133 | +| 📈 Monitoring | `get_cache_hit_ratios`, `check_long_running_queries` | |
| 134 | +| 🛠️ Maintenance | `get_bloated_tables`, `vacuum_analyze_table` | |
| 135 | +| 🔐 Security | `create_user`, `grant_privileges`, `list_users` | |
| 136 | +| 🔁 Replication | `get_replication_status`, `get_backup_status` | |
206 | 137 |
|
207 |
| -### Security Auditing |
208 |
| -``` |
209 |
| -"Show me all users and their privileges" |
210 |
| -"What permissions does user 'app_user' have?" |
211 |
| -"List all active database connections" |
212 |
| -``` |
| 138 | +--- |
| 139 | + |
| 140 | +## 💬 Prompt Examples for AI Agents |
| 141 | + |
| 142 | +### 👁️ Database Insight |
| 143 | + |
| 144 | +> "List all indexes in the public schema" |
| 145 | +> "Show detailed structure of the `orders` table" |
| 146 | +
|
| 147 | +### 🚀 Performance |
| 148 | + |
| 149 | +> "What are the slowest queries?" |
| 150 | +> "Analyze unused indexes" |
| 151 | +
|
| 152 | +### ⚙ Maintenance |
| 153 | + |
| 154 | +> "VACUUM ANALYZE the `products` table" |
| 155 | +> "Find bloated tables in the database" |
| 156 | +
|
| 157 | +### 🔒 Security |
| 158 | + |
| 159 | +> "List all users and their roles" |
| 160 | +> "Show active connections and privilege levels" |
| 161 | +
|
| 162 | +--- |
| 163 | + |
| 164 | +## 📚 Use Cases |
| 165 | + |
| 166 | +### ✅ Health & Diagnostics |
| 167 | + |
| 168 | +* Full PostgreSQL system health check |
| 169 | +* Query and index optimization |
| 170 | + |
| 171 | +### ⚙ Admin & Automation |
| 172 | + |
| 173 | +* AI-based schema exploration |
| 174 | +* SQL query building with NLP |
| 175 | + |
| 176 | +### 🔐 Security & Auditing |
| 177 | + |
| 178 | +* Role auditing, connection analysis |
| 179 | +* Secure query validation and permission check |
| 180 | + |
| 181 | +--- |
| 182 | + |
| 183 | +## 🗞️ License |
| 184 | + |
| 185 | +This project is licensed under the MIT License – see the [LICENSE](./LICENSE) file for details. |
| 186 | + |
| 187 | +--- |
| 188 | + |
| 189 | +## ⭐ Contribute & Support |
| 190 | + |
| 191 | +If you find this useful, please: |
| 192 | + |
| 193 | +* ⭐ Star the repo |
| 194 | +* 🍝 Fork it |
| 195 | +* 🛠️ Submit issues or PRs |
| 196 | + |
| 197 | +> 🧠 Let’s build smarter PostgreSQL automation – one prompt at a time. |
| 198 | +
|
| 199 | +--- |
| 200 | + |
| 201 | +## 🔍 GitHub Search Tags |
213 | 202 |
|
214 |
| -## Advanced Use Cases |
215 |
| - |
216 |
| -### Database Health Assessment |
217 |
| -The server can perform comprehensive health checks including: |
218 |
| -- Buffer pool efficiency analysis |
219 |
| -- Index usage optimization |
220 |
| -- Table bloat assessment |
221 |
| -- Replication lag monitoring |
222 |
| -- Connection pool analysis |
223 |
| - |
224 |
| -### Performance Tuning |
225 |
| -- Identify slow queries and suggest optimizations |
226 |
| -- Analyze execution plans for query improvement |
227 |
| -- Monitor resource utilization patterns |
228 |
| -- Recommend index strategies |
229 |
| - |
230 |
| -### Proactive Monitoring |
231 |
| -- Set up monitoring for long-running transactions |
232 |
| -- Track database growth trends |
233 |
| -- Monitor backup and recovery readiness |
234 |
| -- Analyze connection patterns and bottlenecks |
235 |
| - |
236 |
| -## Security Features |
237 |
| - |
238 |
| -- **Query Validation**: Only allows specific query types (SELECT for reads, INSERT/UPDATE/DELETE for writes) |
239 |
| -- **Connection Pooling**: Efficient database connection management with proper cleanup |
240 |
| -- **Parameter Sanitization**: Prevents SQL injection through parameterized queries |
241 |
| -- **Error Handling**: Comprehensive error handling with informative messages |
242 |
| -- **Logging**: Contextual logging for monitoring and debugging |
243 |
| -- **Environment Variables**: Secure credential management |
244 |
| -- **Permission Checks**: Respects database user permissions and access controls |
| 203 | +`#PostgreSQL` `#MCP` `#AI_Database_Tools` `#Claude` `#Query_Optimizer` `#Database_Health_Check` `#Python_PostgreSQL` `#DevOps_AI` `#Database_Monitoring` `#OpenSource` |
0 commit comments