How to create a read-only Postgres login for an AI agent
Updated
Create a separate login for the AI with CREATE ROLE, and give it CONNECT on the database, USAGE on the schema and SELECT on the tables it may read, nothing more. PostgreSQL then refuses any write from that login, whatever the AI sends. Give the AI this login, never your app's, and check what it can do with one command.
Step by step
- Create the loginAs an administrator, run CREATE ROLE ai_reader LOGIN with a long random password. Don't reuse your app's login: it can write.
- Let it connect and see the schemaGRANT CONNECT ON DATABASE shop TO ai_reader, then GRANT USAGE ON SCHEMA public TO ai_reader.
- Grant SELECT on what it may readEvery table in the schema with GRANT SELECT ON ALL TABLES IN SCHEMA public, or only some with GRANT SELECT ON public.orders, public.customers. A column grant, such as GRANT SELECT (id, status, total) ON public.orders, keeps the other columns out of reach.
- Cover the tables you add laterALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT SELECT ON TABLES TO ai_reader. Run it as the role that creates your tables, often the one your migrations use: default privileges apply only to tables that role creates.
- Check itRun npx -y @kaiva/postgres-mcp with the new login's connection URL and --check. It connects once, reports whether the login is a superuser and whether it can change data, and exits.
The check, with the new login in the URL:
npx -y @kaiva/postgres-mcp "postgresql://ai_reader:PASSWORD@HOST:5432/shop?sslmode=require" --checkWith Kaiva Cloud
A Kaiva Cloud database comes with a read-only login for AI clients already made. Your app keeps its own connection string, which reads and writes.
The whole setup in SQL
For a database called shop. Run it once as an administrator, then put ai_reader in the AI's connection URL.
CREATE ROLE ai_reader LOGIN PASSWORD 'choose-a-long-random-password';
GRANT CONNECT ON DATABASE shop TO ai_reader;
GRANT USAGE ON SCHEMA public TO ai_reader;
GRANT SELECT ON ALL TABLES IN SCHEMA public TO ai_reader;
ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT SELECT ON TABLES TO ai_reader;
ALTER ROLE ai_reader SET default_transaction_read_only = on;
ALTER ROLE ai_reader SET statement_timeout = '15s';Two settings that help, and what they don't do
default_transaction_read_only = on makes this login's transactions read-only unless a session says otherwise. It is a second guard, not the main one: a session can switch it off. The grants are what stop writes.
statement_timeout = '15s' stops any query from this login after 15 seconds, so one large query can't hold the database for long.
Row level security, on Supabase and elsewhere
If a table has row level security on, the new login sees only the rows a policy lets it read, and with no policy it sees none. Supabase turns it on for tables made in its Table Editor. Add a read policy for the login on each table it should read. Put a condition in place of true to share only some rows.
CREATE POLICY ai_reader_read ON public.orders FOR SELECT TO ai_reader USING (true);On PostgreSQL 14 and later
GRANT pg_read_all_data TO ai_reader lets the login read every table in every schema, now and later, in one line. It is wider than the grants above, and some managed providers don't let you grant it.
Use the login from an AI client
On your own machine, Kaiva Postgres MCP gives Claude Code, Cursor or Claude Desktop this login's view of the database, one read-only statement per call. It is free and open source.
For Claude.ai, ChatGPT or a team, Kaiva Bridge turns the tables this login can see into tools on a managed MCP server. A table or column the login can't read never becomes part of a tool, and the password is stored encrypted and never sent to the AI client.
Free includes 10 MCP servers, 200,000 tool calls a month and 200 pages of documents, with no card.
Other ways to do it
- A read replicaPoint the AI at a read replica instead of the main database. Writes fail there too, and heavy queries don't slow your app. It costs a second database.
- ViewsCreate views that show only the rows and columns the AI should see, and grant SELECT on the views alone.
- An MCP server's read-only settingMany MCP servers have one. Use it as well as the login, not instead: the login's grants are enforced by PostgreSQL itself.
Questions
- Is default_transaction_read_only enough on its own?
- No. A session can switch it off. Grant only SELECT, so PostgreSQL refuses writes whatever a session does.
- Why does the new login see no rows?
- The table probably has row level security on. Add a SELECT policy for the login on that table.
- Why does a table I added later say permission denied?
- Default privileges cover only tables created by the role that set them. Run ALTER DEFAULT PRIVILEGES as the role that creates your tables, or grant SELECT on the new table.
- Can I hide some columns?
- Yes. Grant SELECT only on the columns the AI may read, for example GRANT SELECT (id, status, total) ON public.orders TO ai_reader. A query that asks for another column is refused.
- Does this work for MySQL?
- The idea is the same: CREATE USER, then GRANT SELECT on the database to it. The database guide has the MySQL lines.