Skip to content

Instantly share code, notes, and snippets.

@khattaksd
Created September 22, 2023 00:19
Show Gist options
  • Star 23 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save khattaksd/4e8f4c89f4e928a2ecaad56d4a17ecd1 to your computer and use it in GitHub Desktop.
Save khattaksd/4e8f4c89f4e928a2ecaad56d4a17ecd1 to your computer and use it in GitHub Desktop.
Supabase Seed Users for local development & testing
-- supabase/seed.sql
--
-- create test users
INSERT INTO
auth.users (
instance_id,
id,
aud,
role,
email,
encrypted_password,
email_confirmed_at,
recovery_sent_at,
last_sign_in_at,
raw_app_meta_data,
raw_user_meta_data,
created_at,
updated_at,
confirmation_token,
email_change,
email_change_token_new,
recovery_token
) (
select
'00000000-0000-0000-0000-000000000000',
uuid_generate_v4 (),
'authenticated',
'authenticated',
'user' || (ROW_NUMBER() OVER ()) || '@example.com',
crypt ('password123', gen_salt ('bf')),
current_timestamp,
current_timestamp,
current_timestamp,
'{"provider":"email","providers":["email"]}',
'{}',
current_timestamp,
current_timestamp,
'',
'',
'',
''
FROM
generate_series(1, 10)
);
-- test user email identities
INSERT INTO
auth.identities (
id,
user_id,
identity_data,
provider,
last_sign_in_at,
created_at,
updated_at
) (
select
uuid_generate_v4 (),
id,
format('{"sub":"%s","email":"%s"}', id::text, email)::jsonb,
'email',
current_timestamp,
current_timestamp,
current_timestamp
from
auth.users
);
@fluid-design-io
Copy link

Thanks for the great seed template!
I got this error while seeding the data:
failed to send batch: ERROR: null value in column "provider_id" of relation "identities" violates not-null constraint (SQLSTATE 23502)
After some digging, seems like newer version of Supabase added a required provider_id field in auth.identities. To fix this, simply insert provider_id with value id like so:

before:

-- test user email identities
INSERT INTO
    auth.identities (
        id,
        user_id,
        identity_data,
        provider,
        last_sign_in_at,
        created_at,
        updated_at
    ) (
        select
            uuid_generate_v4 (),
            id,
            format('{"sub":"%s","email":"%s"}', id::text, email)::jsonb,
            'email',
            current_timestamp,
            current_timestamp,
            current_timestamp
        from
            auth.users
    );

After:

-- test user email identities
INSERT INTO
    auth.identities (
        id,
        user_id,
        -- New column
        provider_id,
        identity_data,
        provider,
        last_sign_in_at,
        created_at,
        updated_at
    ) (
        select
            uuid_generate_v4 (),
            id,
            -- New column
            id,
            format('{"sub":"%s","email":"%s"}', id :: text, email) :: jsonb,
            'email',
            current_timestamp,
            current_timestamp,
            current_timestamp
        from
            auth.users
    );

@kerren
Copy link

kerren commented Mar 21, 2024

Thanks @khattaksd 😄!! I also had to use @fluid-design-io's method for it to work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment