Short answer: yes, if the AI can only read. Give it a database user with
SELECT on the tables it needs, and put a gateway in front that accepts one read query at a
time. What makes it unsafe is handing an agent the same connection string your application uses.
What "read-only" really means
Read-only has to hold at two layers, because each one misses something the other catches.
- The database user. If the user only has
SELECT, the database itself refuses writes, whatever SQL arrives. This is the layer you control and the one to set up first. - The gateway in front. It checks every statement before sending it, so a write never reaches the database at all, and it adds what a user cannot: a row cap, a timeout, a log of each query and a token you can revoke without changing the password.
A read-only transaction on its own is not enough. The archived official Postgres MCP server ran queries in a read-only transaction and still had a documented SQL injection that got around it. Check the statement first.
What AI2SQL blocks before a query runs
Every run_query call from an AI agent (Claude, ChatGPT, Cursor or your own code through
AI2SQL's MCP server) must be exactly one SELECT or
WITH statement. These are refused with a read_only_violation error, before
anything is sent to your database:
| Refused | Examples |
|---|---|
| Writes and schema changes | INSERT, UPDATE, DELETE, MERGE, CREATE, DROP, ALTER, TRUNCATE, GRANT, REVOKE, COPY, CALL |
| More than one statement | SELECT 1; DROP TABLE users is refused as a batch before anything runs |
| Writes hidden inside a read | WITH d AS (DELETE … RETURNING *) SELECT … and EXPLAIN ANALYZE DELETE … |
| SQL Server escape hatches | EXEC / EXECUTE, xp_cmdshell, sp_OACreate, OPENROWSET, OPENDATASOURCE, OPENQUERY, WAITFOR |
| PostgreSQL and MySQL file and network functions | pg_read_file, pg_ls_dir, dblink, pg_sleep, pg_terminate_backend, LOAD_FILE, SLEEP, BENCHMARK |
On top of that, agent queries return at most 1,000 rows and stop after
10 seconds on PostgreSQL, MySQL and SQL Server. Each call is logged in Agent activity
with the query, the database and the token that ran it. In the AI2SQL web app, the Run button also
refuses any statement that contains a write or schema keyword, including data-changing CTEs and
EXPLAIN ANALYZE DELETE.
Create a read-only user
Grant reads on the tables the AI needs. Replace the names and password with your own.
PostgreSQL
CREATE ROLE ai_reader WITH LOGIN PASSWORD 'choose-a-strong-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;
-- tables created later:
ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT SELECT ON TABLES TO ai_reader;
-- belt and braces: every session starts read-only
ALTER ROLE ai_reader SET default_transaction_read_only = on; MySQL
CREATE USER 'ai_reader'@'%' IDENTIFIED BY 'choose-a-strong-password';
GRANT SELECT ON shop.* TO 'ai_reader'@'%';
-- or only some tables:
-- GRANT SELECT ON shop.orders TO 'ai_reader'@'%'; SQL Server
CREATE LOGIN ai_reader WITH PASSWORD = 'Choose-a-Strong-Password1';
USE shop;
CREATE USER ai_reader FOR LOGIN ai_reader;
ALTER ROLE db_datareader ADD MEMBER ai_reader;
-- keep it away from sensitive tables:
-- DENY SELECT ON dbo.payment_cards TO ai_reader; Step-by-step guides for Claude: connect Claude to MySQL and connect Claude to SQL Server. For any agent, see how to connect an AI agent to a database.
The limits, plainly
- Reads can still expose data. Read-only stops changes, not reading. Whatever the
user can
SELECT, the AI can return. Keep sensitive tables out of its grants. - A read can still be expensive. The 10-second timeout and 1,000-row cap apply to
agent queries. Queries run from the AI2SQL web app on PostgreSQL and MySQL have no statement timeout
of their own, so for large production databases, set one on the read-only user (for example
ALTER ROLE ai_reader SET statement_timeout = '15s'on PostgreSQL) or connect a replica. - The gateway is a second layer, not a replacement. If you connect AI2SQL with a user that can write, the statement checks still refuse writes, but the database would not. Use a read-only user anyway.
So, is it safe?
Yes, with a read-only user, a gateway that checks every statement, a row cap and timeout, and a log you can read. That is what AI2SQL sets up: add the database once with the read-only user, and every agent that connects through it can only read. For the control-by-control detail, see how to give an AI agent read-only database access.