https://supabase.com/ logo
Hi all, how can I seed a database with users that ...
# help
a
Hi all, how can I seed a database with users that I can login to and explore the app from their account? Currently, I have a
seed.sql
file in my supabase directory that gets run when I reset the database using the Supabase CLI (local development). That seed file does something like this:
Copy code
sql
DO $$

  DECLARE user_1_id uuid;
  DECLARE user_2_id uuid;

  BEGIN
    user_1_id := uuid_generate_v4();
    user_2_id := uuid_generate_v4();

    INSERT INTO auth.users (id, email, created_at, updated_at)
      VALUES
        (user_1_id, 'audrow@hey.com', now(), now()),
        (user_2_id, 'not-audrow@hey.com', now(), now());

    -- use user_1_id and user_2_id in other tables
$$;
With this setup, I see the two users in my database, but I cannot login with them (I get a generic database error) and cannot email them a login link or reset their password. I think that I can probably do what I want with a JS script that uses my Supabase client, but don't know how to use a JS file to seed the database automatically each time I reset the database.
n
Hello @Audrow! This thread has been automatically created from your message in #843999948717555735 a few seconds ago. We have already mentioned the @User so that they can see your message and help you as soon as possible! Want to unsubscribe from this thread? Right-click the thread in Discord (or use the ``...`` menu) and select "Leave Thread" to unsubscribe from future updates. Want to change the title? Use the ``/title`` command! We have solved your problem? Click the button below to archive it.
j
There are other things that happen when you add a user to the database. I’d recommend fake signing up a user using inviteUserByEmail and taking a look at what is populated in the database.
a
I've tried to do so, but haven't had luck yet. I'm signing up a new user and then comparing the fields. This is what I have now.
Copy code
sql
    INSERT INTO auth.users (id, instance_id, email, created_at, updated_at, email_confirmed_at, recovery_sent_at, last_sign_in_at, raw_app_meta_data, aud, role, encrypted_password, raw_user_meta_data, is_super_admin, confirmation_token, email_change_token_new, email_change)
      VALUES
        (user_1_id, uuid_generate_v4(), 'audrow@hey.com', now(), now(), now(), now(), now(), email_raw_meta_data, 'authenticated', 'authenticated', '$2a$10$X', '{}', false, '', '', ''),
        (user_2_id, uuid_generate_v4(), 'not-audrow@hey.com', now(), now(), now(), now(), now(), email_raw_meta_data, 'authenticated', 'authenticated', '$2a$10$X', '{}', false, '', '', '');
I'm only modifying
auth.users
. I'm not sure if other databases are modified. I think I'll make a JS script to populate users and data.
Specifically, I'm not able to send a signin email or reset the password for the users I create above with SQL.
I fixed this by `pg_dump`ing the data in my user's table. This approach should let you dump other data from the users, too.
Copy code
pg_dump postgresql://postgres:postgres@localhost:54322/postgres -t auth.users --insert --data-only > supabase/seed.sql
This way, I don't have to run a javascript file, and I can have data to test my application with.
n
Thread was archived by @Audrow. Anyone can send a message to unarchive it.