Check what your agent's database identity can write
Español: es/DOCTOR.md
Experimental: not covered by the 1.x stability guarantee (see STABILITY.md); it may change in a minor release.
The guard rejects writes in SQL, but the last line of defence is the database account itself. sqlguardllm doctor answers one question: can the identity the agent connects with modify the tables it can reach?
pip install "sqlguardllm[db]" # plus your driver: psycopg, pymssql or pyodbc
sqlguardllm doctor --db postgresql+psycopg://agent_ro@db.example/shop
sqlguardllm doctor --db mssql+pymssql://agent_ro@db.example/shop --schema dbo --format json
--db is a SQLAlchemy URL for the agent's identity (default: the DATABASE_URL variable, the same as sqlguardllm mcp). Use --schema (repeatable) to limit the scope, --timeout for connect and statement seconds, --format json for a machine-readable report.
Verdicts and exit codes
| Verdict | Exit | Meaning |
|---|---|---|
confirmed_write_access |
1 | At least one inspected table or view has INSERT, UPDATE, DELETE or TRUNCATE (PostgreSQL), or INSERT, UPDATE, DELETE, ALTER, CONTROL or TAKE OWNERSHIP (SQL Server), table-wide or on some columns. |
no_write_access_detected |
0 | None found in the inspected scope. This is not a proof of safety: read the list of what the check cannot see. |
inconclusive |
3 | No write confirmed, but something could not be decided: the scope has no visible object or names a schema that is not visible, a foreign table carries a write privilege (the remote server decides), or a permission could not be read. |
| (error) | 2 | Could not connect or the engine is unsupported. |
The report lists the checked scope, every object with a write privilege and which privileges, the owner of each object, elevated privileges, why a check was inconclusive, and a short remediation example.
What each adapter inspects
| PostgreSQL | SQL Server | |
|---|---|---|
| Effective object privileges | has_table_privilege and has_any_column_privilege, which include inherited roles and superuser |
sys.fn_my_permissions, which includes database roles, schema grants and sysadmin |
| Objects | tables, partitioned tables, views, materialized views, foreign tables in every schema you can see | tables and views of the current database |
| Elevated | SUPERUSER, CREATEROLE, CREATEDB, BYPASSRLS, REPLICATION, CREATE on the database and on schemas, pg_write_all_data, pg_write_server_files, pg_execute_server_program, pg_read_server_files |
sysadmin, securityadmin, serveradmin, bulkadmin, db_owner, db_datawriter, db_ddladmin, db_securityadmin |
| Ownership | the owning role, and whether the identity is (a member of) it | the owning principal |
| Row-level security | noted on objects that have it | not inspected |
What it cannot see
- Procedures, functions, sequences, synonyms and CLR objects.
EXECUTEon a procedure,SECURITY DEFINERfunctions and triggers can write on behalf of someone else. - Default privileges for objects created later,
SET ROLE,EXECUTE AS,IMPERSONATEand ownership chaining. - Other databases of the cluster or server. SQL Server hides objects the identity has no permission on, so they are not listed.
- It reads metadata at this moment; it does not prove that a given statement would succeed.
It is read only
The command never attempts a write. Every statement it sends is a SELECT or a session SET (an internal check refuses anything else), and on PostgreSQL the transaction is READ ONLY with a statement timeout. SQL Server has no read-only session, so there only SELECT is sent, with a lock timeout.
Output carries role, schema and object names, never the connection string or a password: connection and driver errors are reduced to the exception type.
Example (fictional database)
$ sqlguardllm doctor --db postgresql+psycopg://ana@db.example/shop --schema shop
sqlguardllm doctor (postgresql). Experimental: not covered by the 1.x stability guarantee ...
Identity: ana Database: shop
Checked scope: shop; 2 table(s) and view(s) inspected
Verdict: CONFIRMED WRITE ACCESS. This identity can change data.
Write privileges (1):
shop.invoices (table): UPDATE
What this check cannot see:
...
Remediation example:
REVOKE INSERT, UPDATE, DELETE, TRUNCATE ON ALL TABLES IN SCHEMA public FROM agent_ro;
...
$ echo $?
1
Here ana is a member of agent_writers, which has UPDATE on shop.invoices: the write comes by inheritance, not from a grant to ana.
Remediation
PostgreSQL:
REVOKE INSERT, UPDATE, DELETE, TRUNCATE ON ALL TABLES IN SCHEMA public FROM agent_ro;
REVOKE agent_writers FROM agent_ro;
ALTER ROLE agent_ro NOSUPERUSER NOCREATEROLE NOBYPASSRLS;
ALTER ROLE agent_ro SET default_transaction_read_only = on;
SQL Server:
ALTER ROLE db_datawriter DROP MEMBER [agent_ro];
DENY INSERT, UPDATE, DELETE, ALTER ON SCHEMA::dbo TO [agent_ro];
GRANT SELECT ON SCHEMA::dbo TO [agent_ro];
Run doctor again after the change. Tests against real PostgreSQL 16 and SQL Server 2022 (a read-only identity, inherited write access, inconclusive scopes) are in tests/engines/test_doctor_engines.py, run by the doctor CI job.