postgres-mcp-server
A Model Context Protocol server that provides secure database access to PostgreSQL through Kysely ORM, enabling natural language interaction with PostgreSQL databases. It offers 40+ MCP tools for querying, schema inspection, diagnostics, and Java backend monitoring.
README
PostgreSQL MCP Server
A secure Model Context Protocol (MCP) Server for PostgreSQL built with Kysely ORM. It enables AI assistants such as Claude Desktop, GitHub Copilot, Cline, and others to interact with PostgreSQL databases using natural language.
A secure Model Context Protocol (MCP) server for PostgreSQL built with Kysely ORM. It provides AI assistants with secure, read-only (by default) access to PostgreSQL databases using natural language.
Table of Contents
- Features
- Installation
- Configuration
- Available Tools
- Claude Desktop Configuration
- Development
- Environment Variables
- Migration Guide
- License
Features
Database Features
- ✅ PostgreSQL 12–18 support
- ✅ Multi-database support
- ✅ Lazy database initialization
- ✅ Connection pooling
- ✅ Query timeout configuration
- ✅ Read-only mode by default
- ✅ Parameterized queries (SQL Injection protection)
- ✅ Type-safe validation using Zod
MCP Capabilities
More than 40 MCP tools covering:
- SQL Query Execution
- Schema Inspection
- Table Explorer
- Index Explorer
- Query Explain Plan
- Database Diagnostics
- Performance Monitoring
- PostgreSQL DBA Utilities
- Java Backend Monitoring
PostgreSQL Advanced Monitoring
Supports PostgreSQL modern features including:
- Partitioning
- WAL Monitoring
- Replication
- Parallel Query
- JSONB Analysis
- Generated Columns
- Extended Statistics
- Autovacuum Advisor
- Backup Monitoring
- Huge Pages
- Extensions
- Foreign Key Analysis
Java Backend Support
Built specifically for Java backend engineers.
Supports monitoring for:
- Spring Boot
- Quarkus
- Hibernate
- Spring Data JPA
- HikariCP
- Flyway
- Liquibase
- JDBC Batch
- Transaction Monitoring
- Connection Leak Detection
Installation
Run directly with NPX:
npx @irsyadjpp/postgres-mcp-server
Configuration
The server supports three configuration methods.
Option 1 — Environment Variables (Recommended)
Default Database
DB_HOST=127.0.0.1
DB_PORT=5432
DB_USER=postgres
DB_PASSWORD=your_password
DB_NAME=postgres
DB_SSL=true
Additional Databases
DB_PROD_HOST=prod.example.com
DB_PROD_PORT=5432
DB_PROD_USER=app_user
DB_PROD_PASSWORD=secret
DB_PROD_DATABASE=production
DB_PROD_SSL=true
DB_STAGING_HOST=staging.example.com
DB_STAGING_PORT=5432
DB_STAGING_USER=app_user
DB_STAGING_PASSWORD=secret
DB_STAGING_DATABASE=staging
DB_STAGING_SSL=true
Option 2 — JSON Configuration
Set:
DB_CONFIG_PATH=/path/to/db-config.json
Example:
{
"databases": [
{
"name": "default",
"host": "127.0.0.1",
"port": 5432,
"user": "postgres",
"password": "password",
"database": "postgres",
"maxConnections": 5,
"ssl": true
},
{
"name": "production",
"host": "prod.example.com",
"port": 5432,
"user": "app_user",
"password": "secret",
"database": "production",
"maxConnections": 10,
"ssl": true
}
]
}
Option 3 — Dynamic Database Selection
Every MCP tool accepts an optional parameter:
{
"database_name": "production"
}
Example:
{
"sql": "SELECT * FROM users",
"database_name": "production"
}
Available Tools
Core Tools
| Tool | Description |
|---|---|
query |
Execute SQL queries with pagination |
describe_table |
Describe table structure |
list_objects |
List tables, views, or functions |
list_schemas |
List schemas |
list_indexes |
List indexes |
search_objects |
Search database objects |
explain_query |
Explain execution plan |
get_connections |
Show active connections |
diagnose_database |
Database health check |
get_slow_queries |
Analyze slow queries |
PostgreSQL Advanced Tools
| Tool | Description |
|---|---|
list_partitions |
Partition monitoring |
replication_status |
Replication monitoring |
progress_report |
VACUUM / CREATE INDEX progress |
wal_monitor |
WAL monitoring |
extended_stats |
Extended statistics |
index_dedup |
Index deduplication analysis |
generated_columns |
Generated column analysis |
jsonb_analysis |
JSONB recommendations |
parallel_query |
Parallel worker monitoring |
autovacuum_advisor |
Autovacuum recommendations |
huge_pages |
Huge pages monitoring |
statements_enhanced |
Enhanced pg_stat_statements |
foreign_key |
Foreign key analysis |
backup_monitor |
Backup monitoring |
extensions |
Installed extensions |
Java Backend Tools
| Tool | Description |
|---|---|
connection_pool |
HikariCP monitoring |
jpa_mapping |
Validate JPA mappings |
orm_performance |
ORM performance analysis |
transaction_monitor |
Transaction monitoring |
prepared_statement |
Prepared statement analysis |
migration_tracking |
Flyway/Liquibase monitoring |
orm_index_coverage |
ORM index recommendations |
jsonb_entity |
JSONB entity analysis |
batch_operation |
JDBC batch monitoring |
sequence_monitor |
Sequence monitoring |
timeseries_partition |
Partition recommendations |
connection_leak |
Connection leak detection |
deadlock_analysis |
Deadlock analysis |
jpa_schema_validation |
Validate JPA annotations |
orm_performance_baseline |
CRUD performance baseline |
Key Capabilities
- Multi-database support
- Automatic pagination
- SQL Injection protection
- PostgreSQL diagnostics
- Query performance analysis
- Database health monitoring
- DBA utilities
- Spring Boot optimization
- Quarkus optimization
- Hibernate optimization
Claude Desktop Configuration
Example:
{
"mcpServers": {
"postgres": {
"command": "npx",
"args": [
"@irsyadjpp/postgres-mcp-server@latest"
],
"env": {
"DB_HOST": "127.0.0.1",
"DB_PORT": "5432",
"DB_USER": "postgres",
"DB_PASSWORD": "password",
"DB_NAME": "postgres",
"DB_SSL": "true"
}
}
}
}
Other Supported AI Assistants
Configuration examples are included for:
- Claude Desktop
- GitHub Copilot Chat
- Cline
- Antigravity IDE
See CONFIG-GUIDES.md for detailed setup instructions.
Claude Configuration Locations
macOS
~/Library/Application Support/Claude/claude_desktop_config.json
Windows
%APPDATA%\Claude\claude_desktop_config.json
Development
Clone the repository:
git clone https://github.com/irsyadjpp/postgres-mcp-server.git
cd postgres-mcp-server
npm install
Run development server:
npm run dev
Build:
npm run build
Run all tests:
npm test
Run unit tests:
npm run test:unit
Run integration tests:
npm run test:integration
Environment Variables
| Variable | Default | Description |
|---|---|---|
DB_HOST |
127.0.0.1 |
PostgreSQL host |
DB_PORT |
5432 |
PostgreSQL port |
DB_USER |
postgres |
Database user |
DB_PASSWORD |
— | Database password |
DB_NAME |
postgres |
Database name |
DB_SSL |
true |
Enable SSL |
DB_CONFIG_PATH |
— | JSON configuration path |
READ_ONLY |
true |
Restrict to SELECT/EXPLAIN |
QUERY_TIMEOUT |
30000 |
Query timeout (ms) |
MAX_PAGE_SIZE |
500 |
Maximum page size |
DEFAULT_PAGE_SIZE |
100 |
Default page size |
Additional databases:
DB_<NAME>_HOST
DB_<NAME>_PORT
DB_<NAME>_USER
DB_<NAME>_PASSWORD
DB_<NAME>_DATABASE
DB_<NAME>_SSL
Migration Guide
Existing users require no configuration changes.
The server maintains full backward compatibility.
To add another database:
DB_PROD_HOST=prod.example.com
DB_PROD_PORT=5432
DB_PROD_USER=app_user
DB_PROD_PASSWORD=secret
DB_PROD_DATABASE=production
Use it in any MCP tool:
{
"database_name": "prod"
}
Alternatively, migrate to a JSON configuration by setting:
DB_CONFIG_PATH=/path/to/db-config.json
License
ISC
推荐服务器
Baidu Map
百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。
Playwright MCP Server
一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。
Magic Component Platform (MCP)
一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。
Audiense Insights MCP Server
通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。
VeyraX
一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。
graphlit-mcp-server
模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。
Kagi MCP Server
一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。
e2b-mcp-server
使用 MCP 通过 e2b 运行代码。
Neon MCP Server
用于与 Neon 管理 API 和数据库交互的 MCP 服务器
Exa MCP Server
模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。