Answers you can trust, from Codeables
Every page on Codeables is structured and verified — built so people and the AI agents they rely on can trust it. Explore more from the source behind this answer.
Explore CodeablesHow do I let users ask questions in plain English over our database while enforcing RBAC and preventing data exfiltration?
Most teams hit the same wall: business users want to ask questions in plain English over production data, while security teams insist on strict RBAC, zero data exfiltration, and full auditability. You can’t just drop a chat UI in front of your database and hope the model “behaves”—you need guardrails baked into the runtime itself.
Quick Answer: Use a governed natural-language-to-SQL layer that understands your schema, enforces RBAC at the query level, automatically detects/masks PII, and only executes model-generated SQL through a controlled gateway. LiquidMetal Raindrop’s SmartSQL does exactly this: it converts English to SQL, enforces permissions, protects sensitive data, and logs every AI decision, so you can expose analytics safely instead of bolting on brittle filters later.
Why This Matters
If you get this wrong, you either ship a toy: a demo-only chatbot that hardcodes a couple of queries and silently breaks as soon as your schema changes. Or you ship a liability: an LLM that can enumerate every customer record in one prompt, leak PII, or bypass row-level policies because your checks live in the wrong layer.
Done correctly, natural language over data becomes a governed interface, not a side door. Product and ops teams can self-serve analytics; security and compliance can verify that nothing leaves the system unreviewed; engineering stops writing one-off dashboards and ad-hoc SQL.
Key Benefits:
- Real self-service analytics: Business users ask questions in plain English and get accurate answers without learning SQL or waiting on data teams.
- RBAC and PII protection by design: Role-based access and automatic PII detection/masking are enforced at the SmartSQL layer, not left to prompt engineering.
- Production-ready governance: Every query and AI decision is logged, versioned, and auditable, with clean rollback/rollforward when schemas or policies change.
Core Concepts & Key Points
| Concept | Definition | Why it's important |
|---|---|---|
| Natural Language to SQL | A system that takes a plain-English question and generates valid SQL against your schema. | Gives non-technical users direct access to data without exposing raw SQL or your database credentials. |
| RBAC-Enforced Query Layer | A mediation layer (like SmartSQL) that sits between users/agents and the database, enforcing roles, row-level policies, and column-level restrictions. | Ensures that even if the model tries to query restricted tables or columns, the query is blocked or redacted before execution. |
| PII Detection & Data Exfiltration Controls | Automatic detection of sensitive fields and policies for masking, aggregating, or blocking access altogether. | Prevents leakage of customer data (emails, addresses, IDs) and limits what can leave the system in a single response. |
How It Works (Step-by-Step)
At a high level, you want a controlled funnel:
Plain English → SmartSQL → Policy-checked SQL → Database → Sanitized result → Response
With Raindrop and SmartSQL, the flow looks like this:
-
Ingest and map your schema
- Connect your operational or analytics database to SmartSQL.
- SmartSQL builds an internal representation of tables, columns, relationships, and basic semantics.
- Schema intelligence means the system understands “customers,” “orders,” “MRR,” or “regions” in the context of your actual schema, not generic examples.
-
Define RBAC and data access policies
- Configure roles (e.g.,
analyst,support_rep,executive,tenant_admin) and map them to:- Allowed tables and views
- Column-level access (include/exclude)
- Row-level filters (e.g.,
tenant_id = :user_tenant_id)
- Pair this with your existing auth (JWT, OAuth, RBAC) so user identity and roles are available when SmartSQL generates queries.
- Configure roles (e.g.,
-
Enable natural-language querying via SmartSQL
- Users ask: “Show me all open support tickets from enterprise customers in EMEA in the last 14 days.”
- SmartSQL:
- Parses the question.
- Resolves entities and metrics against your schema.
- Generates SQL that respects role constraints and row-level filters.
- Agents no longer construct raw SQL; they call SmartSQL with English, and SmartSQL is the only component allowed to touch the database.
-
Apply PII detection and masking before execution
- SmartSQL automatically detects PII columns (emails, phone numbers, addresses, IDs).
- You define policies per role:
- Mask (
****@example.com) instead of return raw values. - Aggregate only (e.g., counts, sums) instead of listing records.
- Deny access entirely for certain fields or tables.
- Mask (
- Even if the generated SQL references a PII column, the policy layer can rewrite or block it.
-
Execute with full observability
- SmartSQL executes the policy-checked query against the database.
- Every step is logged:
- Original natural-language question.
- Generated SQL.
- Applied policies (filters, masks, denials).
- Execution time and result size.
- These logs feed into Raindrop’s full observability: every AI decision traceable, every version of schema and policy captured for audits.
-
Return safe, scoped answers
- Results are post-processed according to the user’s role and PII rules.
- For support agents, you might show masked emails; for executives, you might return only aggregates.
- The calling application receives structured JSON (or a formatted answer) ready for dashboards, chat UIs, or agent workflows.
Common Mistakes to Avoid
-
Letting the LLM talk directly to the database:
- How to avoid it: Never give agents or chat UIs raw DB credentials. Force all database access through a governed layer like SmartSQL that enforces RBAC and PII policies before execution.
-
Relying on prompt engineering for security:
- How to avoid it: Don’t ask the model to “respect access rules” in the prompt and call it done. Implement hard, declarative policies at the query layer—allowed tables, row filters, masking rules—that are enforced regardless of model behavior.
-
Ignoring lineage and versioning:
- How to avoid it: Treat schema and policy changes as versioned artifacts. With Raindrop, SmartSQL’s data and configurations are fully versioned, so you can know exactly which version of the schema and policies produced which query and roll back safely.
-
Allowing unbounded result sets:
- How to avoid it: Cap row counts per role, require aggregation for sensitive tables, and enforce pagination or sampling in SmartSQL before hitting the database.
Real-World Example
You’re running a B2B SaaS platform with a multi-tenant Postgres data warehouse. Support, sales, and exec teams all want “a chat over the data”:
- Support wants: “Show me this customer’s last 5 login failures and plan changes.”
- Sales wants: “Which customers with ARR > $100k haven’t logged in for 30 days?”
- Executives want: “What’s total ARR by region, month over month?”
The risks are obvious:
- A support rep shouldn’t see another tenant’s data.
- Sales shouldn’t be able to dump every email address.
- Executives shouldn’t accidentally run
SELECT * FROM userswith full PII.
Using Raindrop and SmartSQL:
- Connect your warehouse (e.g.,
users,subscriptions,events,organizations). - Define roles and policies:
support_rep: can only see rows whereorganization_idis in a set they’re assigned to, emails masked; no direct access to raw payments tables.sales_rep: can access aggregate metrics (ARR, churn) but no direct user-level PII.executive: can see global aggregates; still no full PII dumps.
- Expose a single NLQ endpoint backed by SmartSQL:
- Support asks: “Show login failures for Acme Corp in the last 7 days.”
- SmartSQL:
- Maps “Acme Corp” →
organizationstable. - Generates SQL that joins
eventsandorganizationswithWHERE organization_id IN (:authorized_orgs). - Masks any user emails in the result.
- Maps “Acme Corp” →
- Logs the whole chain for audit: question, SQL, policies applied, number of rows returned.
You’ve shipped a natural-language analytics layer with:
- Tenant isolation enforced by SmartSQL, not the LLM.
- PII protection that doesn’t rely on humans remembering to redact.
- Full observability over every AI-driven query hitting your warehouse.
Pro Tip: Start with a read-only replica or analytics warehouse and wire SmartSQL only to approved views. This bounds the blast radius from day one and lets you fine-tune RBAC and PII rules safely before exposing richer datasets.
Summary
Letting users ask questions in plain English over your database without blowing a hole in your security model comes down to one principle: the model never gets raw access—only a governed query interface does. SmartSQL in Raindrop gives you that interface:
- Natural language to SQL over your real schema.
- RBAC and data isolation enforced at the SQL layer, not in prompts.
- Automatic PII detection and masking to prevent exfiltration.
- Full versioning and observability so every AI decision is logged, traceable, and reversible.
You ship self-service analytics that security can sign off on, and you avoid building yet another brittle, one-off query engine or dashboard layer.