1

I am trying to create a supabase table client side using RPC.

I have setup a function in supabase to be called when RPC is invoked. Where I pass an table name client side in Next.js with pages router.

BEGIN
EXECUTE 'CREATE TABLE ' || custom_table_name || ' (
    id serial PRIMARY KEY,
    name text,
    age integer,
    email text,
    address text
);';
END;

From client side i call this function and pass the name.

async function createTable(customTableName) {
    try {
      const { data, error } = await supabase.rpc("create_custom_table", {
        custom_table_name: customTableName,
      });

      if (error) {
        console.error("Error: ", error);
      } else {
        console.log("Success:", data);
      }
    } catch (error) {
      console.error("Error: ", error);
    }
  }

I am experiencing the error:

code: "42501"
details: null
hint: null
message: "permission denied for schema public"

Can someone point me in the right direction on how to make this work.

juliomalves
  • 42,130
  • 20
  • 150
  • 146
Aleksa_97
  • 21
  • 3
  • 3
    Please don't do this as it is bad by design. This would mean that anyone who is using your application client side could call this endpoint and create as many tables as they wish. In relational databases you create the tables prior to using them, it's not the same as NoSQL where you create tables at runtime. You should plan your database structure out well and create the tables necessary from inside the database. – Andrew Smith Jun 25 '23 at 16:40

0 Answers0