Connect Claude to PostgreSQL — the safe way

Claude can't see your database until you connect it. Here's how to give it read-only, logged, revocable access to PostgreSQL over MCP — without handing it your production password.

Short answer: connect Claude to PostgreSQL through an MCP server that only runs read-only SQL, and log in with a Postgres role that can only SELECT. Don't rely on the model's instructions to prevent writes; enforce it in the database. With AI2SQL you add the database once, add the connector https://builder.ai2sql.io/api/claude-mcp to Claude, and sign in. Your password never goes into a config file.

Step 1: a read-only Postgres role

CREATE ROLE claude_ro LOGIN PASSWORD 'a-long-random-password';
GRANT CONNECT ON DATABASE app TO claude_ro;
GRANT USAGE ON SCHEMA public TO claude_ro;
GRANT SELECT ON ALL TABLES IN SCHEMA public TO claude_ro;
ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT SELECT ON TABLES TO claude_ro;
ALTER ROLE claude_ro SET default_transaction_read_only = on;

Grant SELECT on specific tables instead of ALL TABLES to hide sensitive ones, and point it at a read replica if production is busy.

Step 2: add the database and the connector

  1. Add the database. In AI2SQL, open Connections, choose PostgreSQL and enter the claude_ro role.
  2. claude.ai or Claude Desktop: Customize → Connectors, add AI2SQL from the directory or as a custom connector with https://builder.ai2sql.io/api/claude-mcp, press Connect and sign in.
  3. Claude Code: run claude mcp add --transport http ai2sql https://builder.ai2sql.io/api/claude-mcp, then /mcp → ai2sql → Authenticate.

Details for every Claude app: Claude MCP SQL server.

Using a gateway key instead (scripts, CI)

Claude is great at writing SQL, but it can't answer questions about your data until it can reach your database. The usual way to wire it up — a raw PostgreSQL MCP server running on your laptop — has two problems: your database password sits in a plain config file, and most servers will run whatever SQL the model produces, including an UPDATE or DROP it hallucinated.

The AI2SQL Gateway fixes both. Claude connects to a hosted MCP endpoint that enforces read-only access, caps result size, times out long queries, and logs every call — and it authenticates with a revocable key instead of your credentials.

What you'll need

Gateway key setup

  1. Connect your database. In the AI2SQL Gateway, add your PostgreSQL connection. Credentials are encrypted and stay server-side.
  2. Generate a gateway key. The setup wizard creates a key scoped to your account plus the connection id for that database.
  3. Add the MCP server to Claude. Paste one line (or one JSON block for Claude Desktop) with your key and connection id:
Claude Code — terminal
$ claude mcp add --transport http ai2sql \
    https://builder.ai2sql.io/api/mcp \
    --header "x-ai2sql-key: $AI2SQL_KEY" \
    --header "x-ai2sql-connection: $CONNECTION_ID"
✓ ai2sql connected — 3 tools available
  run_query · describe_schema · list_connections

Get $AI2SQL_KEY and $CONNECTION_ID from the AI2SQL Gateway — connect a database and the setup wizard generates both for you.

That's it. Ask Claude something like "how many orders shipped last week, by status?" — it calls describe_schema to learn your tables, writes the SQL, runs it through run_query, and returns the answer. Every call is recorded on your gateway activity page.

What Claude can and can't do

Once connected, Claude can:

It cannot:

Why not just use a raw Postgres MCP server?

You can — the open-source Postgres MCP servers are fine for a local scratch database. But for a database that matters, "the model writes SQL and we run it" is one hallucinated statement away from a bad day, your password lives in a config file on every laptop, and there's no record of what the agent touched. The gateway trades that for read-only enforcement, encrypted credentials, revocable keys and a full audit trail. See is it safe to connect AI to your production database?

Frequently asked

Can Claude modify or delete data in my PostgreSQL database? +

No. The gateway classifies every statement before it runs and blocks INSERT, UPDATE, DELETE, DDL and dangerous functions. Queries also execute inside a read-only transaction, so even a statement that slips past the classifier cannot change data.

Do I paste my Postgres password into Claude's config? +

No. You add the connection once in AI2SQL, where credentials are encrypted at rest. Claude signs in with OAuth (or, for scripts, a scoped gateway key) and never sees your database password. You can revoke access at any time.

Which Claude products work with this? +

Claude Code, Claude Desktop and claude.ai all speak MCP over HTTP, so the same gateway endpoint works across them. Cursor and ChatGPT (developer-mode MCP) work too.

Is there a free tier? +

Signing in, listing connections, reading schemas, the demo_shop sample and a few SQL writing requests a day are free. Running queries on your own database needs Pro or Team, which start with a 7-day trial.

Give your agent a database it can't break

Connect a database, grab a key, paste one config. Read-only, logged and revocable from the first query.

Start free →

Related guides