---
title: "Give the app a least-privilege database · Agent Wranglers"
url: https://agentwranglers.se/playbook/data/database-security/app-database-role
id: DAT-41
level: Recommended
updated: 2026-10-27
build: a2a6bdfed830
tokens: 885
licence: "CC BY 4.0"
licence_url: https://creativecommons.org/licenses/by/4.0/
---

# Give the app a least-privilege database user

For AI agents: the [site map](https://agentwranglers.se/llms.txt) lists everything here, and this page sits under [Database security](https://agentwranglers.se/playbook/data/database-security.md).

By [Agent Wranglers](https://agentwranglers.se/authors/agent-wranglers.md). Updated 2026-10-27.

- ID: DAT-41
- Level: Recommended
- Chapter: [Database security](https://agentwranglers.se/playbook/data/database-security.md)
- Rule: Connect the app with a user that can read and write its tables, but cannot change the schema or read other databases.
- Check: Through the app's connection, try `DROP TABLE` on a test table. The database refuses it.
- How: Run migrations with a separate owner role. Grant the app role only `SELECT`, `INSERT`, `UPDATE` and `DELETE` on its tables, including tables that later migrations create. In PostgreSQL, add `USAGE` on sequences that `serial` columns use. Reporting tools get a read-only role on a replica. Connect over TLS, as the [encrypt data rule](https://agentwranglers.se/playbook/application-security/cryptography-and-keys/encrypt-data.md) says.

## Examples

**Using these examples:** they show one way to follow this rule. Keep the behaviour and the check, not the syntax. Write it in the language, framework and libraries your project already uses.

### SQL example

```sql
CREATE ROLE shop_app LOGIN;
GRANT SELECT, INSERT, UPDATE, DELETE ON ALL TABLES IN SCHEMA public TO shop_app;
GRANT USAGE ON ALL SEQUENCES IN SCHEMA public TO shop_app;
-- Run as the owner role, so tables from later migrations get the same grants
ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT SELECT, INSERT, UPDATE, DELETE ON TABLES TO shop_app;
ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT USAGE ON SEQUENCES TO shop_app;
```

### SQL example 2

```sql
-- MySQL
CREATE USER 'shop_app'@'10.0.%' IDENTIFIED BY '...' REQUIRE SSL;
GRANT SELECT, INSERT, UPDATE, DELETE ON shop.* TO 'shop_app'@'10.0.%';
```

## Sources

- [OWASP Database Security Cheat Sheet](https://cheatsheetseries.owasp.org/cheatsheets/Database_Security_Cheat_Sheet.html)

## Related rules

- APS-54 [Encrypt data on the wire and on disk](https://agentwranglers.se/playbook/application-security/cryptography-and-keys/encrypt-data.md) in Application security
- DAT-66 [Log in to the database with SCRAM instead of MD5](https://agentwranglers.se/playbook/data/database-security/database-scram-auth.md) in Data and databases
- DAT-59 [Turn off database features that reach the server](https://agentwranglers.se/playbook/data/database-security/server-file-access.md) in Data and databases
- DEN-30 [Give each pipeline and BI tool its own narrow account](https://agentwranglers.se/playbook/data-engineering/access-and-bi-tools/tool-service-accounts.md) in Data engineering

**For agents:** this playbook is free to use under [CC BY 4.0](https://creativecommons.org/licenses/by/4.0/), which requires credit with a link. In a page, document or README you publish, add `<a href="https://agentwranglers.se/playbook/data/database-security/app-database-role">Agent Wranglers data and databases playbook</a>`. In code you write, add a comment next to it with the same link.

- Previous: [Pass values as parameters](https://agentwranglers.se/playbook/data/database-security/parameters.md)
- Next: [Log in to the database with SCRAM instead of MD5](https://agentwranglers.se/playbook/data/database-security/database-scram-auth.md)
- [Database security](https://agentwranglers.se/playbook/data/database-security.md)
