MCP-SERVER-POSTGRES
Verified Safeby AnhQuanengineer
Overview
Provides an MCP server for PostgreSQL database interaction (schema listing, read-only querying) and AI-powered sentiment analysis using either external LLMs or local Hugging Face models.
Installation
python postgres.pyEnvironment Variables
- POSTGRES_HOST
- POSTGRES_PORT
- POSTGRES_DB
- POSTGRES_USER
- POSTGRES_PASSWORD
- LLM_PROVIDER
- OPENROUTER_API_KEY
- OPENROUTER_MODEL
- GOOGLE_API_KEY
- GOOGLE_MODEL
- HF_MODEL_NAME
- PRELOAD_MODEL
Security Notes
The `postgres.py` component implements robust SQL query validation (`validate_sql_query`) to restrict operations to `SELECT` statements only, prevent multiple statements, and filter dangerous patterns (e.g., SQL comments, file operations, stored procedures). This significantly mitigates SQL injection risks for its defined read-only functions. API keys and database credentials are managed via environment variables. The `sentiment.py` component includes a rate limiter for LLM API calls, preventing abuse, and attempts to parse LLM responses as JSON with a fallback for malformed output. No direct use of `eval` or `exec` is present. A potential future risk exists if the `update_sentiment` prompt (currently unimplemented in `postgres.py`'s `handle_call_tool`) were to be implemented without similar stringent input validation and parameterization.
Similar Servers
MCP-PostgreSQL-Ops
The MCP server provides professional operations, monitoring, and management capabilities for PostgreSQL databases using natural language queries.
enhanced-postgres-mcp-server
This server acts as a Model Context Protocol interface for PostgreSQL, enabling LLMs to query data, modify records, and manage database schema objects with read and write capabilities.
SchemaCrawler-AI
Provides an AI-powered interface for natural language database schema exploration, analysis, visualization, and SQL assistance.
mcp-postgres
Provides a read-only Model Context Protocol (MCP) server for AI tools (like Claude Code, Cursor) to query a PostgreSQL database directly.