MCP Postgres Query Server

MCP Postgres Query Server

A secure Model Context Protocol server that allows Claude to execute read-only SQL queries against a PostgreSQL database, enabling natural language interaction with database data.

RathodDarshil

Developer Tools
Visit Server

README

MCP Postgres Query Server

A Model Context Protocol (MCP) server implementation for querying a PostgreSQL database in read-only mode, designed to work with Claude Desktop and other MCP clients.

Overview

This project implements a Model Context Protocol (MCP) server that provides:

  1. A secure, read-only interface to a PostgreSQL database
  2. Integration with Claude Desktop through the MCP protocol
  3. SQL query validation to ensure only SELECT queries are executed
  4. Query timeout protection (10 seconds)

Prerequisites

  • Node.js (v14 or later)
  • npm (comes with Node.js)
  • PostgreSQL database (connection details provided via command line)

Installation

# Clone the repository
git clone https://github.com/RathodDarshil/mcp-postgres-query-server.git
cd mcp-postgres-query-server

# Install dependencies
npm install

# Build the project
npm run build

Connecting to Claude Desktop

You can configure Claude Desktop to automatically launch and connect to the MCP server:

  1. Access the Claude Desktop configuration file:

    • Open Claude Desktop
    • Go to Settings > Developer > Edit Config
    • This will open the configuration file in your default text editor
  2. Add the postgres-query-server to the mcpServers section of your claude_desktop_config.json:

{
    "mcpServers": {
        "postgres-query": {
            "command": "node",
            "args": [
                "/path/to/your/mcp-postgres-query-server/dist/index.js",
                "postgresql://username:password@hostname:port/database"
            ]
        }
    }
}
  1. Replace /path/to/your/ with the actual path to your project directory.
  2. Replace the PostgreSQL connection string with your actual database credentials.
  3. Save the file and restart Claude Desktop. The MCP server should now appear in the MCP server selection dropdown in Settings.

Example Configuration

Here's a complete example of a configuration file with postgres-query:

{
    "mcpServers": {
        "postgres-query": {
            "command": "node",
            "args": [
                "/Users/darshilrathod/mcp-servers/mcp-postgres-query-server/dist/index.js",
                "postgresql://user:password@localhost:5432/mydatabase"
            ]
        }
    }
}

Updating Configuration

To update your Claude Desktop configuration:

  1. Open Claude Desktop
  2. Go to Settings > Developer > Edit Config
  3. Make your changes to the configuration file
  4. Save the file
  5. Restart Claude Desktop for the changes to take effect
  6. If you've updated the MCP server code, make sure to rebuild it with npm run build before restarting

Features

  • Read-Only Database Access: Only SELECT queries are permitted for security
  • Query Validation: Prevents potentially harmful SQL operations
  • Timeout Protection: Queries running longer than 10 seconds are automatically terminated
  • MCP Protocol Support: Complete implementation of the Model Context Protocol
  • JSON Response Formatting: Query results are returned in structured JSON format

API

Tools

query-postgres

Executes a read-only SQL query against the configured PostgreSQL database.

Parameters:

  • query (string): A SQL SELECT query to execute

Response:

  • JSON object containing:
    • rows: The result set rows
    • rowCount: Number of rows returned
    • fields: Column metadata

Example:

query-postgres: SELECT * FROM users LIMIT 5

Development

The main server implementation is in src/index.ts. Key components:

  • PostgreSQL connection pool setup
  • Query validation logic
  • MCP server configuration
  • Tool and resource definitions

To modify the server's behavior, you can:

  • Edit the query validation logic in isReadOnlyQuery()
  • Add additional tools or resources to the MCP server
  • Modify the query timeout duration (currently 10 seconds)

Security Considerations

  • The server validates all queries to ensure they are read-only
  • Connection to the database uses SSL
  • Query timeout prevents resource exhaustion
  • No write operations are permitted
  • Database credentials are passed directly via command line arguments, not stored in files

License

ISC

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

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
MCP Package Docs Server

MCP Package Docs Server

Facilitates LLMs to efficiently access and fetch structured documentation for packages in Go, Python, and NPM, enhancing software development with multi-language support and performance optimization.

Featured
Local
TypeScript
Claude Code MCP

Claude Code MCP

An implementation of Claude Code as a Model Context Protocol server that enables using Claude's software engineering capabilities (code generation, editing, reviewing, and file operations) through the standardized MCP interface.

Featured
Local
JavaScript
@kazuph/mcp-taskmanager

@kazuph/mcp-taskmanager

Model Context Protocol server for Task Management. This allows Claude Desktop (or any MCP client) to manage and execute tasks in a queue-based system.

Featured
Local
JavaScript
Linear MCP Server

Linear MCP Server

Enables interaction with Linear's API for managing issues, teams, and projects programmatically through the Model Context Protocol.

Featured
JavaScript
mermaid-mcp-server

mermaid-mcp-server

A Model Context Protocol (MCP) server that converts Mermaid diagrams to PNG images.

Featured
JavaScript
Jira-Context-MCP

Jira-Context-MCP

MCP server to provide Jira Tickets information to AI coding agents like Cursor

Featured
TypeScript
Linear MCP Server

Linear MCP Server

A Model Context Protocol server that integrates with Linear's issue tracking system, allowing LLMs to create, update, search, and comment on Linear issues through natural language interactions.

Featured
JavaScript
Sequential Thinking MCP Server

Sequential Thinking MCP Server

This server facilitates structured problem-solving by breaking down complex issues into sequential steps, supporting revisions, and enabling multiple solution paths through full MCP integration.

Featured
Python