If you're getting the error:
ERROR: 42710: policy "Users can view public templates and own templates" for table "templates" already exists
This means the database policies already exist. Here are the solutions:
Run the policy cleanup script in your Supabase SQL Editor:
-- Copy and paste this into Supabase SQL Editor:
-- Drop existing policies if they exist
DROP POLICY IF EXISTS "Users can view public templates and own templates" ON templates;
DROP POLICY IF EXISTS "Users can insert own templates" ON templates;
DROP POLICY IF EXISTS "Users can update own templates" ON templates;
DROP POLICY IF EXISTS "Users can delete own templates" ON templates;
-- Ensure RLS is enabled
ALTER TABLE templates ENABLE ROW LEVEL SECURITY;
-- Create policies for templates
CREATE POLICY "Users can view public templates and own templates" ON templates
FOR SELECT USING (
is_public = true OR
auth.uid()::text = user_id
);
CREATE POLICY "Users can insert own templates" ON templates
FOR INSERT WITH CHECK (auth.uid()::text = user_id);
CREATE POLICY "Users can update own templates" ON templates
FOR UPDATE USING (auth.uid()::text = user_id);
CREATE POLICY "Users can delete own templates" ON templates
FOR DELETE USING (auth.uid()::text = user_id);
-- Grant necessary permissions
GRANT ALL ON templates TO authenticated;
GRANT SELECT ON templates TO anon;If you want to start fresh, run the reset script:
- Go to your Supabase Dashboard
- Navigate to SQL Editor
- Copy and paste the content from
scripts/reset-database.sql - Run the script
- Go to Supabase Dashboard → Authentication → Policies
- Find the "templates" table policies
- Delete all existing policies
- Run the setup script again
After running the fix, verify that:
- ✅ Templates table exists
- ✅ RLS is enabled
- ✅ Policies are created
- ✅ Sample templates are inserted
- ✅ Authentication works
- Register a new user
- Sign in successfully
- Browse templates
- Create a new template
- Access profile page
The application should work without any database errors.