Database role

On this page 7

A database role is a named collection of privileges that a database administrator grants to users, applications or other roles, so that permissions are managed per function rather than per person. In database security, roles are the main building block of role-based access control (RBAC): privileges are attached to a role such as reporting_reader, and every account that needs that access simply receives the role.

The SQL standard has defined roles since SQL:1999, and all major engines implement them, including PostgreSQL, Oracle Database, Microsoft SQL Server, MySQL 8.0 and MariaDB. The details differ considerably, so the same word can mean slightly different things from one product to another.

How a database role controls access

Access control in a relational database rests on three elements: a principal that requests access, a securable object such as a table, view, schema or function, and a privilege such as SELECT, INSERT, EXECUTE or CREATE. A role sits between principals and privileges. Privileges are granted to the role with GRANT SELECT ON sales.orders TO reporting_reader, and the role is granted to users with GRANT reporting_reader TO alice.

Roles can be nested. A role analyst may include reporting_reader plus write access to a sandbox schema, and a user who receives analyst inherits both sets of privileges. This hierarchy keeps definitions small and lets administrators change access for hundreds of accounts by editing one role.

Revocation follows the same path. Removing a privilege from a role takes effect for every member at once, and removing a user from a role withdraws all privileges that came through it. Privileges granted directly to individual accounts bypass this structure, which is why direct grants are a common audit finding.

Types of roles across database engines

Engines distinguish between built-in roles and roles that administrators define, and some add roles that belong to applications. The following table summarizes the main categories.

Role typeExamplesScopeTypical purpose
Predefined server rolessysadmin in SQL Server, pg_read_all_data in PostgreSQLWhole instanceAdministration and broad system tasks
Predefined database rolesdb_datareader, db_owner in SQL ServerOne databaseCommon access patterns out of the box
User-defined rolesreporting_reader, billing_writerObjects chosen by the administratorLeast-privilege access per job function
Application rolesSQL Server application rolesOne database, activated by the applicationAccess only through a trusted application
Secure application rolesOracle roles enabled by a PL/SQL procedureSession, after a security checkConditional activation, for example by network
Main categories of database roles

Predefined roles are convenient but often broader than necessary. Membership in db_owner or sysadmin, for example, grants control far beyond reading and writing data, so security reviews treat these roles as privileged accounts.

Products draw the line between users and roles differently. In PostgreSQL, users and roles are the same object type: a user is simply a role with the LOGIN attribute. In SQL Server and Oracle, logins or users and roles are distinct objects, and roles cannot log in.

Activation also differs. PostgreSQL roles marked INHERIT grant their privileges automatically, while NOINHERIT requires SET ROLE. In MySQL 8.0, granted roles are inactive until they are activated with SET ROLE or configured as default roles, which surprises many administrators after an upgrade.

A role works at the level of objects and operations. Neighboring mechanisms in database security restrict data differently, as the table shows.

ControlWhat it restrictsGranularityExample
Database roleWhich operations on which objectsDatabase, schema, table, columnRead access to the sales schema
Row-level securityWhich rows a query returnsIndividual rowsSales staff see only their region
Database encryptionReadability of stored or transmitted dataFiles, columns or connectionsEncrypted card numbers
Database auditNothing, it records activityStatements and sessionsLog of all privilege changes
Database role compared with related controls

Designing a role model

A good role model applies the principle of least privilege and reflects real job functions, not individual people. Practitioners commonly separate functional roles that hold privileges from membership roles that represent teams, which keeps the structure readable as the organization changes.

The following decision table shows recommended patterns for frequent requirements.

RequirementRecommended patternReason
Analysts need read accessRead-only role on reporting schemas or viewsPrevents accidental changes to data
Application service accountDedicated role with DML only, no DDLLimits damage from SQL injection
Schema migrationsSeparate migration role used only by deploymentsKeeps structural changes out of runtime
Temporary elevated accessTime-limited role membership with reviewAvoids permanent privilege creep
Segregation of dutiesMutually exclusive roles for approval and executionSupports audit and compliance rules
Role design decisions

For new tables to inherit the right permissions automatically, engines provide default privilege mechanisms, such as ALTER DEFAULT PRIVILEGES in PostgreSQL. Without them, newly created objects are often invisible to the reader role until someone grants access manually.

Common mistakes with database roles

Most role problems arise gradually over years rather than through one wrong decision. The typical failure modes are these:

  • Privilege creep: users collect roles when they change jobs and never lose the old ones.
  • Shared administrator accounts, which make it impossible to attribute actions in an audit.
  • Applications connecting as the owner of the schema, so a single injection flaw can drop tables.
  • Overuse of the PUBLIC pseudo-role, which grants privileges to every account on the database.
  • No periodic access review, so nobody notices that a former contractor still holds a write role.

Regular access recertification, typically quarterly or semiannually in regulated environments, and audit logging of every GRANT and REVOKE are the standard countermeasures.

A non-technical view

A database role works like a job badge in an office building. Instead of programming every door for every employee, the building assigns a badge type such as “accounting” or “visitor”, and each badge type opens certain doors.

When someone joins the accounting team, they get the accounting badge and immediately have the right access. When they leave, the badge is taken back. Managing a few badge types is far simpler and safer than tracking thousands of individual door permissions.

A concrete example

A regional insurance company runs its claims system on PostgreSQL with about 120 database accounts: employees, reporting tools and three applications. An internal audit finds that 38 accounts hold direct table grants and that the claims application connects as the schema owner.

The database team defines five functional roles: claims_reader, claims_writer, claims_migrator, finance_reporting and dba_admin. Direct grants are revoked, accounts receive only the roles their job requires, and the application now connects with claims_writer, which cannot alter or drop tables. Default privileges ensure that new tables become readable to the reporting role automatically.

At the next audit, the access review takes a few hours instead of several days, because reviewers check five role definitions and a membership list rather than hundreds of individual grants.