# pgleash

> pgleash is an open-source MCP server for databases. Postgres proxy that stops AI agents from running destructive or out-of-scope SQL. It runs locally on your machine and is started by your AI client.

- **Kind:** MCP server
- **Pricing:** Open source
- **Platforms:** CLI, API, Self-hosted
- **Repository:** https://github.com/mufasil/pgleash
- **MCP Registry name:** io.github.mufasil/pgleash
- **Categories:** [databases](https://ai-powered.com/categories/databases), [security](https://ai-powered.com/categories/security)
- **Listing updated:** 2026-12-01

## About

pgleash sits between an agent and a Postgres database and parses each statement with Postgres's own parser before it reaches the server. Reads pass through, while statements such as DROP, TRUNCATE, unbounded DELETE, and writes hidden inside CTEs or EXPLAIN ANALYZE are stopped. The agent connects with a pgleash token and never receives the real database credentials.

Beyond statement types, it can block specific tables, columns and functions, cap the number of rows returned, enforce a statement timeout, and write an audit log of every query with the rule that blocked it. It runs as a standalone proxy that psql and common Postgres drivers can connect to, or as an MCP server exposing query, list_tables and describe_table tools.

It suits developers who want coding agents to query production-like data without holding the real connection string. Its authors describe it as a guardrail rather than a sandbox, so it is best paired with a least-privilege database role.

## Features

- **Read-only by default:** Only reads, EXPLAIN, SHOW and transaction control are allowed, and the session cannot be switched back to writable.
- **Parser-based statement checks:** Each statement is parsed with Postgres's own parser, so writes hidden in CTEs or EXPLAIN ANALYZE are caught.
- **Guarded write mode:** An optional mode allows INSERT and filtered UPDATE or DELETE while keeping DDL and TRUNCATE blocked.
- **Denied tables and columns:** Named tables and columns can be off-limits, and denied columns are returned as NULL in results.
- **Row cap and timeouts:** Results are capped at 1,000 rows by default and statements have a 10 second timeout.
- **Audit log:** Every query, allowed or blocked, is written as a JSON line with the matching rule.
- **Offline policy check:** A check command applies the same rules to a query string without connecting to a database.

## Good for

- Letting a coding agent explore a development database without risking destructive writes
- Preventing an agent from reading sensitive columns such as email or password hashes
- Auditing which queries an AI agent ran against a database
- Testing SQL policy rules in CI or agent hooks before deployment
- Giving an MCP client read access to Postgres tables with row limits

## Install

### Claude Code: Add with the CLI (Terminal)

```sh
claude mcp add pgleash --env PGLEASH_UPSTREAM='<PGLEASH_UPSTREAM>' -- npx -y pgleash
```

Add --scope user to use it in every project, or --scope project to share it in .mcp.json.

### Claude Code: Or share it with your project (.mcp.json)

```json
{
  "mcpServers": {
    "pgleash": {
      "command": "npx",
      "args": [
        "-y",
        "pgleash"
      ],
      "env": {
        "PGLEASH_UPSTREAM": "<PGLEASH_UPSTREAM>"
      }
    }
  }
}
```

### Claude Desktop (claude_desktop_config.json)

```json
{
  "mcpServers": {
    "pgleash": {
      "command": "npx",
      "args": [
        "-y",
        "pgleash"
      ],
      "env": {
        "PGLEASH_UPSTREAM": "<PGLEASH_UPSTREAM>"
      }
    }
  }
}
```

Settings → Developer → Edit Config, then quit and reopen Claude Desktop.

### Cursor (~/.cursor/mcp.json)

```json
{
  "mcpServers": {
    "pgleash": {
      "command": "npx",
      "args": [
        "-y",
        "pgleash"
      ],
      "env": {
        "PGLEASH_UPSTREAM": "<PGLEASH_UPSTREAM>"
      }
    }
  }
}
```

Use .cursor/mcp.json in a project to share it with the project.

### VS Code (~/.copilot/mcp-config.json)

```json
{
  "mcpServers": {
    "pgleash": {
      "type": "stdio",
      "command": "npx",
      "args": [
        "-y",
        "pgleash"
      ],
      "env": {
        "PGLEASH_UPSTREAM": "<PGLEASH_UPSTREAM>"
      }
    }
  }
}
```

Or put the same entry in .mcp.json at your workspace root to share it.

### Codex: Add with the CLI (Terminal)

```sh
codex mcp add pgleash --env PGLEASH_UPSTREAM='<PGLEASH_UPSTREAM>' -- npx -y pgleash
```

### Codex: Or add to config.toml (~/.codex/config.toml)

```toml
[mcp_servers.pgleash]
command = "npx"
args = ["-y", "pgleash"]
env = { PGLEASH_UPSTREAM = "<PGLEASH_UPSTREAM>" }
```

### Gemini CLI and Antigravity: Gemini CLI (~/.gemini/settings.json)

```json
{
  "mcpServers": {
    "pgleash": {
      "command": "npx",
      "args": [
        "-y",
        "pgleash"
      ],
      "env": {
        "PGLEASH_UPSTREAM": "<PGLEASH_UPSTREAM>"
      }
    }
  }
}
```

Or run: gemini mcp add -s user -e PGLEASH_UPSTREAM='<PGLEASH_UPSTREAM>' pgleash npx -- -y pgleash

### Gemini CLI and Antigravity: Antigravity (~/.gemini/config/mcp_config.json)

```json
{
  "mcpServers": {
    "pgleash": {
      "command": "npx",
      "args": [
        "-y",
        "pgleash"
      ],
      "env": {
        "PGLEASH_UPSTREAM": "<PGLEASH_UPSTREAM>"
      }
    }
  }
}
```

Use .agents/mcp_config.json to add it to one workspace only.

### Devin Desktop (~/.config/devin/mcp_config.json)

```json
{
  "mcpServers": {
    "pgleash": {
      "command": "npx",
      "args": [
        "-y",
        "pgleash"
      ],
      "env": {
        "PGLEASH_UPSTREAM": "<PGLEASH_UPSTREAM>"
      }
    }
  }
}
```

On Windows the file is %APPDATA%\\devin\\mcp_config.json. Use .devin/mcp_config.json to share it with a project.

### Cline (cline_mcp_settings.json)

```json
{
  "mcpServers": {
    "pgleash": {
      "command": "npx",
      "args": [
        "-y",
        "pgleash"
      ],
      "env": {
        "PGLEASH_UPSTREAM": "<PGLEASH_UPSTREAM>"
      }
    }
  }
}
```

Open it from the MCP Servers icon → Configure → Configure MCP Servers.

### Zed (Zed settings.json)

```json
{
  "context_servers": {
    "pgleash": {
      "command": "npx",
      "args": [
        "-y",
        "pgleash"
      ],
      "env": {
        "PGLEASH_UPSTREAM": "<PGLEASH_UPSTREAM>"
      }
    }
  }
}
```

Open it with zed: open settings file. Merge context_servers into the settings you already have.

### JetBrains AI Assistant and Junie: AI Assistant (Settings → Tools → AI Assistant → Model Context Protocol (MCP) → Add)

```json
{
  "mcpServers": {
    "pgleash": {
      "command": "npx",
      "args": [
        "-y",
        "pgleash"
      ],
      "env": {
        "PGLEASH_UPSTREAM": "<PGLEASH_UPSTREAM>"
      }
    }
  }
}
```

The AI Assistant docs only show command and args. If the env block is ignored, use Junie for this server.

### JetBrains AI Assistant and Junie: Junie (~/.junie/mcp/mcp.json)

```json
{
  "mcpServers": {
    "pgleash": {
      "command": "npx",
      "args": [
        "-y",
        "pgleash"
      ],
      "env": {
        "PGLEASH_UPSTREAM": "<PGLEASH_UPSTREAM>"
      }
    }
  }
}
```

Use .junie/mcp/mcp.json to share it with a project.

### Kiro (~/.kiro/settings/mcp.json)

```json
{
  "mcpServers": {
    "pgleash": {
      "command": "npx",
      "args": [
        "-y",
        "pgleash"
      ],
      "env": {
        "PGLEASH_UPSTREAM": "<PGLEASH_UPSTREAM>"
      }
    }
  }
}
```

Use .kiro/settings/mcp.json to add it to one workspace only.

### Other clients (your client's MCP settings (most use this shape))

```json
{
  "mcpServers": {
    "pgleash": {
      "command": "npx",
      "args": [
        "-y",
        "pgleash"
      ],
      "env": {
        "PGLEASH_UPSTREAM": "<PGLEASH_UPSTREAM>"
      }
    }
  }
}
```

## Tools

From the tool list in its README, going by the names, all 3 tools only read data. From the server's own tool labels and tool names; not verified by us.

- `query` (name suggests: reads): Runs a SQL query through the pgleash rules.
- `list_tables` (name suggests: reads): Lists tables available through the proxy.
- `describe_table` (name suggests: reads): Describes the columns of a table.

## FAQ

### Does pgleash need a database to check queries?

No. The pgleash check command runs the same policy on a query string without connecting to a database.

### Can the agent see the real database credentials?

No. The agent connects with a pgleash token, while the upstream connection string stays with the proxy.

### What happens when a query is blocked?

The client gets a normal Postgres error that states the reason and includes a hint.

### Which MCP tools does pgleash expose?

In MCP mode it provides query, list_tables and describe_table.

### Is pgleash free?

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

### How do I add pgleash to Claude Code?

Run this command in your terminal: claude mcp add pgleash --env PGLEASH_UPSTREAM='<PGLEASH_UPSTREAM>' -- npx -y pgleash

### What are the best alternatives to pgleash?

The closest alternatives to pgleash are Pgmoat, Pg Readonly MCP, Postgres Secured (Aegis Zero-Trust), pgbrake and Lokra.

## Where this comes from

- Description: written from the project's README and its MCP Registry entry.
- Install steps: from the official MCP Registry entry.

## Alternatives

- [Pgmoat](https://ai-powered.com/mcp/pgmoat): Pgmoat is an open-source MCP server for databases.
- [Pg Readonly MCP](https://ai-powered.com/mcp/pg-readonly-mcp): Pg Readonly MCP is an open-source MCP server for databases.
- [Postgres Secured (Aegis Zero-Trust)](https://ai-powered.com/mcp/postgres-secured-aegis-zero-trust): Postgres Secured (Aegis Zero-Trust) is an open-source MCP server for databases.
- [pgbrake](https://ai-powered.com/mcp/pgbrake): pgbrake is an open-source MCP server for databases.
- [Lokra](https://ai-powered.com/mcp/lokra): Lokra is an open-source MCP server for databases.
- [Orion Gate](https://ai-powered.com/mcp/orion-gate): Orion Gate is an open-source MCP server for databases.
- [PostgreSQL (hardened, read-only)](https://ai-powered.com/mcp/postgresql-hardened-read-only): PostgreSQL (hardened, read-only) is an open-source MCP server for databases.
- [Safedb MCP](https://ai-powered.com/mcp/safedb-mcp): Safedb MCP is an open-source MCP server for databases.

---
Source: https://ai-powered.com/mcp/pgleash
