This guide walks through connecting PostgreSQL to Claude Code using QueryBear’s managed MCP server. End result: your terminal Claude session can read your Postgres database safely — no write access, no sensitive columns exposed, every query logged.Documentation Index
Fetch the complete documentation index at: https://docs.querybear.com/llms.txt
Use this file to discover all available pages before exploring further.
What you’ll need
- A QueryBear account (sign up free)
- A PostgreSQL database (any version 12+)
- Claude Code installed (
npm i -g @anthropic-ai/claude-codeor via your preferred installer)
Step 1: Create a read-only PostgreSQL role
Run this as a Postgres superuser. It creates aquerybear role that can only SELECT:
Step 2: Add the connection to QueryBear
In the QueryBear dashboard → Connections → New connection → PostgreSQL:- Host — e.g.
db.example.comor your RDS endpoint - Port —
5432 - Database — your DB name
- User —
querybear - Password — from Step 1
- SSL mode —
require
password_hash, email, etc.).
Step 3: Add QueryBear to Claude Code
One command:claude if you had a session open.
Step 4: Authorize and verify
Start a Claude Code session:“What QueryBear tools do you have, and what connections are available?”You should see
list_connections, get_schema, run_query, and your Postgres connection listed.
Try it
“In production Postgres, how many users signed up in the last 7 days, broken down by signup source?”Claude calls
get_schema to find the relevant tables, writes the SQL, calls run_query, and returns the result.
Postgres + Claude Code gotchas
- CTE writes are blocked. Postgres allows
WITH x AS (DELETE ...) SELECT * FROM x. QueryBear’s parser rejects this — if Claude generates such a query, it’ll see a clear error. - Connection pooling-mode Postgres (PgBouncer transaction mode) is fine for read queries. No special config needed.
- PgVector works transparently. Operators like
<->,<=>,<#>are allowed. - Materialized views and views appear in
get_schemaand are queryable. - Schemas other than
publicneed an additionalGRANT USAGE ON SCHEMA <name>for thequerybearrole.
Related
- PostgreSQL MCP server — Postgres-specific deep dive
- Claude Code client — Claude Code overview
- Security model — what the gateway protects against