Templates/SaaS & accounts

Roles & permissions database schema

Role-based access control: roles, permissions and who holds them.

5 tables, 5 relationships

Opens as a new diagram you can change. Free.

Tables

users

  • iduuid not null
  • emailtext not null
  • nametext
  • created_attimestamptz not null

roles

  • iduuid not null
  • nametext not null
  • descriptiontext
  • created_attimestamptz not null

permissions

  • iduuid not null
  • keytext not null
  • descriptiontext
  • created_attimestamptz not null

role_permissions

  • iduuid not null
  • role_id> rolesuuid not null
  • permission_id> permissionsuuid not null
  • created_attimestamptz not null

user_roles

  • iduuid not null
  • user_id> usersuuid not null
  • role_id> rolesuuid not null
  • granted_by_id> usersuuid
  • created_attimestamptz not null

As PostgreSQL

The same diagram exports to MySQL, MariaDB, SQLite, SQL Server and Oracle, and to Prisma, Drizzle, SQLAlchemy and Convex models.

CREATE TABLE "permissions" (
  "id" UUID DEFAULT gen_random_uuid() NOT NULL,
  "key" TEXT NOT NULL UNIQUE,
  "description" TEXT,
  "created_at" TIMESTAMPTZ DEFAULT now() NOT NULL,
  PRIMARY KEY ("id")
);

CREATE TABLE "role_permissions" (
  "id" UUID DEFAULT gen_random_uuid() NOT NULL,
  "role_id" UUID NOT NULL,
  "permission_id" UUID NOT NULL,
  "created_at" TIMESTAMPTZ DEFAULT now() NOT NULL,
  PRIMARY KEY ("id")
);

CREATE TABLE "roles" (
  "id" UUID DEFAULT gen_random_uuid() NOT NULL,
  "name" TEXT NOT NULL UNIQUE,
  "description" TEXT,
  "created_at" TIMESTAMPTZ DEFAULT now() NOT NULL,
  PRIMARY KEY ("id")
);

CREATE TABLE "user_roles" (
  "id" UUID DEFAULT gen_random_uuid() NOT NULL,
  "user_id" UUID NOT NULL,
  "role_id" UUID NOT NULL,
  "granted_by_id" UUID,
  "created_at" TIMESTAMPTZ DEFAULT now() NOT NULL,
  PRIMARY KEY ("id")
);

CREATE TABLE "users" (
  "id" UUID DEFAULT gen_random_uuid() NOT NULL,
  "email" TEXT NOT NULL UNIQUE,
  "name" TEXT,
  "created_at" TIMESTAMPTZ DEFAULT now() NOT NULL,
  PRIMARY KEY ("id")
);

ALTER TABLE "role_permissions" ADD CONSTRAINT "fk_role_permissions_role_id" FOREIGN KEY ("role_id") REFERENCES "roles" ("id") ON DELETE CASCADE ON UPDATE NO ACTION;

ALTER TABLE "role_permissions" ADD CONSTRAINT "fk_role_permissions_permission_id" FOREIGN KEY ("permission_id") REFERENCES "permissions" ("id") ON DELETE CASCADE ON UPDATE NO ACTION;

ALTER TABLE "user_roles" ADD CONSTRAINT "fk_user_roles_user_id" FOREIGN KEY ("user_id") REFERENCES "users" ("id") ON DELETE CASCADE ON UPDATE NO ACTION;

ALTER TABLE "user_roles" ADD CONSTRAINT "fk_user_roles_role_id" FOREIGN KEY ("role_id") REFERENCES "roles" ("id") ON DELETE CASCADE ON UPDATE NO ACTION;

ALTER TABLE "user_roles" ADD CONSTRAINT "fk_user_roles_granted_by_id" FOREIGN KEY ("granted_by_id") REFERENCES "users" ("id") ON DELETE SET NULL ON UPDATE NO ACTION;

More SaaS & accounts templates

Start with one diagram, free

One diagram with two branches, no card needed. Upgrade when your team needs more.