Back to MCP Catalog

PostgreSQL MCP Server

DatabasesTypeScript
Read-only access to PostgreSQL databases for LLMs
Available Tools

query

Execute read-only SQL queries against the connected database

sql

The PostgreSQL MCP server provides a secure interface for LLMs to interact with PostgreSQL databases in a read-only capacity. It enables AI assistants to inspect database schemas and execute SQL queries without modifying data, making it ideal for data analysis and exploration tasks. This server automatically discovers and exposes table schemas from your connected database, allowing LLMs to understand your data structure. All queries are executed within READ ONLY transactions, ensuring data integrity while providing valuable insights from your PostgreSQL databases.

Overview

The PostgreSQL MCP server creates a bridge between AI assistants and your PostgreSQL databases, allowing for safe, read-only access to your data. This enables AI models to help with data analysis, query optimization, and database exploration without risking data modifications.

Installation

You can run the PostgreSQL MCP server using either Docker or NPX. Choose the method that best fits your environment.

Prerequisites

  • A running PostgreSQL database
  • Docker installed (for Docker method) or Node.js installed (for NPX method)

Installation with Claude Desktop

To use this server with Claude Desktop, add the following configuration to the "mcpServers" section of your claude_desktop_config.json:

Using Docker:

{
  "mcpServers": {
    "postgres": {
      "command": "docker",
      "args": [
        "run",
        "-i",
        "--rm",
        "mcp/postgres",
        "postgresql://host.docker.internal:5432/mydb"
      ]
    }
  }
}

Note for macOS users: When running Docker on macOS, use host.docker.internal instead of localhost if the PostgreSQL server is running on your host machine.

Using NPX:

{
  "mcpServers": {
    "postgres": {
      "command": "npx",
      "args": [
        "-y",
        "@modelcontextprotocol/server-postgres",
        "postgresql://localhost:5432/mydb"
      ]
    }
  }
}

Installation with VS Code

For VS Code integration, you can add the configuration to your User Settings (JSON) file or to a .vscode/mcp.json file in your workspace.

Using Docker:

{
  "mcp": {
    "inputs": [
      {
        "type": "promptString",
        "id": "pg_url",
        "description": "PostgreSQL URL (e.g. postgresql://user:pass@host.docker.internal:5432/mydb)"
      }
    ],
    "servers": {
      "postgres": {
        "command": "docker",
        "args": [
          "run",
          "-i",
          "--rm",
          "mcp/postgres",
          "${input:pg_url}"
        ]
      }
    }
  }
}

Using NPX:

{
  "mcp": {
    "inputs": [
      {
        "type": "promptString",
        "id": "pg_url",
        "description": "PostgreSQL URL (e.g. postgresql://user:pass@localhost:5432/mydb)"
      }
    ],
    "servers": {
      "postgres": {
        "command": "npx",
        "args": [
          "-y",
          "@modelcontextprotocol/server-postgres",
          "${input:pg_url}"
        ]
      }
    }
  }
}

Connection URL Format

The PostgreSQL connection URL follows this format:

postgresql://[user[:password]@][host][:port][/database]

Examples:

  • postgresql://localhost:5432/mydb (default user, no password)
  • postgresql://user:password@localhost:5432/mydb (with credentials)
  • postgresql://host.docker.internal:5432/mydb (for Docker on macOS)

Usage

Once configured, the PostgreSQL MCP server provides:

  1. Table Schema Information: The server automatically discovers and exposes schema information for all tables in your database, including column names and data types.

  2. Read-Only Query Execution: You can execute SQL queries against your database. All queries are executed within a READ ONLY transaction to ensure data safety.

Building from Source

If you prefer to build the Docker image yourself:

docker build -t mcp/postgres -f src/postgres/Dockerfile .

Security Considerations

  • The server only allows read-only access to your database
  • All queries are executed within READ ONLY transactions
  • Consider using a read-only database user for additional security
  • Be cautious about exposing sensitive data through this interface

Related MCPs

Milvus Vector Database
DatabasesPython

Connect to Milvus vector database for semantic search and vector operations

MotherDuck DuckDB
DatabasesPython

SQL analytics with DuckDB and MotherDuck for AI assistants

Alibaba Cloud Tablestore
DatabasesJava, Python

Connect to Alibaba Cloud Tablestore for vector search and RAG applications

About Model Context Protocol

Model Context Protocol (MCP) allows AI models to access external tools and services, extending their capabilities beyond their training data.

Generate Cursor Documentation

Save time on coding by generating custom documentation and prompts for Cursor IDE.