PostgreSQL MCP Server

Python MCP server for PostgreSQL database inspection and querying

Local serverstdioPython

What is the PostgreSQL MCP server?

Most database access work still happens through a UI a human drives. PostgreSQL MCP server moves it into the conversation instead. Python MCP server for PostgreSQL database inspection and querying.

The short version

A Python Model Context Protocol (MCP) server for inspecting and querying PostgreSQL databases from MCP-compatible clients. It provides schema discovery, safe read-only query execution, query explanation, table previews, index analysis, relationship inspection, and PostgreSQL resources for table metadata.

  • List public tables and inspect table schemas
  • Execute read-only SQL in a PostgreSQL read-only transaction
  • Explain query plans without executing the target query directly
  • Preview table rows with a fixed limit
  • Inspect foreign-key relationships and indexes
  • Expose passive MCP resources for table lists and schema details

The tools it exposes

The server publishes 10 tools. What each one is for:

  • postgresql_list_tables — List public base tables
  • postgresql_describe_table — Show columns and metadata for a table
  • postgresql_execute_read_query — Run bounded SQL under read-only transaction mode
  • postgresql_execute_write_query — Run a single approved modifying SQL statement and commit
  • postgresql_explain_query — Return PostgreSQL EXPLAIN output for a single query
  • postgresql_get_database_summary — Return database version and table count
  • postgresql_get_relationships — Inspect foreign-key relationships
  • postgresql_analyze_indexes — Inspect indexes and sizes
  • postgresql_preview_table — Return up to 10 rows from a table
  • postgresql_search_sql_definitions — Search public SQL routines/functions

Getting it running

Installation goes through your MCP client rather than a global install: point it at mdev-postgresql-mcp-server on PyPI and it is fetched when the client starts. The copy-paste blocks for Claude Desktop, Claude Code and Cursor are further down this page.

What it needs from you

Configuration is passed through the environment: POSTGRES_HOST, POSTGRES_PORT, POSTGRES_USER, POSTGRES_PASSWORD, POSTGRES_DB. Treat anything key-shaped as a real credential — scope it to the minimum the server needs, and rotate it if it ever lands in a shared config.

  • Python 3.11+ - PostgreSQL database - MCP-compatible client such as Claude Desktop, Cursor, VS Code, or another MCP host

How it compares

Among the database access options, the useful question is rarely "what can it do" but "what does it cost you to run" — permissions, credentials, and how much of your context its toolset consumes. PostgreSQL's toolset — postgresql_list_tables, postgresql_describe_table, postgresql_execute_read_query and 7 more — is a fair guide to whether it matches your workflow. It is maintained by musaddiq-dev; worth a glance at recent repository activity before you build anything load-bearing on it.

This entry was verified against PostgreSQL's own documentation before publication; SyncDev keeps the directory reviewed rather than auto-generated.

Things to watch

  • It runs with your machine's permissions. That is convenient and also the reason to think about what you point it at before you approve a tool call.
  • With 10 tools registered it takes up a noticeable share of the context window; turn it off in projects that never touch PostgreSQL.
  • Keep per-call confirmation enabled while you learn its behaviour; it is the cheapest safeguard you have.

Available tools

ToolWhat it does
postgresql_list_tablesList public base tables
postgresql_describe_tableShow columns and metadata for a table
postgresql_execute_read_queryRun bounded SQL under read-only transaction mode
postgresql_execute_write_queryRun a single approved modifying SQL statement and commit
postgresql_explain_queryReturn PostgreSQL EXPLAIN output for a single query
postgresql_get_database_summaryReturn database version and table count
postgresql_get_relationshipsInspect foreign-key relationships
postgresql_analyze_indexesInspect indexes and sizes
postgresql_preview_tableReturn up to 10 rows from a table
postgresql_search_sql_definitionsSearch public SQL routines/functions

How to install the PostgreSQL MCP server

{
  "mcpServers": {
    "postgresql-server": {
      "command": "uvx",
      "args": ["mdev-postgresql-mcp-server"],
      "env": {
        "POSTGRES_HOST": "your-value",
        "POSTGRES_PORT": "your-value",
        "POSTGRES_USER": "your-value",
        "POSTGRES_PASSWORD": "your-value",
        "POSTGRES_DB": "your-value"
      }
    }
  }
}

Add to claude_desktop_config.json, then restart Claude Desktop.

Configuration

  • Python 3.11+ - PostgreSQL database - MCP-compatible client such as Claude Desktop, Cursor, VS Code, or another MCP host
VariableDescriptionRequired
POSTGRES_HOSTEndpoint or connection string the server talks to.Optional
POSTGRES_PORTConfiguration value read at startup.Optional
POSTGRES_USERConfiguration value read at startup.Optional
POSTGRES_PASSWORDConfiguration value read at startup.Optional
POSTGRES_DBConfiguration value read at startup.Optional

Example prompts to try

  • Use PostgreSQL to postgresql list tables.
  • Use PostgreSQL to postgresql describe table.
  • Use PostgreSQL to postgresql execute read query.

Frequently asked questions

It connects PostgreSQL to MCP-compatible AI assistants such as Claude and Cursor, exposing 10 tools (postgresql_list_tables, postgresql_describe_table, postgresql_execute_read_query, and more) that the assistant can call on your behalf. Instead of copying data back and forth by hand, the assistant works with PostgreSQL directly.