BLUENEXUS INTEGRATIONSHOW-TO

How to give AI read-only access to your Postgres database, safely

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

A role of its ownNot your app's user. A login that only the AI uses, so you can limit and audit it.
Only what it needsGrant SELECT on views in a reporting schema, without personal data or secrets.
Time limitsA statement timeout and a connection limit, set on the role.
A replicaA read replica can't be written to, and keeps heavy queries off your app.

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_only is 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. Run REVOKE CREATE ON SCHEMA public FROM PUBLIC;
  • pg_read_all_data is 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 uses FORCE 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?

1Paste this into Claude or ChatGPT

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 prompt

Anything sensitive?

1Paste this into Claude or ChatGPT

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 prompt

Are the limits on?

1Paste this into Claude or ChatGPT

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 prompt

A 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.

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.

Questions people ask about PostgreSQL and AI

How do I create a read-only user in Postgres?

Create a login role, grant CONNECT on the database, USAGE on the schema and SELECT on the tables or views it needs. On Postgres 14 and later, pg_read_all_data grants read access to everything, which is usually more than an AI needs.

Is a read-only role enough for AI access?

It stops writes, but not heavy queries. Add a statement_timeout and a connection limit on the role, grant only the views it needs, and prefer a read replica.

Does default_transaction_read_only make a role read-only?

Not on its own. It's a session default, and a session can change it. Use it as a backstop, and rely on grants for the real limit.

Can EXPLAIN ANALYZE change data?

Yes. EXPLAIN ANALYZE actually runs the statement, so on an UPDATE or DELETE it changes data. Plain EXPLAIN only shows the plan.

Should AI query a read replica?

Where you have one, yes. A replica can't be written to and keeps heavy queries off your app. Long queries on a replica can be canceled when they conflict with incoming changes.

Subscribe to our blog

Product insights, AI and product thinking delivered to your inbox.