home / mcp / postgresql mcp server

PostgreSQL MCP Server

Provides read-only access to PostgreSQL schemas and read-only queries for LLMs and tooling.

Installation
Add the following to your MCP client configuration file.

Configuration

View docs
{
  "mcpServers": {
    "ampcome-mcps-postgres-mcp": {
      "url": "https://mcp.postgres.example/mcp"
    }
  }
}

The PostgreSQL MCP Server provides read-only access to PostgreSQL databases, letting you inspect schemas and run safe, read-only queries. It is designed for language models and tooling to explore database structures and retrieve data without modifying anything.

How to use

You can connect to the PostgreSQL MCP Server from an MCP client using either a remote HTTP configuration or a local stdio configuration. The server exposes a query tool that runs read-only SQL within a transaction, and it serves table schema information so you can understand the database structure before querying.

How to install

Choose one of the supported installation methods shown here. Each method provides a complete, runnable configuration you can copy into your MCP client setup.

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

If you prefer using NPX to run the server directly, use the following configuration.

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

For quick setup inside a development environment or VS Code, you can also configure MCP via a JSON snippet in your VS Code settings or workspace file as shown here.

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

If you are using Docker with a host-based PostgreSQL server, the following example demonstrates how to run the MCP server with a host URL. Remember to replace mydb with your actual database name.

Additional installation details

Docker note: When running Docker on macOS and your PostgreSQL server is on the host, use host.docker.internal in the connection URL instead of localhost.

Building the MCP server image can be done with a standard Docker build command as shown.

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

Notes and security

The server is designed for read-only access. Ensure your application uses transactions set to READ ONLY for all executed queries.

Configuration references

The server exposes a Query tool for executing read-only SQL against the connected database and returns results in a secure, read-only context. It also serves table schemas via a dedicated endpoint so you can discover column names and data types across tables.

Available tools

query

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