From ea9e29c52a76fc65c2a6939d59dd5ac524fbb2dd Mon Sep 17 00:00:00 2001 From: Copple <10214025+kiwicopple@users.noreply.github.com> Date: Thu, 3 Aug 2023 12:43:19 +0200 Subject: [PATCH] Adds some seed commands --- .../guides/cli/seeding-your-database.mdx | 32 +++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/apps/docs/pages/guides/cli/seeding-your-database.mdx b/apps/docs/pages/guides/cli/seeding-your-database.mdx index 7afe96493c7..e0c101e423c 100644 --- a/apps/docs/pages/guides/cli/seeding-your-database.mdx +++ b/apps/docs/pages/guides/cli/seeding-your-database.mdx @@ -7,14 +7,42 @@ export const meta = { subtitle: 'Testing emails for Supabase Auth on your local machine.', } +When you run `supabase init` we create an empty `seed.sql` file in the root of the `supabase` folder. You can use this to populate your database with seed data. + ## What is seed data? -- +Seeding is the process of populating a database with initial data, typically used to provide sample or default records for testing and development purposes. You can use this to create "reproducable environments" for local development, staging, and production. ## Using the seed file +The `seed.sql` file is run every time you run `supabase start` or `supabase db reset`. Seeding is done _after_ all the database migrations have been run. As a general rule, you should not add schema statements to your seed file, only data. + +You can add any SQL statements to this file. For example. + +```sql supabase/seed.sql +insert into countries + (name, code) +values + ('United States', 'US'), + ('Canada', 'CA'), + ('Mexico', 'MX'); ``` -supabase/seed.sql + +### Splitting up your seed file + +The seed file can grow quite large, so you can split it up into multiple files. For example, you can create a `seed` folder and add a `countries.sql` file. Then you can add the following to your `seed.sql` file. + +```sql supabase/seed.sql +\i seed/countries.sql +``` + +### Using CSV data to seed your database + +co +You can use the `COPY` command to import CSV data into your database. For example, you can create a `seed` folder and add a `countries.csv` file. Then you can add the following to your `seed.sql` file. + +```sql supabase/seed.sql +COPY countries (name, code) FROM 'seed/countries.csv' WITH (FORMAT csv); ``` ## Generating seed data from Production