Connecting an AI to Postgres takes a few minutes. Making sure it can't change data, read what it shouldn't, or slow down your app takes a little longer, and it's worth doing first.
The shortcut most guides take is a read-only setting in the AI tool. That helps, but tool settings can be bypassed. The original reference MCP server for Postgres ran every query in a read-only transaction, and Datadog Security Labs showed a query could end that transaction and write. What holds is what Postgres itself enforces: the role, its grants and its limits.
Here are four layers, the SQL to set them up, three prompts to check them, and how a small engineering team keeps an eye on it every week.
Four layers of read-only
Four layers of read-only
Set it up in Postgres
Start with views in a reporting schema that leave out personal data and secrets. Then create the role and give it only those:
CREATE ROLE ai_reader LOGIN;
GRANT CONNECT ON DATABASE shop
TO ai_reader;
GRANT USAGE ON SCHEMA reporting
TO ai_reader;
GRANT SELECT ON ALL TABLES
IN SCHEMA reporting TO ai_reader;
ALTER DEFAULT PRIVILEGES
IN SCHEMA reporting
GRANT SELECT ON TABLES
TO ai_reader;
ON ALL TABLES covers the views that exist now. The default privileges line covers views created later by the role that runs it.
Add limits on the role itself:
ALTER ROLE ai_reader
SET statement_timeout = '15s';
ALTER ROLE ai_reader
SET idle_in_transaction_session_timeout = '30s';
ALTER ROLE ai_reader
SET default_transaction_read_only = on;
ALTER ROLE ai_reader CONNECTION LIMIT 5;
A few notes from the Postgres docs:
default_transaction_read_onlyis a backstop. It's a session default, and a session can change it. The grants are the real limit.- On Postgres 14 and earlier, any role can create tables in
public. RunREVOKE CREATE ON SCHEMA public FROM PUBLIC; pg_read_all_datais broad. Since Postgres 14, it grants read access to every table. Most AI roles need less.- Row-level security applies to this role, but not to superusers, roles with
BYPASSRLS, or table owners, unless the table usesFORCE ROW LEVEL SECURITY. - Point it at a replica if you can. Replicas can't be written to. Long queries may be canceled there, which the timeout makes rarer.
Do it in Claude or ChatGPT
Connect with the new role using our guide to PostgreSQL in Claude, ChatGPT and MCP, then check what it can actually do. All three prompts only read.
What can it see?
Using Postgres, tell me which role you're connected as, and list every schema, table and view you can read. Don't change anything.
Copy promptAnything sensitive?
Look at the columns you can read and list any that look like personal data or secrets: emails, phone numbers, addresses, payment details, tokens. Give the table and column names only. Don't show their values.
Copy promptAre the limits on?
Show this session's statement_timeout, default_transaction_read_only and idle_in_transaction_session_timeout settings, and whether you're on a replica. Don't change anything.
Copy promptA few things to know:
- Don't reuse your app's user. It can usually write, and may bypass row-level security.
- Keep secrets out of reach. Text in rows, like support messages, can carry instructions. A role that can't read tokens can't leak them.
- Plain EXPLAIN only. EXPLAIN ANALYZE runs the query, so on an UPDATE or DELETE it changes data.
- Check again when the schema changes. A new view can expose a column the old ones hid.
When a whole team asks data questions
On your own, you'd set this up once. In a company, new views are added every week, more people ask questions, and the role that was safe in October can see an email column in November.
Thimble & Co, a 45-person craft supplies subscription company in Portland, lets support, operations and finance ask data questions in BlueNexus. Rowan, the engineering lead, connected Postgres with the ai_reader role on a read replica. Every Monday at 09:00, the data agent checks the role's settings and grants, looks for columns that look like personal data, and reviews its own slow questions from the week.
Illustration of the BlueNexus app with example data.
Illustration of the BlueNexus app with example data.
The settings haven't changed, but a view added on Thursday shows customer emails. Three questions timed out, all asking for two years of orders at once. The agent prepares the next steps for Rowan.
Rowan approves, and fixes the view in a migration that afternoon. Wren asks for orders by quarter instead, and gets the answer in seconds.
For Rowan, the engineering lead. From Postgres. Nothing is posted or changed until Rowan approves it.
- Post the check in the engineering channelSettings, grants and the one exposed column
- Create a task for Rowan to hide email in refunds_vIn the Engineering project, with the suggested view definition
- Tell Wren to ask for orders by quarterIn the data questions channel. Two years at once hits the limit
- Change the viewLeft unticked: Rowan changes it in a migration
Illustration of the BlueNexus app with example data.
For connecting Postgres, see PostgreSQL in Claude, ChatGPT and MCP. For the team, see how ops, support and finance query Postgres. To give your team safe access to data, sign up for BlueNexus.