Supabase CLI

Seeding your database

Populate your database with initial data for reproducible environments across local and testing.

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 "reproducible 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:

supabase/seed.sql

_10
insert into countries
_10
(name, code)
_10
values
_10
('United States', 'US'),
_10
('Canada', 'CA'),
_10
('Mexico', 'MX');

Generating seed data

You can generate seed data for local development using Snaplet.

If this is your first time using Snaplet to seed your project, you'll need to setup Snaplet for your project with npx @snaplet/seed init, and follow the interactive prompts. The command will look at your database and its structure, and use it to generate the configuration files and assets needed by @snaplet/seed.

Suppose you have a database with the following schema:

An example schema

You can use the seed script example generated by Snaplet seed.ts to define the values you want to generate. For example:

  • A Post with the title "There is a lot of snow around here!"
  • The Post.createdBy user with an email address ending in "@acme.org"
  • Three Post.comments from three different users.
seed.ts

_23
import { createSeedClient } from '@snaplet/seed'
_23
import { copycat } from '@snaplet/copycat'
_23
_23
async function main() {
_23
const seed = await createSeedClient({ dryRun: true })
_23
_23
await seed.Post([
_23
{
_23
title: 'There is a lot of snow around here!',
_23
createdBy: {
_23
email: (ctx) =>
_23
copycat.email(ctx.seed, {
_23
domain: 'acme.org',
_23
}),
_23
},
_23
Comment: (x) => x(3),
_23
},
_23
])
_23
_23
process.exit()
_23
}
_23
_23
main()

Running npx tsx seed.ts > supabase/seed.sql generates the relevant SQL statements inside your supabase/seed.sql file:


_18
-- The `Post.createdBy` user with an email address ending in `"@acme.org"`
_18
INSERT INTO "User" (name, email) VALUES ("John Snow", "snow@acme.org")
_18
_18
--- A `Post` with the title `"There is a lot of snow around here!"`
_18
INSERT INTO "Post" (title, content, createdBy) VALUES (
_18
"There is a lot of snow around here!",
_18
"Lorem ipsum dolar",
_18
1)
_18
_18
--- Three `Post.Comment` from three different users.
_18
INSERT INTO "User" (name, email) VALUES ("Stephanie Shadow", "shadow@domain.com")
_18
INSERT INTO "Comment" (text, userId, postId) VALUES ("I love cheese", 2, 1)
_18
_18
INSERT INTO "User" (name, email) VALUES ("John Rambo", "rambo@trymore.dev")
_18
INSERT INTO "Comment" (text, userId, postId) VALUES ("Lorem ipsum dolar sit", 3, 1)
_18
_18
INSERT INTO "User" (name, email) VALUES ("Steven Plank", "s@plank.org")
_18
INSERT INTO "Comment" (text, userId, postId) VALUES ("Actually, that's not correct...", 4, 1)

Whenever your database structure changes, @snaplet/seed will need to be regenerated so that it is in sync with your new database structure. You can do this by running npx @snaplet/seed sync.

For more information, check out Snaplet's seed documentation