keepcalculator

06 Numbers and Powers

AI-Generated Database Queries: Read-Only Access

Card No. 06·29487 · Updated · Standard formula, shown below

Giving AI-generated database queries read-only access lets a tool answer questions from your data without being able to change or remove records. The safest approach is to create a dedicated database account with narrowly scoped read permissions, then ensure the application uses that account for generated queries.

What You’ll Need

  • Permission to create a database user or ask a database administrator to do so.
  • The database engine and the specific databases, schemas, tables, or views the tool needs to read.
  • A way to configure the tool’s database connection, preferably using a secret manager or protected environment variable.
  • A non-production environment or test database for checking permissions before rollout.

Step-by-Step

  1. Identify exactly what the tool needs to read. List the data sources required for its intended questions. Prefer a small set of reporting views or tables over granting access to an entire database. Exclude sensitive columns and unrelated schemas. If possible, have an administrator prepare views that expose only approved fields and rows.
  2. Create a separate account for the query feature. Do not reuse an application administrator, developer, or personal account. Create a dedicated database login used only by the AI query service. Give it a clear name, and keep its credentials separate from the accounts used for migrations, writes, or routine application operations.
  3. Grant only read permissions. Grant the account the minimum permissions required to connect and read the approved objects. In PostgreSQL, for example, this commonly involves allowing connection to the database, usage of the relevant schema, and SELECT on chosen tables or views. In MySQL, grant SELECT on the chosen database or tables. Exact commands and permission details vary by engine and version; use that engine’s documentation or have a DBA review the statements. Do not grant INSERT, UPDATE, DELETE, CREATE, ALTER, DROP, ownership, or administrator privileges.
  4. Set safe defaults where available. Configure the account so new objects do not automatically become readable unless that is intended. In PostgreSQL, for instance, table permissions do not necessarily cover future tables; default privileges may need separate configuration. Restrict access to the required database and schemas, and consider row-level security or approved views if users should see only some records. Check that the account cannot switch to a more privileged role.
  5. Use the read-only account in the query service. Configure the application or tool’s database connection to use the dedicated credentials. Store the password in a secret manager or protected environment configuration, not in prompts, source code, logs, or generated SQL. Keep write-capable credentials out of the process that executes AI-generated statements.
  6. Enforce read-only behavior at more than one layer. Database permissions are the primary control. Also configure the query runner to accept only one statement at a time and, where suitable, allow only read queries such as SELECT. Do not rely on checking whether a query begins with the word SELECT: comments, multiple statements, stored procedures, functions, and engine-specific syntax can make simple checks unreliable. Use a proper SQL parser or a constrained query interface if application-level validation is needed.
  7. Test both permitted and forbidden actions. Connect using the exact credentials the service will use. Confirm that a SELECT from an approved object succeeds. Then attempt representative writes, schema changes, and reads from an unapproved object; they should fail. Test stored procedures and views as relevant, because their behavior can depend on ownership and execution privileges. Verify the result with a database administrator rather than assuming the connection setting is effective.
  8. Roll out, monitor, and review. Start with a small group or limited dataset. Log query metadata and failures without recording secrets or unnecessary sensitive results. Review access periodically, remove permissions and credentials that are no longer needed, and rotate credentials if they are exposed. Keep the database account read-only even if the tool’s behavior or prompt instructions change.

Common Mistakes

  • Relying on prompts to prevent writes. Instructions such as “never modify data” do not enforce database permissions. The database must reject unauthorized changes.
  • Using a powerful existing account. A connection account with application-admin or migration privileges can turn a faulty or manipulated query into a destructive operation.
  • Granting access too broadly. Database-wide SELECT permission may expose sensitive or irrelevant data. Prefer specific views, tables, and columns.
  • Assuming SELECT always means harmless. Some database features can have side effects, and some functions or procedures may perform actions. Restrict executable routines and review security-definer behavior where the engine supports it.
  • Testing only the user interface. A tool may appear read-only while its database credentials still have write access. Test permissions directly with the service account.
  • Forgetting new objects and inherited roles. Default grants and role membership can expand access over time. Review both direct grants and inherited privileges.

Tips

  • Use read-only reporting views to hide columns the query feature does not need.
  • Set query timeouts and result-size limits to reduce the impact of expensive or accidental broad queries.
  • Use a read replica for reporting workloads when appropriate, while still applying least-privilege permissions.
  • Keep query execution separate from any component that can modify data. If a workflow needs both reading and writing, use separate credentials and explicit, reviewed operations.
  • Document the purpose, owner, objects, and review date for each dedicated account.

Troubleshooting

Approved queries fail with permission errors: Check that the account can connect to the database and has the required schema and object permissions. Confirm the query references the correct schema and that any views, sequences, or functions it uses have the necessary access. Avoid solving this by granting broad privileges; identify the missing permission precisely.

A supposedly read-only query is rejected: Inspect the generated SQL for multiple statements, unsupported syntax, or a function the query runner blocks. Test a simple SELECT on an approved table, then add complexity gradually. If the database itself rejects the query, check the account’s role and grants.

A write unexpectedly succeeds: Stop the service from using that connection, investigate which credentials and roles were active, and have a DBA revoke the write permissions. Check role inheritance, ownership, procedures, and whether the application connected to a different database or account than expected. Re-test using the exact production configuration before restoring access.

New tables are inaccessible: Existing grants may not apply to objects created later. Ask the administrator to configure appropriate default privileges, or explicitly grant access to approved new objects. Do not automatically expose every future table unless that is a deliberate policy.

Frequently Asked Questions

Does a read-only database role guarantee zero risk?
It prevents ordinary writes covered by the database’s permission system, but does not eliminate risks such as sensitive data exposure, expensive queries, or side-effecting routines. Limit data and monitor usage.

Can the AI generate SQL if it cannot execute writes?
Yes. It can generate queries, while the database account determines which operations actually succeed. You can also restrict the query runner to approved read-query syntax.

Should I use a read replica?
A replica can isolate reporting traffic from the primary database, but it is not a replacement for limited permissions. Apply access controls to the replica too.

Is filtering SQL for the word SELECT enough?
No. SQL syntax varies, and statements can include multiple commands or call routines. Enforce permissions in the database and use a parser or constrained interface for additional validation.

What if the tool needs to update records as well?
Keep its read connection read-only. Handle required changes through a separate, narrowly scoped workflow with explicit authorization and validation; do not upgrade the query account for convenience.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.

More cards in this drawer

The whole Numbers and Powers drawer →