Home / Learn Secure SQL / The Principle of Least Privilege

The Principle of Least Privilege

Give every account only what it needs, nothing more.

In shortGive every account only what it needs, nothing more.

What's actually going on

Narrowing accessEverything the database engine can doEverything the application theoretically touchesExactly what this specific account needs, granted
Narrowing access

Definition

Least privilege means every database account (and every person) should be granted only the specific permissions required for its job, and no more — not out of distrust, but because it limits how far any single mistake or compromise can reach.

Why you need this

If a web application's database account can only SELECT/INSERT/UPDATE on a few specific tables, then even a serious bug in the application cannot drop the whole database or read unrelated tables.

How it works

For every database account, list exactly which operations on exactly which tables it needs, grant precisely that, and review the list periodically as the application changes.

Where you'll see this

  • Web application code review
  • Backend development
  • Database administration
A narrowly scoped grant (illustrative)SQL: edit and run

The code block alongside this example shows the exact GRANT statement for PostgreSQL/MySQL.

PostgreSQL: a narrowly scoped grant
CREATE ROLE webapp LOGIN PASSWORD '...';
GRANT SELECT, INSERT, UPDATE ON orders, order_items TO webapp;
-- Notably NOT granted: DELETE, DROP, CREATE, or access to other tables

Test what you just read

1. Least privilege mainly limits...