home / mcp / postgresql mcp server

PostgreSQL MCP Server

Provides read‑only access to PostgreSQL databases with schema inspection and read‑only queries.

Installation
Add the following to your MCP client configuration file.

Configuration

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

This MCP server provides read‑only access to PostgreSQL databases, letting you inspect schemas and run safe queries. It is designed for large language models and other tools to explore database structures without modifying data.

How to use

You can use the PostgreSQL MCP server with an MCP client to explore database schemas and execute read‑only SQL. Start the server (via Docker or NPX) and point your client to the appropriate runtime command. Your client will then be able to request table schemas and issue read‑only queries within a TRANSACTION READ ONLY context.

Two common ways to run the MCP server are provided below. Choose the one that fits your environment, then configure your MCP client to connect using the resulting runtime. The server exposes schema information for each table and a tool to execute read‑only queries.

How to install

Prerequisites you need before installing either option are listed here.

  • Docker is installed and running on your system
  • Node.js is installed if you plan to use NPX (Node.js 12+ is typical)
  • Access to a PostgreSQL database with a reachable connection URL
docker build -t mcp/postgres -f src/postgres/Dockerfile .

If you prefer using NPX to run the MCP server directly without building a Docker image, ensure Node.js and npm are installed, then use NPX as shown.

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

Additional sections

Configuration, security, and practical notes are below to help you operate the MCP server smoothly.

- The server runs read‑only queries inside a read‑only transaction, ensuring data cannot be modified through the MCP interface.

- The server provides table schemas via a schema endpoint pattern, helping you understand column names and data types for each table.

- If you run the Docker version, you can connect to your database using a connection string like postgresql://host:port/db-name inside the container command. For Docker on macOS, use host.docker.internal when the database is on the host network.

Available tools

query

Execute read‑only SQL queries against the connected PostgreSQL database within a READ ONLY transaction.