momodemo333/php-mcp-postgresql
Composer 安装命令:
composer require momodemo333/php-mcp-postgresql
包简介
PostgreSQL MCP Server for Claude Code - Secure and configurable PostgreSQL integration via Model Context Protocol
README 文档
README
PostgreSQL MCP Server for Claude Code - Secure and configurable PostgreSQL integration via Model Context Protocol.
🎉 Version 1.0.0-beta: Core functionality complete! Production testing in progress.
🚀 Features
- 🔒 Secure by Default - Read-only operations by default, write operations require explicit permission
- 🎯 PostgreSQL Native - Full support for JSONB, arrays, CTEs, window functions, and more
- ⚡ High Performance - Connection pooling, prepared statements, query timeouts
- 📊 Schema Introspection - Explore databases, tables, columns, indexes
- 🔧 Flexible Configuration - Environment variables, .env files, multi-database support
- 🛡️ Built-in Protection - SQL injection prevention, dangerous keyword blocking, result limits
📦 Requirements
- PHP 8.1 or higher
- PostgreSQL 12 or higher
- Composer
- PHP extensions:
ext-pdo,ext-pdo_pgsql
🔧 Installation
1. Clone the Repository
git clone https://github.com/momodemo333/php-mcp-postgresql.git
cd php-mcp-postgresql
2. Install Dependencies
composer install
3. Configure Environment
cp .env.example .env
# Edit .env with your PostgreSQL credentials
⚙️ Configuration
Environment Variables
| Variable | Description | Default |
|---|---|---|
PGSQL_HOST |
PostgreSQL server host | localhost |
PGSQL_PORT |
PostgreSQL server port | 5432 |
PGSQL_USER |
Database username | postgres |
PGSQL_PASS |
Database password | (empty) |
PGSQL_DB |
Database name (optional for multi-db) | (empty) |
ALLOW_INSERT_OPERATION |
Enable INSERT queries | false |
ALLOW_UPDATE_OPERATION |
Enable UPDATE queries | false |
ALLOW_DELETE_OPERATION |
Enable DELETE queries | false |
QUERY_TIMEOUT |
Query timeout in seconds | 30 |
MAX_RESULTS |
Maximum rows returned | 1000 |
CONNECTION_POOL_SIZE |
Max concurrent connections | 5 |
LOG_LEVEL |
Logging level (DEBUG/INFO/WARN/ERROR) | INFO |
Claude Code Configuration
Add to your Claude Code settings (claude-code-settings.json):
{
"mcpServers": {
"postgresql": {
"command": "php",
"args": ["/absolute/path/to/php-mcp-postgresql/bin/server.php"],
"type": "stdio",
"env": {
"PGSQL_HOST": "localhost",
"PGSQL_PORT": "5432",
"PGSQL_USER": "your_user",
"PGSQL_PASS": "your_password",
"PGSQL_DB": "your_database"
}
}
}
}
See examples/ for more configuration examples.
🛠️ Available Tools
Database Management
pgsql_list_databases- List all available databasespgsql_list_tables- List tables in a databasepgsql_describe_table- Get detailed table structurepgsql_server_status- Get server status and statistics
Query Execution
pgsql_select- Execute SELECT queries safelypgsql_insert- Insert data (requires permission)pgsql_update- Update data (requires permission)pgsql_delete- Delete data (requires permission)pgsql_execute_query- Execute custom queries (with validation)
🧪 Testing
Quick Test
# Start test PostgreSQL database ./tests/start-test-db.sh # Run all tests ./tests/run-tests.sh # Stop test database ./tests/stop-test-db.sh
Docker Test Environment
The project includes a complete Docker test environment:
# Start PostgreSQL container with test data docker-compose -f docker-compose.test.yml up -d # Run tests php tests/test_connection.php php tests/test_mcp_server.php # Stop containers docker-compose -f docker-compose.test.yml down
🔒 Security
Default Security Features
- Read-only by default - All write operations disabled unless explicitly enabled
- Prepared statements - Prevents SQL injection attacks
- Query validation - Blocks dangerous keywords and operations
- Result limits - Prevents memory exhaustion from large result sets
- Connection timeouts - Prevents hanging queries
- Schema restrictions - Limit access to specific schemas
Best Practices
- Never enable write operations in production unless absolutely necessary
- Use read-only database users when possible
- Set appropriate
MAX_RESULTSandQUERY_TIMEOUTvalues - Review logs regularly for suspicious activity
- Keep the server updated with security patches
🎆 PostgreSQL-Specific Features
JSONB Support
-- Query JSONB fields SELECT * FROM users WHERE metadata->>'role' = 'admin'; SELECT * FROM products WHERE specifications @> '{"cpu": "Intel i7"}';
Array Support
-- Query array fields SELECT * FROM products WHERE 'electronics' = ANY(tags); SELECT * FROM products WHERE tags && ARRAY['computers', 'portable'];
Advanced Features
- Common Table Expressions (CTEs)
- Window functions
- RETURNING clause on INSERT/UPDATE/DELETE
- Materialized views
- Full-text search
- Custom types and domains
📝 Documentation
- Installation Guide
- Configuration Reference
- MCP Tools Documentation
- Troubleshooting
- Migration Plan
- Implementation Status
🤝 Contributing
Contributions are welcome! Please:
- Fork the repository
- Create a feature branch
- Write tests for new functionality
- Ensure all tests pass
- Submit a pull request
📄 License
MIT License - see LICENSE file for details.
🙏 Acknowledgments
- Built with php-mcp/server framework
- Based on php-mcp-mysql architecture
- Thanks to the MCP community for the protocol specification
💬 Support
- Issues: GitHub Issues
- Discussions: GitHub Discussions
Version: 1.0.0-beta
Status: Production Testing
Last Updated: January 2025
momodemo333/php-mcp-postgresql 适用场景与选型建议
momodemo333/php-mcp-postgresql 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1 次下载、GitHub Stars 达 2, 最近一次更新时间为 2025 年 08 月 12 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「database」 「postgresql」 「pgsql」 「postgres」 「ai」 「mcp」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 momodemo333/php-mcp-postgresql 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 momodemo333/php-mcp-postgresql 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 momodemo333/php-mcp-postgresql 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
SilverStripe now has tentative support for PostgreSQL ('Postgres')
Dibi is Database Abstraction Library for PHP
Async Reactive Postgres Driver for PHP (Non-blocking)
Store your language lines in the database, yaml or other sources
Powerful PHP database abstraction layer (DBAL) with many features for database schema introspection and management.
A package for automatically encrypting and decrypting Eloquent attributes in Laravel 5.5+, based on configuration settings.
统计信息
- 总下载量: 1
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 27
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-08-12