Is it safe to connect AI to your database?

Yes, when read-only is enforced in two places: a database user that can only SELECT, and a gateway that refuses anything else before it reaches the database. Connecting an agent with a full-access connection string is the risky version.

Updated 26 Nov 2026

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.

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

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.

Frequently asked

Is it safe to give AI read-only access to my database? +

Yes, if read-only is enforced in two places: a database user that can only SELECT, and a gateway that refuses anything but a single SELECT before it reaches the database. Add a row cap, a query timeout and a log of every query, and connect a replica when you can.

What's the biggest risk of connecting AI to a database? +

An agent running a destructive statement it generated, and the database password sitting in a plaintext config file on every machine. A read-only user plus a gateway that keeps the credentials server-side removes both.

Is a read-only database user enough on its own? +

It stops writes, which is the most important part. It does not cap a SELECT that scans a huge table, keep a record of what the AI read, or let you cut off one agent without changing the password. Those come from the gateway.

Can the AI leak my data? +

An agent can only return what its database user can read. Grant SELECT on the tables it needs and nothing else, connect a replica if you have one, and use the query log to see what was read.

Should I connect production or a replica? +

A read-only replica is better: no load on live traffic and nothing an agent query can block. If you connect production, use a read-only user with access only to the tables the agent needs.

Does AI2SQL ever change data in my database? +

No. AI2SQL's agent tools and the web builder run only reads; writes, DDL and multi-statement batches are refused before they are sent. If you ask for an UPDATE, AI2SQL writes the SQL for you to copy and run yourself.

Connect your database read-only

Add PostgreSQL, MySQL or SQL Server once with a read-only user. Every query from Claude, ChatGPT or your own agent is checked, capped and logged.

Start free →

Related guides