/supabase-tutorials

How to set custom user roles in Supabase?

Step-by-step guide to create custom user roles in Supabase. Learn to set up your project, grant permissions, enforce policies, and enable row-level security.

Matt Graham, CEO of Rapid Developers

Book a call with an Expert

Starting a new venture? Need to upgrade your web app? RapidDev builds application with your growth in mind.

Book a free No-Code consultation

How to set custom user roles in Supabase?

 

Step 1: Set Up Your Supabase Project

 

First, ensure you have a Supabase account and have created a new project. Visit the Supabase website, log in or sign up, and create a new project if you haven't already done so.

 

Step 2: Access Supabase SQL Editor

 

Once your project is set up, navigate to the SQL Editor within the Supabase dashboard. This is where you'll execute the SQL commands to create custom user roles.

 

Step 3: Create a New Role

 

Execute the following SQL command in the SQL Editor to create a new role:

create role new_role;

Replace new_role with your desired role name. This command initializes a new role in your Supabase database.

 

Step 4: Grant Permissions to the New Role

 

Assign specific permissions to your newly created role using SQL commands or SQL policy functions. For example, to grant full permissions on a specific table:

grant all on table your_table to new_role;

Replace your_table with the name of your table where the role should have permissions. Modify the permissions (all,select, insert, etc.) based on your requirements.

 

Step 5: Create a Policy to Enforce Role Restrictions

 

You can create row-level security policies to enforce role-specific restrictions:

create policy "Role-based access to table" 
on your_table 
for select 
using (auth.role() = 'new_role');

This policy allows users with the new_role to select data from your_table. Define separate policies for insert, update, and delete as needed.

 

Step 6: Enable Row Level Security (RLS)

 

Enable row level security on the table to ensure that the role-based policies are enforced:

alter table your_table enable row level security;

RLS must be enabled for custom roles and policies to be effective.

 

Step 7: Assign the Role to Users

 

Assign the newly created role to specific users by altering their role attribute through an SQL command. Here's an example:

alter user your_user set role = 'new_role';

Replace your_user with the user you wish to assign the new role. This command links the user to the newly defined role permissions.

 

Step 8: Verify Role Permissions

 

After setting custom roles and permissions, verify that everything is functioning as expected. Test by signing in as a user assigned to new_role and interacting with the database to ensure the role permissions work correctly.

 

Step 9: Maintain and Update Roles as Needed

 

As your application and security requirements evolve, update your roles and permissions as necessary. Regularly review your security policies and roles to adapt to new requirements or optimizations.

 

By following these steps, you can effectively manage and assign custom user roles within Supabase to meet your application's specific needs and security standards.

Want to explore opportunities to work with us?

Connect with our team to unlock the full potential of no-code solutions with a no-commitment consultation!

Book a Free Consultation

Client trust and success are our top priorities

When it comes to serving you, we sweat the little things. That’s why our work makes a big impact.

Rapid Dev was an exceptional project management organization and the best development collaborators I've had the pleasure of working with. They do complex work on extremely fast timelines and effectively manage the testing and pre-launch process to deliver the best possible product. I'm extremely impressed with their execution ability.

CPO, Praction - Arkady Sokolov

May 2, 2023

Working with Matt was comparable to having another co-founder on the team, but without the commitment or cost. He has a strategic mindset and willing to change the scope of the project in real time based on the needs of the client. A true strategic thought partner!

Co-Founder, Arc - Donald Muir

Dec 27, 2022

Rapid Dev are 10/10, excellent communicators - the best I've ever encountered in the tech dev space. They always go the extra mile, they genuinely care, they respond quickly, they're flexible, adaptable and their enthusiasm is amazing.

Co-CEO, Grantify - Mat Westergreen-Thorne

Oct 15, 2022

Rapid Dev is an excellent developer for no-code and low-code solutions.
We’ve had great success since launching the platform in November 2023. In a few months, we’ve gained over 1,000 new active users. We’ve also secured several dozen bookings on the platform and seen about 70% new user month-over-month growth since the launch.

Co-Founder, Church Real Estate Marketplace - Emmanuel Brown

May 1, 2024 

Matt’s dedication to executing our vision and his commitment to the project deadline were impressive. 
This was such a specific project, and Matt really delivered. We worked with a really fast turnaround, and he always delivered. The site was a perfect prop for us!

Production Manager, Media Production Company - Samantha Fekete

Sep 23, 2022