Principle of Least Privilege for Database Security
Fast recall
Quick revision
Core answer
Whether you understand least privilege as damage containment, not prevention.
Running the app as a DB superuser/root, so any injection is catastrophic.
Frame it as defense-in-depth that shrinks the blast radius of an injection.
Interview-ready guide
Interview answer
The order service may need SELECT/INSERT/UPDATE on orders and order_items but not ALTER TABLE or access to payroll. Flyway uses a separate deployment credential with schema-change privileges. If a concatenation bug allows injected SQL, the runtime role still cannot drop the schema or read unrelated tables. The original query remains vulnerable, so parameterization is still mandatory.
Principle of Least Privilege for Database Security
Direct Answer
Least privilege means each database identity receives only the operations and objects needed for its job. A runtime application account should not normally own the schema or have DROP/ALTER privileges; a read service may need SELECT only on specific views. This does not prevent SQL injection, but it limits what an injection or application bug can read or change.
Code Example
-- create a dedicated app account with minimal rights
CREATE USER app_user WITH PASSWORD 'secret';
GRANT SELECT, INSERT, UPDATE ON orders, users TO app_user;
-- deliberately NOT granted: DROP, ALTER, DELETE on others, superuser
-- a separate read-only account for reporting:
CREATE USER report_user WITH PASSWORD 'secret';
GRANT SELECT ON sales_summary TO report_user;
Even if an attacker injects, they can only perform actions these grants permit.
Common Mistakes & Pitfalls
- Running the app as root/superuser: any injection becomes catastrophic.
- One all-powerful account for every service: violates least privilege.
- Granting
DROP/ALTERthe app never needs. - Treating it as injection prevention: it's damage limitation, not a substitute for parameterization.
Check your understanding
Answer these without looking back at the article.
- What is SQL injection and how does it work?
- How does PreparedStatement prevent SQL injection in JDBC?
- What are other SQL injection prevention techniques besides PreparedStatement?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.