PostgreSQL Read-Only MCP Server

PostgreSQL Read-Only MCP Server

A secure MCP server that enables querying PostgreSQL databases through an SSH tunnel with enforced read-only access, connection pooling, and comprehensive data exploration tools.

Category
Visit Server

README

PostgreSQL Read-Only MCP Server

A secure MCP (Model Context Protocol) server that provides read-only access to PostgreSQL databases through an SSH tunnel.

Features

  • Secure SSH Tunnel: Connects to PostgreSQL through encrypted SSH tunnel
  • Read-Only Enforcement: All queries run in read-only transactions
  • Connection Pooling: Efficient database connection management
  • Query Timeout: 15-second timeout for all operations
  • Comprehensive Tools: Query, list tables, describe schemas, analyze data

Configuration

  1. Copy the example configuration file:

    cp .env.example ~/.pg_mcp/.env
    
  2. Edit ~/.pg_mcp/.env with your credentials:

    # PostgreSQL Database Configuration
    POSTGRES_DB=your_database
    POSTGRES_USER=readonly_user
    POSTGRES_PASSWORD=your_password
    
    # SSH Tunnel Configuration
    SSH_HOST=your-ec2-instance.amazonaws.com
    SSH_USER=ec2-user
    SSH_KEY_PATH=/path/to/ssh/key
    
    # RDS Configuration
    RDS_HOST=your-database.rds.amazonaws.com
    
  3. Create a read-only database user:

    CREATE USER readonly_mcp WITH PASSWORD 'secure_password';
    GRANT CONNECT ON DATABASE your_database TO readonly_mcp;
    GRANT USAGE ON SCHEMA public TO readonly_mcp;
    GRANT SELECT ON ALL TABLES IN SCHEMA public TO readonly_mcp;
    ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT SELECT ON TABLES TO readonly_mcp;
    

Installation

pnpm install
pnpm run build

Usage

Start the MCP server:

./start_mcp.sh

For development mode with hot reload:

./start_mcp.sh dev

Available MCP Tools

  • postgres_query: Execute read-only SQL queries
  • postgres_list_tables: List all tables with optional row counts
  • postgres_describe_table: Get detailed schema information
  • postgres_analyze_table: Analyze table for data quality issues
  • postgres_find_related: Find foreign key relationships
  • postgres_explain_query: Get query execution plans

SSH Tunnel Management

The server implements a shared SSH tunnel system:

  • Multiple instances share a single tunnel
  • Reference counting prevents premature closure
  • Automatic reconnection on failure
  • Lock files in /tmp/pg_mcp_tunnel/

Monitor tunnel status:

./check_tunnel.sh

Security

  • All queries validated for read-only operations
  • Enforced read-only transactions
  • 15-second query timeout
  • SSH encryption for all connections
  • Credentials stored outside repository

Requirements

  • Node.js v24+
  • pnpm package manager
  • PostgreSQL database
  • SSH access to database server

Recommended Servers

playwright-mcp

playwright-mcp

A Model Context Protocol server that enables LLMs to interact with web pages through structured accessibility snapshots without requiring vision models or screenshots.

Official
Featured
TypeScript
Magic Component Platform (MCP)

Magic Component Platform (MCP)

An AI-powered tool that generates modern UI components from natural language descriptions, integrating with popular IDEs to streamline UI development workflow.

Official
Featured
Local
TypeScript
Audiense Insights MCP Server

Audiense Insights MCP Server

Enables interaction with Audiense Insights accounts via the Model Context Protocol, facilitating the extraction and analysis of marketing insights and audience data including demographics, behavior, and influencer engagement.

Official
Featured
Local
TypeScript
VeyraX MCP

VeyraX MCP

Single MCP tool to connect all your favorite tools: Gmail, Calendar and 40 more.

Official
Featured
Local
graphlit-mcp-server

graphlit-mcp-server

The Model Context Protocol (MCP) Server enables integration between MCP clients and the Graphlit service. Ingest anything from Slack to Gmail to podcast feeds, in addition to web crawling, into a Graphlit project - and then retrieve relevant contents from the MCP client.

Official
Featured
TypeScript
Kagi MCP Server

Kagi MCP Server

An MCP server that integrates Kagi search capabilities with Claude AI, enabling Claude to perform real-time web searches when answering questions that require up-to-date information.

Official
Featured
Python
E2B

E2B

Using MCP to run code via e2b.

Official
Featured
Neon Database

Neon Database

MCP server for interacting with Neon Management API and databases

Official
Featured
Exa Search

Exa Search

A Model Context Protocol (MCP) server lets AI assistants like Claude use the Exa AI Search API for web searches. This setup allows AI models to get real-time web information in a safe and controlled way.

Official
Featured
Qdrant Server

Qdrant Server

This repository is an example of how to create a MCP server for Qdrant, a vector search engine.

Official
Featured