JaviMaligno/postgres_mcp

๐Ÿ—„๏ธ Databases
0 Views
0 Installs

๐Ÿ ๐Ÿ  - PostgreSQL MCP server with 14 tools for querying, schema exploration, and table analysis. Features security-first design with SQL injection prevention and read-only by default.

Quick Install

One-Click IDE Configuration
claude_desktop_config.json
{
  "mcpServers": {
    "javimaligno-postgres-mcp": {
      "command": "npx",
      "args": [
        "-y",
        "javimaligno-postgres-mcp"
      ]
    }
  }
}
Or

Using an AI coding agent (Claude Code, Cursor, etc.)? Copy a ready-made prompt that tells it to fetch the setup instructions and install this server for you.

Documentation Overview

PostgreSQL MCP Server

CI PyPI version npm version License: MIT

MCP server for PostgreSQL database operations. Works with Claude Code, Claude Desktop, Cursor, and any MCP-compatible client.

Language Versions

This repository contains both TypeScript and Python implementations:

VersionDirectoryStatusInstallation
TypeScript/typescriptโœ… Recommended (Smithery)npm install -g postgresql-mcp
Python/pythonโœ… Stablepipx install postgresql-mcp

Note: The TypeScript version is used for Smithery deployments. Both versions provide identical functionality.

Features

  • Query Execution: Execute SQL queries with read-only protection by default
  • Schema Exploration: List schemas, tables, views, and functions
  • Table Analysis: Describe structure, indexes, constraints, and statistics
  • Performance Tools: EXPLAIN queries and analyze table health
  • Security First: SQL injection prevention, credential protection, read-only by default
  • MCP Prompts: Guided workflows for exploration, query building, and documentation
  • MCP Resources: Browsable database structure as markdown

Quick Start

TypeScript (Recommended for Smithery)

# Install globally
npm install -g postgresql-mcp

# Or run directly with npx
npx postgresql-mcp

Python

# Install
pipx install postgresql-mcp

# Configure Claude Code
claude mcp add postgres -s user \
  -e POSTGRES_HOST=localhost \
  -e POSTGRES_USER=your_user \
  -e POSTGRES_PASSWORD=your_password \
  -e POSTGRES_DB=your_database \
  -- postgresql-mcp

Full Installation Guide - Includes database permissions setup, remote connections, and troubleshooting.

Configuration

Environment Variables

VariableRequiredDefaultDescription
POSTGRES_HOSTlocalhostDatabase host
POSTGRES_PORT5432Database port
POSTGRES_USERโœ…Database user
POSTGRES_PASSWORDโœ…Database password
POSTGRES_DBโœ…Database name
POSTGRES_SSLMODEpreferSSL mode
ALLOW_WRITE_OPERATIONSfalseEnable INSERT/UPDATE/DELETE
QUERY_TIMEOUT30Query timeout (seconds)
MAX_ROWS1000Maximum rows returned

Claude Code CLI

# TypeScript version
claude mcp add postgres -s user \
  -e POSTGRES_HOST=localhost \
  -e POSTGRES_USER=your_user \
  -e POSTGRES_PASSWORD=your_password \
  -e POSTGRES_DB=your_database \
  -- npx postgresql-mcp

# Python version
claude mcp add postgres -s user \
  -e POSTGRES_HOST=localhost \
  -e POSTGRES_USER=your_user \
  -e POSTGRES_PASSWORD=your_password \
  -e POSTGRES_DB=your_database \
  -- postgresql-mcp

Cursor IDE

Add to ~/.cursor/mcp.json:

{
  "mcpServers": {
    "postgres": {
      "command": "npx",
      "args": ["postgresql-mcp"],
      "env": {
        "POSTGRES_HOST": "localhost",
        "POSTGRES_PORT": "5432",
        "POSTGRES_USER": "your_user",
        "POSTGRES_PASSWORD": "your_password",
        "POSTGRES_DB": "your_database"
      }
    }
  }
}

Available Tools (14 total)

Query Execution

ToolDescription
queryExecute read-only SQL queries against the database
executeExecute write operations (INSERT/UPDATE/DELETE) when enabled
explain_queryGet EXPLAIN plan for query optimization

Schema Exploration

ToolDescription
list_schemasList all schemas in the database
list_tablesList tables in a specific schema
describe_tableGet table structure (columns, types, constraints)
list_viewsList views in a schema
describe_viewGet view definition and columns
list_functionsList functions and procedures

Performance & Analysis

ToolDescription
table_statsGet table statistics (row count, size, bloat)
list_indexesList indexes for a table
list_constraintsList constraints (PK, FK, UNIQUE, CHECK)

Database Info

ToolDescription
get_database_infoGet database version and connection info
search_columnsSearch for columns by name across all tables

MCP Prompts

Guided workflows that help Claude assist you effectively:

PromptDescription
explore_databaseComprehensive database exploration and overview
query_builderHelp building efficient queries for a table
performance_analysisAnalyze table performance and suggest optimizations
data_dictionaryGenerate documentation for a schema

MCP Resources

Browsable database structure:

Resource URIDescription
postgres://schemasList all schemas
postgres://schemas/{schema}/tablesTables in a schema
postgres://schemas/{schema}/tables/{table}Table details
postgres://databaseDatabase connection info

Example Usage

Once configured, ask Claude to:

Schema Exploration:

  • "List all tables in the public schema"
  • "Describe the users table structure"
  • "What views are available?"

Querying:

  • "Show me 10 rows from the orders table"
  • "Find all customers who placed orders last week"
  • "Count records grouped by status"

Performance Analysis:

  • "What indexes exist on the orders table?"
  • "Analyze the performance of the users table"
  • "Explain this query: SELECT * FROM orders WHERE created_at > '2024-01-01'"

Documentation:

  • "Generate a data dictionary for this database"
  • "What columns contain 'email' in their name?"

Security

This MCP server implements multiple security layers:

Read-Only by Default

Write operations (INSERT, UPDATE, DELETE) are blocked unless explicitly enabled via ALLOW_WRITE_OPERATIONS=true.

SQL Injection Prevention

  • All queries are validated before execution
  • Dangerous operations (DROP DATABASE, etc.) are always blocked
  • Multiple statements are not allowed
  • SQL comments are blocked

Credential Protection

  • Passwords stored using secure string types
  • Credentials never appear in logs or error messages

Query Limits

  • Results limited by MAX_ROWS (default: 1000)
  • Query timeout configurable via QUERY_TIMEOUT

Development

TypeScript

cd typescript
npm install
npm run build
npm run dev  # Watch mode

Python

cd python
uv sync
uv run pytest -v --cov=postgres_mcp

Running Tests

# Python unit tests (no database required)
cd python
uv run pytest tests/test_security.py tests/test_settings.py -v

# Integration tests (requires PostgreSQL)
docker-compose up -d
uv run pytest tests/test_integration.py -v

Troubleshooting

Connection Issues

# Verify PostgreSQL is running
pg_isready -h localhost -p 5432

# Test connection with psql
psql -h localhost -U your_user -d your_database

Permission Denied

Ensure your database user has SELECT permissions:

GRANT SELECT ON ALL TABLES IN SCHEMA public TO your_user;

MCP Server Not Connecting

# Check server status
claude mcp get postgres

# Test server directly
postgresql-mcp  # Should wait for MCP messages

Links

Author

Built by Javier Aguilar - AI Agent Architect specializing in multi-agent orchestration and MCP development.

License

MIT

Related MCP Servers

modelcontextprotocol/server-postgresVerified

๐Ÿ“‡ ๐Ÿ  - PostgreSQL database integration with schema inspection and query capabilities

๐Ÿ—„๏ธ Databases1 views
Aiven-Open/mcp-aiven

๐Ÿ โ˜๏ธ ๐ŸŽ–๏ธ - Navigate your Aiven projects and interact with the PostgreSQLยฎ, Apache Kafkaยฎ, ClickHouseยฎ and OpenSearchยฎ services

๐Ÿ—„๏ธ Databases0 views
alexanderzuev/supabase-mcp-server

Supabase MCP Server with support for SQL query execution and database exploration tools

๐Ÿ—„๏ธ Databases0 views
aliyun/alibabacloud-tablestore-mcp-server

โ˜• ๐Ÿ โ˜๏ธ - MCP service for Tablestore, features include adding documents, semantic search for documents based on vectors and scalars, RAG-friendly, and serverless.

๐Ÿ—„๏ธ Databases0 views

Engagement

Views
0
Installs
0
Upvotes
0

Views and upvotes are unique per visitor network (hashed IP). Installs count copy actions.

Status

Health: Not checked yet

We have not completed a health check for this listing yet.

No check timestamp yet.

Unclaimed listing (imported or pending owner verification). Claim it โ†’
โ˜… Spotlight Slot

Feature Your MCP Server

Get maximum visibility for your server across our directory, search results, and detail pages.

Spotlight Your Server

Own this project?

This directory is pre-filled from public sources. Claim via GitHub README, site badge, or DNS TXT to get the verified badge and attach your website.

Claim this listing

Promote this listing

Optional paid placement. Free listings stay free forever.

Share & Embed

Add our SVG badge (dark/light directory styles) or embeddable widget to your site.