Skip to content
MCP server

PostgreSQL

MCP server for PostgreSQL: local, Docker, RDS, Neon, Supabase, or behind an SSH bastion.

Runs locallyStatus and history
Install
Pricing
Open source
Runs
Locally
Version
0.3.1
Listing updated
4 days ago

About PostgreSQL

PostgreSQL is an open-source MCP server for databases. It runs locally on your machine and is started by your AI client.

Install

Run in a terminal

claude mcp add mcp-postgres-server --env DATABASE_URL='<DATABASE_URL>' --env PG_PASSWORD='<PG_PASSWORD>' --env PG_SSH_PASSPHRASE='<PG_SSH_PASSPHRASE>' --env PG_SSH_PASSWORD='<PG_SSH_PASSWORD>' -- npx -y mcp-postgres-server

Add to claude_desktop_config.json

{
  "mcpServers": {
    "mcp-postgres-server": {
      "command": "npx",
      "args": [
        "-y",
        "mcp-postgres-server"
      ],
      "env": {
        "DATABASE_URL": "<DATABASE_URL>",
        "PG_PASSWORD": "<PG_PASSWORD>",
        "PG_SSH_PASSPHRASE": "<PG_SSH_PASSPHRASE>",
        "PG_SSH_PASSWORD": "<PG_SSH_PASSWORD>"
      }
    }
  }
}

Add to ~/.cursor/mcp.json

{
  "mcpServers": {
    "mcp-postgres-server": {
      "command": "npx",
      "args": [
        "-y",
        "mcp-postgres-server"
      ],
      "env": {
        "DATABASE_URL": "<DATABASE_URL>",
        "PG_PASSWORD": "<PG_PASSWORD>",
        "PG_SSH_PASSPHRASE": "<PG_SSH_PASSPHRASE>",
        "PG_SSH_PASSWORD": "<PG_SSH_PASSWORD>"
      }
    }
  }
}

Add to .vscode/mcp.json

{
  "servers": {
    "mcp-postgres-server": {
      "type": "stdio",
      "command": "npx",
      "args": [
        "-y",
        "mcp-postgres-server"
      ],
      "env": {
        "DATABASE_URL": "<DATABASE_URL>",
        "PG_PASSWORD": "<PG_PASSWORD>",
        "PG_SSH_PASSPHRASE": "<PG_SSH_PASSPHRASE>",
        "PG_SSH_PASSWORD": "<PG_SSH_PASSWORD>"
      }
    }
  }
}

Add to ~/.codeium/windsurf/mcp_config.json

{
  "mcpServers": {
    "mcp-postgres-server": {
      "command": "npx",
      "args": [
        "-y",
        "mcp-postgres-server"
      ],
      "env": {
        "DATABASE_URL": "<DATABASE_URL>",
        "PG_PASSWORD": "<PG_PASSWORD>",
        "PG_SSH_PASSPHRASE": "<PG_SSH_PASSPHRASE>",
        "PG_SSH_PASSWORD": "<PG_SSH_PASSWORD>"
      }
    }
  }
}

Add to your client's MCP settings (most use this shape)

{
  "mcpServers": {
    "mcp-postgres-server": {
      "command": "npx",
      "args": [
        "-y",
        "mcp-postgres-server"
      ],
      "env": {
        "DATABASE_URL": "<DATABASE_URL>",
        "PG_PASSWORD": "<PG_PASSWORD>",
        "PG_SSH_PASSPHRASE": "<PG_SSH_PASSPHRASE>",
        "PG_SSH_PASSWORD": "<PG_SSH_PASSWORD>"
      }
    }
  }
}

Configuration

  • DATABASE_URLFull connection string (preferred). Supports sslmode in the URL.
  • PG_HOSTDatabase host (fallback when DATABASE_URL is not set).
  • PG_PORTDatabase port.
  • PG_USERDatabase user.
  • PG_PASSWORDDatabase password.
  • PG_DATABASEDatabase name.
  • PG_ALLOW_WRITEWhen true, execute performs writes and reads are sent directly. Off (default) is read-only: execute refuses writes and each read runs in a READ ONLY transaction.
  • PG_SSLMODETLS mode. require/allow/prefer encrypt without verifying the certificate; verify-ca/verify-full verify it (supply a CA via PG_SSL_CA). Unlike libpq, allow/prefer do not fall back to plaintext, so a server without TLS needs disable.
  • PG_SSL_CAPath to a CA certificate file. Setting it by itself implies verify-full.
  • PG_ENABLE_RUNTIME_CONNECTRegister the connect_db tool (runtime credential switching).
  • PG_MAX_RESULT_BYTESByte budget for a query result sent to the model. Whole rows are kept while they fit; over the budget returnedRows < rowCount and truncated is true.
  • PG_STATEMENT_TIMEOUTStatement timeout in milliseconds, applied to every session.
  • PG_CONNECT_TIMEOUTTimeout in milliseconds for a single connect attempt (raise it for slow links or SSH tunnels).
  • PG_SSH_HOSTSSH bastion host. Setting it enables tunneling: the server reaches the database only through an SSH tunnel to this host. Needs the ssh2 optional dependency.
  • PG_SSH_PORTSSH bastion port.
  • PG_SSH_USERSSH username.
  • PG_SSH_PRIVATE_KEYPath to a private key file. If unset, auth falls back like ssh: a running agent (SSH_AUTH_SOCK), then a default key (~/.ssh/id_ed25519, id_rsa, id_ecdsa).
  • PG_SSH_PASSPHRASEPassphrase for the private key, if encrypted.
  • PG_SSH_AGENTtrue to use the ambient agent (SSH_AUTH_SOCK), or an explicit socket path / Windows named pipe.
  • PG_SSH_PASSWORDSSH login password. Opt-in; a key or agent takes precedence. Prefer keys, a bastion often disables password auth.
  • PG_SSH_FINGERPRINTPinned host-key fingerprint (SHA256:...). Host-key verification is mandatory and set only this way: without it the tunnel refuses to connect. Get it with ssh-keygen -lF host.
  • PG_SSH_KEEPALIVE_INTERVALSSH keepalive interval in ms; the tunnel drops after 3 unanswered keepalives, and the next call reconnects.

Pricing

Open source

PostgreSQL FAQ

Is PostgreSQL free?

Yes. PostgreSQL is open source, so you can use and self-host it for free.

How do I add PostgreSQL to Claude Code?

Run this command in your terminal: claude mcp add mcp-postgres-server --env DATABASE_URL='<DATABASE_URL>' --env PG_PASSWORD='<PG_PASSWORD>' --env PG_SSH_PASSPHRASE='<PG_SSH_PASSPHRASE>' --env PG_SSH_PASSWORD='<PG_SSH_PASSWORD>' -- npx -y mcp-postgres-server

What are the best alternatives to PostgreSQL?

The closest alternatives to PostgreSQL are Midplane, PlatypusDB, Lorebase, Sovaria and model2data studio.

Alternatives to PostgreSQL

  1. Midplane

    MCP server

    Postgres gateway for AI agents with table grants, column masking and approval for writes

    Runs locally
    • Open source
    • 5 GitHub stars
    • Databases
  2. PlatypusDB

    MCP server

    Gives MCP clients access to your PlatypusDB database connections, scoped per agent

    Runs locally
    • Open source
    • Databases
  3. Lorebase

    MCP server

    Lets agents read and change a governed, versioned Postgres knowledge graph

    Runs locally
    • Open source
    • Knowledge and memory
  4. Sovaria

    MCP server

    Manage a Sovaria backend (Postgres, auth, storage, functions) from an AI assistant

    Not checked yet
    • 4 tools
    • Remote
    • Freemium
    • Databases
  5. Generate realistic connected test data, dbt projects and dashboards from a schema

    • Remote
    • Freemium
    • Databases
  6. semlayer

    MCP server

    Infers a semantic layer from your data warehouse and serves it to agents

    Runs locally
    • Free
    • Databases