如何在Supabase中重建误删的默认auth.users表?
重建Supabase默认auth.users表的方法
如果你误删了Supabase的auth.users表,可通过以下步骤重建以恢复注册功能:
- 登录Supabase控制台,进入SQL编辑器,新建查询。
- 执行以下SQL创建与默认结构完全匹配的
auth.users表:
CREATE TABLE IF NOT EXISTS auth.users ( id uuid NOT NULL DEFAULT gen_random_uuid() PRIMARY KEY, instance_id uuid NOT NULL, email text, encrypted_password text, email_confirmed_at timestamp with time zone, invited_at timestamp with time zone, confirmation_token text, confirmation_sent_at timestamp with time zone, recovery_token text, recovery_sent_at timestamp with time zone, email_change_token_new text, email_change text, email_change_sent_at timestamp with time zone, last_sign_in_at timestamp with time zone, raw_app_meta_data jsonb DEFAULT '{}'::jsonb, raw_user_meta_data jsonb DEFAULT '{}'::jsonb, is_super_admin boolean DEFAULT false, created_at timestamp with time zone NOT NULL DEFAULT now(), updated_at timestamp with time zone NOT NULL DEFAULT now(), CONSTRAINT users_instance_id_fkey FOREIGN KEY (instance_id) REFERENCES auth.instances(id) ON DELETE CASCADE );
- 创建默认索引:
CREATE INDEX IF NOT EXISTS idx_auth_users_instance_id_email ON auth.users USING btree (instance_id, email); CREATE INDEX IF NOT EXISTS idx_auth_users_instance_id_id ON auth.users USING btree (instance_id, id);
- 恢复必要的触发器(若关联函数未被删除):
-- 确保用户创建处理函数存在,若不存在则创建 CREATE OR REPLACE FUNCTION auth.handle_new_user() RETURNS trigger AS $$ BEGIN INSERT INTO public.profiles (id, email) VALUES (NEW.id, NEW.email); RETURN NEW; END; $$ LANGUAGE plpgsql SECURITY DEFINER; -- 重建插入触发器 DROP TRIGGER IF EXISTS on_auth_user_created ON auth.users; CREATE TRIGGER on_auth_user_created AFTER INSERT ON auth.users FOR EACH ROW EXECUTE FUNCTION auth.handle_new_user();
注意:如果你的项目自定义了
profiles表或触发器逻辑,需调整上述触发器函数代码以匹配实际结构。
完成所有操作后,测试Supabase Auth注册接口,确认功能恢复正常。
内容的提问来源于stack exchange,提问作者Gianna
相关产品推荐
相关产品推荐

