How do I create separate tables for each user in the database??

Hi,
I want to have separate tables for each user, is this possible and how can I implement this?

I asked supabase AI and it gave me this answer

how can I automatically create separate tables for each user
To automatically create separate tables for each user, you can use row level security (RLS) policies. With RLS policies, you can restrict access to specific rows in a table based on the user who is accessing the data.

Here's an example of how to create a table with RLS policies:

create table private_data (
  id uuid not null,
  user_id uuid not null,
  data text,
  primary key (id)
);

alter table private_data enable row level security;

create policy private_data_policy on private_data for select using (auth.uid() = user_id);

In this example, we create a table called private_data with columns for id, user_id, and data. We then enable row level security on the table using the alter table command. Finally, we create a policy called private_data_policy that restricts access to rows in the private_data table based on the user_id column.

With this policy in place, each user will only be able to access rows in the private_data table where their user_id matches the auth.uid() value of the user making the request.

Note that you will need to create a new table for each user, or modify the user_id column in an existing table to use RLS policies.
Was this page helpful?