Skip to content
← Products

MCP servers

pg-guard-mcp

Read-only, audited, PII-masked access to PostgreSQL for AI agents.

An MCP server that lets an AI agent read a PostgreSQL database without being able to change it, stall it, or see the columns you've marked as personal data. Every call is written to an append-only audit log.

agent ──stdio──▶ pg-guard-mcp ──▶ SQL guard ──▶ READ ONLY transaction ──▶ PostgreSQL
                                   (parse tree)   (timeouts, row cap,       (a role with
                                                   masking, rollback)        SELECT only)
                         └──▶ audit.jsonl

Need MySQL or SQLite, or opt-in writes? mcp-database covers three engines with the same read-only-by-default approach, without masking or the audit log.

Setup

Requires Node.js 24 or later. The test suite runs against PostgreSQL 17. The catalog queries use nothing newer than PostgreSQL 13, but older versions have not been tested.

1. Create a dedicated role

CREATE ROLE agent_reader LOGIN PASSWORD 'change-me'
  NOSUPERUSER NOCREATEDB NOCREATEROLE NOREPLICATION;
ALTER ROLE agent_reader SET default_transaction_read_only = on;
ALTER ROLE agent_reader SET statement_timeout = '5s';

GRANT CONNECT ON DATABASE yourdb TO agent_reader;
GRANT USAGE ON SCHEMA public TO agent_reader;
GRANT SELECT ON public.orders, public.customers, public.products TO agent_reader;

-- Column-level grants are stricter than masking: the column cannot be read at all.
-- REVOKE SELECT ON public.customers FROM agent_reader;
-- GRANT SELECT (id, country, created_at) ON public.customers TO agent_reader;

If you can, point the server at a read replica.

2. Install and configure

From a clone of this repository:

npm ci && npm run build
cp pg-guard.example.json pg-guard.json

Edit pg-guard.json (reference below). The connection string goes in the DATABASE_URL environment variable, never in the config file, so the config can be committed and reviewed like any other policy.

3. Connect a client

Claude Code:

claude mcp add pg-guard --env DATABASE_URL=postgres://agent_reader:change-me@localhost:5432/yourdb -- node /absolute/path/to/pg-guard-mcp/dist/index.js --config /absolute/path/to/pg-guard.json

Claude Desktop: add this to claude_desktop_config.json (Settings → Developer → Edit Config):

{
  "mcpServers": {
    "pg-guard": {
      "command": "node",
      "args": [
        "/absolute/path/to/pg-guard-mcp/dist/index.js",
        "--config",
        "/absolute/path/to/pg-guard.json"
      ],
      "env": {
        "DATABASE_URL": "postgres://agent_reader:change-me@localhost:5432/yourdb"
      }
    }
  }
}

Use absolute paths, including audit.path in the config file. Desktop clients do not start the server in your project folder. Any other MCP client that can launch a stdio server is set up the same way.

In the docs