Seed your database
To try your app with sample rows, such as demo notes or test products, seed a backend branch. You write the rows as SQL in a seed.sql file in your app’s folder, and run the file with the AWS Amplify CLI. A deployment applies your migrations and never runs seed.sql. To seed the local backend on your computer, see Develop locally.
Choose what to seed
Amplify creates a backend branch for each Git branch that you push. A new backend branch has your tables and no rows or users from another branch or environment. Put your data in one of two places:
-
Reference data that every branch needs – Put reference data, such as a list of categories, in a migration, so that each deployment applies it. For more information, see Tables and migrations.
-
Sample data for testing – Put it in
seed.sql, and seed each backend branch that you want it in.
Write your seed file
Write each statement so that running the file a second time changes nothing. For example, use on conflict do nothing:
insert into public.products (sku, name) values ('demo-1', 'Demo mug'), ('demo-2', 'Demo shirt') on conflict (sku) do nothing;
You can edit seed.sql and run it again at any time. Amplify doesn’t record which seeds ran, and runs every statement in the file each time.
Seed rows that belong to a user
No user is signed in while a seed runs, so auth.uid() returns null. An insert into a not null default auth.uid() column fails. Set the owner column yourself. If it references auth."user", create the user first, and select the user’s ID in the insert:
insert into public.notes (owner_id, body) select u.id, 'hello' from auth."user" u where u.email = 'mary_major@example.com' and not exists (select 1 from public.notes n where n.owner_id = u.id and n.body = 'hello');
backend seed prints Applied seed.sql whenever the file runs without an error, even when a guard such as not exists adds no row. If a row is missing, check the table, not the command’s output.
Seed an environment
Run the following commands to create the demo user and seed the preview environment:
# user create reads the password from standard input printf %s "$DEMO_PASSWORD" | aws amplifygen3 backend user create --email mary_major@example.com --env preview aws amplifygen3 backend seed --env preview # --file: another file; --branch-arn or --deployment-id: a specific backend branch
backend seed runs the whole file in one transaction. If a statement fails, the command applies none of the file. If every statement succeeds, the rows are in the deployed database when the command finishes. For more information about creating users, see Manage users.
While Docker or Finch is running, backend seed runs psql from the local backend image, which can’t open files on your computer. A seed file that uses \i or \copy … from 'file.csv' fails. To use one, pass your own psql with --psql. For more information, see Connect to your database.
Warning:
backend seed writes to the environment that --env names. Check --env before you seed production.
Seed errors
| Error | Cause | Fix |
|---|---|---|
|
|
Your app’s folder has no |
Create the file, or pass |
An empty seed file isn’t an error:
backend seed prints seed.sql is empty; nothing to seed and exits 0. To seed rows, add statements to the file.