Users & Roles

Your application needs a database user that can read the products table but not change it. With Users & Roles, you design such users, their roles and their rights in a permission plan. DbSchema then compares the plan with the database. You review the CREATE ROLE and GRANT statements before they run.

The Users & Roles dialog with the plan production: the user report_app, member of the role shop_reader, with SELECT on shop.orders, and the migration script below

In the current build, DbSchema writes these statements for PostgreSQL and MongoDB. For SQL Server, Oracle, MySQL and the other databases, DbSchema doesn't write them yet. Databases that support it gives the details.

Users, roles and grants

Users & Roles works with these words:

  • A user is an account that logs in to the database, such as the account of your application.
  • A role is a group of rights with a name, such as shop_reader. When you give a role to a user, the user gets the rights of the role. The user is then a member of the role.
  • A grant is one right on one object, such as SELECT on the table shop.products. To revoke a grant means to take it away.
  • A permission plan defines users, roles, their memberships and their grants. DbSchema saves each plan in a file.

The work has these steps:

  1. Create a permission plan.
  2. Add roles, and their grants.
  3. Add users, and give them roles and grants.
  4. Synchronize the plan. DbSchema compares it with the database and shows the statements.
  5. Run the statements in the database.

Editing a plan changes only the plan file, and needs no connection. The database changes only in step 5.

Create a permission plan

  1. Choose Database → Users & Roles.
  2. Press the + button beside Plan.
  3. Type a name, such as production, and press OK.
The Plan bar of the Users & Roles dialog: the plan production, the buttons to add, rename and remove a plan, and Synchronize...

DbSchema saves the plan immediately, and saves every change after that. The pencil button renames the plan. The bin button removes it, and deletes its file.

You can keep several plans for one database type, such as production for the live database and staging for a test copy. The Plan list shows only the plans for the database type of the open model.

The plan is not part of the model, the .dbs file that holds your tables and diagrams. DbSchema saves each plan in a separate file, in the permissions folder inside its configuration folder. By default, the configuration folder is .DbSchema/config in your home folder. Edit → Settings → File Locations → Configuration Folder changes it. The file is named after the database type and the plan, such as PostgreSQL.production.dbp. You can share the plan file, or keep it in Git like any other file.

Add a role and its grants

  1. Press the arrow beside the + button under the list on the left, and choose Role.
  2. Type the name of the role, such as shop_reader, and press OK.
  3. Press Add Grant..., choose the kind of object, such as Table, then the object, such as shop.products.
  4. In the Grants table, tick the rights that the role gets, such as SELECT.
The role shop_reader in the Users & Roles dialog: USAGE on the schema shop, and SELECT on the tables shop.categories and shop.products

Add Grant... lists the objects in the open model, grouped by type: schemas, tables, views, sequences and more. To grant a right on an object that is not in the model, choose Other... and enter its name. In PostgreSQL, a role needs USAGE on a schema before it can read the tables in it. So the role above also has a grant on the schema shop. A role can be a member of another role too: press Add Role... while the role is selected.

The Migration script box at the bottom shows the statements that create every user, role and grant of the plan:

CREATE ROLE shop_reader NOLOGIN;

GRANT USAGE ON SCHEMA shop TO shop_reader;

GRANT SELECT ON shop.categories TO shop_reader;

GRANT SELECT ON shop.products TO shop_reader;

Add a user

  1. Press the + button under the list on the left. It adds a user.
  2. Type the name of the user, such as report_app, and press OK.
  3. To give the user a role, press Add Role... and choose the role, such as shop_reader.
  4. To give a right directly to the user, press Add Grant..., just as you would for a role.
The user report_app in the Users & Roles dialog: Kind User, Member of shop_reader, and SELECT on shop.orders

The user report_app now has the rights of shop_reader, and SELECT on shop.orders. In PostgreSQL, DbSchema creates a user with CREATE ROLE … LOGIN. LOGIN means that the account can log in. It creates a role with CREATE ROLE … NOLOGIN. Use Options to add SQL to the end of that statement, such as VALID UNTIL '2027-01-01'.

DbSchema doesn't store passwords in the plan. In PostgreSQL, it creates the user without a password. After the synchronization, set the password in the SQL Editor. This keeps the password out of the plan file:

ALTER ROLE report_app PASSWORD 'choose-a-password';

Synchronize the plan with the database

Before you run the statements, check what the synchronization changes in the database:

  • It creates the users and roles of the plan that the database doesn't have.
  • For each user and role in the plan, it adds the grants and role memberships that the plan lists.
  • For each user and role in the plan, it also revokes the grants and role memberships that the plan doesn't list. DbSchema reads the grants only on the schemas of the model, so grants on other schemas stay as they are.
  • It never drops a user or a role.

A user or role that is not in the plan never shows in the comparison. The synchronization never changes it. This also applies to users and roles that you remove from the plan. They remain in the database. PostgreSQL's own roles, such as postgres and the roles that start with pg_, are never compared either.

  1. In Users & Roles, choose the plan and press Synchronize....
  2. If DbSchema asks for a connection, choose the database.
  3. In Compare the permission plan with the database, select an action for each row, or set every row to Database.
  4. Select a row to preview the SQL statements of its action.
  5. Press Commit in the database.
  6. In the Execute Script In Database Dialog, press Execute All, then OK.
Compare the permission plan with the database: report_app and shop_reader present in the plan and absent from the database, both set to Create, and the statements CREATE ROLE report_app LOGIN, GRANT shop_reader TO report_app and GRANT SELECT ON shop.orders TO report_app

Each row is a user or a role. Present means that the user or role exists on that side. Absent means that it is missing there. Set every row to Database makes the database match the plan.

ActionWhat it does
CreatePoints to Database: runs the statements in the database
DropPoints to Permission Plan: changes only the plan, never the database
IgnoreLeaves the row out

Generate SQL script writes the statements to an SQL Editor without running them. After the commit, synchronize again: DbSchema answers The permission plan and the database are identical.

Databases that support it

DbSchema keeps the statements of Users & Roles in the settings of each database type. Users & Roles works only where those settings hold them. The PostgreSQL row was checked against a running database. The other rows describe the statements that DbSchema holds for those databases.

DatabaseWhat DbSchema does
PostgreSQLCreates users and roles, adds and revokes grants and memberships, and compares with the existing ones
Databases that DbSchema treats as PostgreSQL, such as TimescaleDB, Greenplum and SupabaseThe same statements as PostgreSQL
MongoDBCreates users and roles in an authentication database, gives roles to users, and grants actions on collections. The createUser statement asks for the password with passwordPrompt(). DbSchema doesn't read the existing MongoDB users, so every user of the plan shows as missing.
SQL Server, Oracle, MySQL and the other databasesNone yet: the synchronization has no statements to run

Common problems

I removed a user from the plan, but it is still in the database

Removing a user or a role from the plan never drops it in the database. To drop it, run DROP ROLE in the SQL Editor.

The synchronization wants to revoke a grant that I gave by hand

The user or role is in the plan, and the plan doesn't list that grant. Add the grant to the plan, or set the row to Ignore.

The new user cannot log in

DbSchema creates the user without a password. Set one with ALTER ROLE … PASSWORD, as Add a user shows.

Next steps