momodemo333 / php-mcp-postgresql
PostgreSQL MCP Server for Claude Code - Secure and configurable PostgreSQL integration via Model Context Protocol
1.0.1
2025-08-12 15:42 UTC
Requires
- php: >=8.1
- ext-pdo: *
- ext-pdo_pgsql: *
- php-mcp/server: ^3.3
This package is auto-updated.
Last update: 2025-08-12 18:08:25 UTC
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_RESULTS
andQUERY_TIMEOUT
values - 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