postgres-mcp

postgres-mcp

Enables safe interaction with PostgreSQL databases through read-only queries, schema exploration, and performance analysis.

Category
Visit Server

README

postgres-mcp

!!! DO NOT USE IN PRODUCTION !!! !!! ONLY FOR LOCAL DEV & DB !!!

A PostgreSQL MCP (Model Context Protocol) server that enables Claude to safely interact with PostgreSQL databases via stdio/JSON-RPC 2.0.

Features

  • Execute queries — run parameterized SELECT statements with row limits
  • Explore structure — list tables/views and inspect columns, indexes, foreign keys
  • Analyze performance — generate EXPLAIN / EXPLAIN ANALYZE plans
  • Schema context — expose full DDL-like schema as an MCP resource
  • Security — read-only mode, schema whitelisting, SQL injection prevention

Requirements

  • Node.js ≥ 18 (ESM)
  • PostgreSQL database

Installation

npm install

Configuration

Copy .env.example to .env and fill in your values:

POSTGRES_URL=postgresql://user:password@localhost:5432/dbname
POSTGRES_SCHEMAS=public
POSTGRES_READONLY=true
POSTGRES_QUERY_TIMEOUT=30000
POSTGRES_MAX_ROWS=500
Variable Default Description
POSTGRES_URL (required) PostgreSQL connection string
POSTGRES_SCHEMAS public Comma-separated list of allowed schemas
POSTGRES_READONLY true Block any write statement
POSTGRES_QUERY_TIMEOUT 30000 Query timeout in milliseconds
POSTGRES_MAX_ROWS 500 Maximum rows returned per query

Usage

Integration with Claude Code

The server is designed to be run via npx directly from its local directory — no global install needed.

Add it to your Claude Code config (~/.claude/settings.json for global use, or .claude/settings.json at project level):

{
  "mcpServers": {
    "postgres": {
      "command": "npx",
      "args": ["/absolute/path/to/postgres_mcp"],
      "env": {
        "POSTGRES_URL": "postgresql://user:password@localhost:5432/dbname",
        "POSTGRES_SCHEMAS": "public",
        "POSTGRES_READONLY": "true"
      }
    }
  }
}

Replace /absolute/path/to/postgres_mcp with the actual path to this repository on your machine. All configuration is passed via env — no .env file required when using this approach.

Note: npx runs npm install automatically on first launch if node_modules is absent.

Testing with MCP Inspector

The MCP Inspector provides a web UI to test the server interactively without Claude Code.

POSTGRES_URL=postgresql://user:password@localhost:5432/dbname \
npx @modelcontextprotocol/inspector npx /absolute/path/to/postgres_mcp

Then open http://localhost:5173 in your browser. From there you can:

  • Call any tool with custom parameters and see the response
  • Read the postgres://schema resource
  • Inspect the raw JSON-RPC messages exchanged

Tools

query

Execute a SQL SELECT query.

{
  "sql": "SELECT * FROM users WHERE id = $1",
  "params": [42]
}

Returns: rows, rowCount, fields, truncated (if row limit was hit).

list_tables

List all accessible tables and views.

Returns an array of { schema, name, type } objects.

describe_table

Get the full structure of a table: columns, indexes, and foreign keys.

{
  "table": "users",
  "schema": "public"
}

Returns: columns (name, type, nullable, default, primary_key), indexes, foreign_keys.

explain_query

Generate an execution plan for a SQL query.

{
  "sql": "SELECT * FROM orders WHERE user_id = 1",
  "analyze": false
}

Set analyze: true to run EXPLAIN ANALYZE (actually executes the query).

Resource

postgres://schema

Auto-loaded resource exposing the full database schema in DDL-like format. Gives Claude upfront context about all accessible tables and their columns.

Security

  • Read-only mode (default): blocks INSERT, UPDATE, DELETE, DROP, CREATE, ALTER, TRUNCATE, GRANT, REVOKE, and other write operations via regex validation
  • Schema isolation: queries are restricted to schemas listed in POSTGRES_SCHEMAS
  • Parameterized queries: prevents SQL injection
  • Connection pooling: max 5 connections with idle/connection timeouts
  • Row and time limits: configurable caps prevent runaway queries

Tests

npm test

Tests use Vitest with mocked database calls — no live database required.

Project Structure

src/
├── index.js          # MCP server entry point
├── db.js             # Connection pool & configuration
├── security.js       # SQL and schema validation
├── tools/
│   ├── query.js
│   ├── list_tables.js
│   ├── describe_table.js
│   └── explain_query.js
└── resources/
    └── schema.js
tests/
├── security.test.js
├── query.test.js
├── list_tables.test.js
├── describe_table.test.js
├── explain_query.test.js
└── schema_resource.test.js

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