Using Custom Schemas
By default, your database has a public schema which is automatically exposed on data APIs.
Creating custom schemas
You can create your own custom schema/s by running the following SQL, substituting myschema with the name you want to use for your schema:
CREATE SCHEMA myschema;Exposing custom schemas
You can expose custom database schemas - to do so you need to follow these steps:
- Go to API settings and add your custom schema to "Exposed schemas".
- Run the following SQL, substituting
myschemawith your schema name:
GRANT USAGE ON SCHEMA myschema TO anon, authenticated, service_role;GRANT ALL ON ALL TABLES IN SCHEMA myschema TO anon, authenticated, service_role;GRANT ALL ON ALL ROUTINES IN SCHEMA myschema TO anon, authenticated, service_role;GRANT ALL ON ALL SEQUENCES IN SCHEMA myschema TO anon, authenticated, service_role;ALTER DEFAULT PRIVILEGES FOR ROLE postgres IN SCHEMA myschema GRANT ALL ON TABLES TO anon, authenticated, service_role;ALTER DEFAULT PRIVILEGES FOR ROLE postgres IN SCHEMA myschema GRANT ALL ON ROUTINES TO anon, authenticated, service_role;ALTER DEFAULT PRIVILEGES FOR ROLE postgres IN SCHEMA myschema GRANT ALL ON SEQUENCES TO anon, authenticated, service_role;Now you can access these schemas from data APIs:
// Initialize the JS clientimport { createClient } from '@supabase/supabase-js'const supabase = createClient(SUPABASE_URL, SUPABASE_PUBLISHABLE_KEY, { db: { schema: 'myschema' },})// Make a requestconst { data: todos, error } = await supabase.from('todos').select('*')// You can also change the target schema on a per-query basisconst { data: todos, error } = await supabase.schema('myschema').from('todos').select('*')With generated Database types that include public, createClient<Database>(...) type-checks db.schema against public only, unless the schema name is also passed as the second generic: createClient<Database, 'myschema'>(...). supabase.schema('myschema').from(...) infers its schema per call and needs no second generic. In both cases myschema has to be present in the generated Database type.
// Initialize the Flutter clientawait Supabase.initialize( url: supabaseUrl, publishableKey: publishableKey, postgrestOptions: const PostgrestClientOptions(schema: 'myschema'),);final supabase = Supabase.instance.client;// Make a requestfinal data = await supabase.from('todos').select();// You can also change the target schema on a per-query basisfinal data = await supabase.schema('myschema').from('todos').select();// Initialize the client with a custom schemavar supabase = new Supabase.Client( SUPABASE_URL, SUPABASE_PUBLISHABLE_KEY, new SupabaseOptions { Schema = "myschema" });await supabase.InitializeAsync();// Make a requestvar todos = await supabase.From<Todo>().Get();# Append /rest/v1/ to your URL, and then use the table name as the route.# for GET or HEAD request use Accept-Profilecurl '<SUPABASE_URL>/rest/v1/todos' \ -H "apikey: <SUPABASE_PUBLISHABLE_KEY>" \ -H "Authorization: Bearer <SUPABASE_PUBLISHABLE_KEY>" \ -H "Accept-Profile: myschema"# for POST, PATCH, PUT and DELETE Request use Content-Profilecurl -X POST '<SUPABASE_URL>/rest/v1/todos' \ -H "apikey: <SUPABASE_PUBLISHABLE_KEY>" \ -H "Authorization: Bearer <SUPABASE_PUBLISHABLE_KEY>" \ -H "Content-Type: application/json" \ -H "Content-Profile: myschema" \ -d '{"column_name": "value"}'