PostgreSQL integration for Claude Desktop/Code via Model Context Protocol (MCP).
The PostgreSQL MCP server is fully configured and operational. It provides safe, read-only database access to Claude for querying and analyzing your PostgreSQL databases.
./test-mcp-final.sh
Claude Desktop/Code
↓
MCP Protocol (stdio)
↓
run-postgres-mcp.sh
↓
Docker Container (crystaldba/postgres-mcp)
↓
postgres-net network
↓
PostgreSQL Database
- Read-only access (restricted mode) - Safe for production use
- Schema exploration - Discover tables, columns, and relationships
- SQL query execution - Run SELECT queries and analyze results
- Network isolation - Runs in Docker network for security
- Automatic authentication - Credentials loaded from environment
Location: ~/.config/claude/mcp-settings.json
{
"mcpServers": {
"postgres": {
"command": "/home/administrator/projects/mcp-postgres/run-postgres-mcp.sh",
"args": [],
"env": {
"MCP_ACCESS_MODE": "restricted"
}
}
}
}
Location: /home/administrator/projects/secrets/postgres.env
POSTGRES_USER=admin
POSTGRES_PASSWORD=<password>
POSTGRES_DB=defaultdb
POSTGRES_PORT=5432
run-postgres-mcp.sh
- Main script that launches the MCP servertest-mcp-final.sh
- Verification script to test the setupdebug-mcp.sh
- Comprehensive debugging script with logginglogs/
- Debug logs directory
./test-mcp-final.sh
./debug-mcp.sh
# Check logs in logs/debug-*.log
# Test database connection
source /home/administrator/projects/secrets/postgres.env
PGPASSWORD="${POSTGRES_PASSWORD}" psql -h localhost -p 5432 -U "${POSTGRES_USER}" -d "${POSTGRES_DB}" -c "SELECT version();"
# Test MCP server
echo '{"jsonrpc":"2.0","method":"initialize","params":{"protocolVersion":"1.0.0","capabilities":{"tools":{}},"clientInfo":{"name":"test","version":"1.0.0"}},"id":1}' | timeout 2 ./run-postgres-mcp.sh 2>/dev/null | head -1
-
Timeouts during testing
- Normal behavior - MCP server uses stdio and stays running
- The server is working correctly if you see "Successfully connected to database"
-
Connection refused
- Check PostgreSQL is running:
docker ps | grep postgres
- Verify network:
docker network ls | grep postgres-net
- Test connectivity:
docker exec postgres pg_isready
- Check PostgreSQL is running:
-
Authentication failed
- Verify credentials:
cat /home/administrator/projects/secrets/postgres.env
- Test direct connection with the credentials
- Verify credentials:
-
MCP not available in Claude
- Restart Claude Desktop/Code after configuration
- Check config:
jq '.mcpServers.postgres' ~/.config/claude/mcp-settings.json
# Check PostgreSQL logs
docker logs postgres --tail 50
# Check network
docker network inspect postgres-net
# Test from within network
docker run --rm --network postgres-net postgres:15 pg_isready -h postgres
# Clean up stale containers
docker ps -a | grep mcp-postgres | awk '{print $1}' | xargs docker rm -f
- Restricted Mode: Only SELECT queries allowed
- Network Isolation: Runs in Docker network
- No External Access: Only accessible from Docker network
- Credential Protection: Secrets stored separately from code
When Claude Desktop/Code starts with this MCP server configured:
- Claude can query your PostgreSQL databases
- Explore schema and table structures
- Analyze data patterns
- Generate SQL queries
- Help with database design and optimization
Example prompts:
- "Show me all tables in the database"
- "What columns does the users table have?"
- "Find all orders from the last week"
- "Analyze the performance of this query"
docker pull crystaldba/postgres-mcp:latest
ls -la logs/
tail -f logs/debug-*.log
docker restart postgres
Last tested: 2025-08-27 Status: Working correctly with restricted mode access