All How-To Guides

3 Ways to Connect to PostgreSQL with Claude Code

How to connect to PostgreSQL with Claude Code?

3 MCP servers featured

Last reviewed June 2026

PostgreSQL is the most popular open-source database, and connecting Claude to your Postgres instance enables powerful data exploration and query assistance. MCP servers provide secure, controlled database access. Here are the best options for PostgreSQL integration.

Recommended MCP Servers

Postgres MCP Pro

crystaldba/postgres-mcp

>2k

All-in-one PostgreSQL server with index tuning, query optimization, health checks, and safe SQL execution.

Key Features

  • Index tuning recommendations
  • EXPLAIN plan analysis
  • Database health monitoring
  • Safe read-only mode

Installation

pip install postgres-mcp

Postgres MCP Pro is the premium choice for PostgreSQL. Beyond basic queries, it offers index tuning, query plan analysis, and health monitoring. Essential for serious database work where performance matters.

Best for: Performance optimization and DBA tasks View on GitHub

Postgres MCP (Anthropic)

anthropics/mcp-server-postgres

>2k

Official Anthropic PostgreSQL server for schema inspection and query execution.

Key Features

  • Schema exploration
  • Query execution
  • Official Anthropic support
  • Simple configuration

Installation

npx @modelcontextprotocol/server-postgres

The official Postgres MCP from Anthropic provides straightforward database access. Explore schemas, run queries, and analyze data with minimal setup. Perfect for developers who need reliable database connectivity.

Best for: Basic PostgreSQL access View on GitHub

Supabase MCP

supabase-community/supabase-mcp

>2k

Official Supabase integration providing PostgreSQL access through the Supabase platform.

Key Features

  • Supabase project integration
  • OAuth authentication
  • Read-only mode available
  • Zero local setup

Installation

Remote server: https://mcp.supabase.com/mcp

If your PostgreSQL runs on Supabase, this is the easiest path. Just add the remote server URL, authenticate with OAuth, and you're connected. No credentials to manage, no local setup required.

Best for: Supabase PostgreSQL projects View on GitHub

Quick Comparison

MCPStarsBest ForProsCons
Postgres MCP Pro>1.5kPerformance tuningIndex optimizationMore features to learn
Postgres MCP>2kGeneral accessOfficial, simpleBasic features
Supabase MCP>2kSupabase projectsZero setupSupabase only

Getting Started

To connect to PostgreSQL with Claude Code:

  1. For any PostgreSQL, use the official server:

    npx @modelcontextprotocol/server-postgres postgresql://user:pass@host:5432/db
    
  2. For Supabase, just add the remote server URL to your config.

  3. For performance work, install Postgres MCP Pro:

    pip install postgres-mcp
    

Example prompt: "Connect to my database and show me the schema for the users table."

Related Guides

Frequently Asked Questions

What is the best MCP server to connect PostgreSQL with Claude Code?

For general PostgreSQL use, Postgres MCP Pro by Crystal DBA (github.com/crystaldba/postgres-mcp) is the recommended choice. It provides configurable read/write access, index tuning, EXPLAIN plan analysis, and health checks. Anthropic's original reference Postgres server was deprecated and archived in 2025, so avoid it. If your database runs on Supabase, use the official Supabase MCP server instead.

How do I give Claude read-only access to my Postgres database?

Run Postgres MCP Pro in restricted mode with the --access-mode=restricted flag, which limits Claude to read-only queries. For Supabase, add read_only=true to the remote server URL. For true safety, also connect using a dedicated database role that only has SELECT privileges, since application-level limits are not a substitute for database permissions.

How do I set up the PostgreSQL connection string for Claude Code?

Install Postgres MCP Pro with 'pipx install postgres-mcp' or 'uv pip install postgres-mcp', then add it to your Claude Code MCP config and pass the connection string via the DATABASE_URI environment variable, formatted as postgresql://user:password@host:5432/dbname. Avoid hardcoding credentials in shared config files; use environment variables and a least-privilege user.

Can Claude run database migrations on Postgres or only query it?

It depends on the server and its access mode. Postgres MCP Pro in unrestricted mode can run DDL, INSERT, UPDATE, and migrations, while restricted mode allows reads only. The Supabase MCP server exposes an apply_migration tool, but it is disabled in read-only mode. For production databases, run read-only and apply migrations through your own reviewed pipeline.

Keep Exploring

WebMCP Developers