Node.js › Databases

What is a database seed script, and how does it differ from a migration?

Intermediate

Answer

A migration changes the database's schema (structure); a seed script populates the database with actual data -- either essential reference data an application needs to function (like default roles or configuration records) or realistic sample data for local development and testing -- run separately from and typically after migrations, since seeding requires the schema to already exist.
// seed.js
async function seed() {
  await db('roles').insert([
    { name: 'admin', permissions: 'all' },
    { name: 'viewer', permissions: 'read-only' },
  ]);
}
Real-world example A team's local development setup runs migrations to create the schema, then a seed script that populates a handful of test users and sample orders, letting any new developer get a fully working local environment with realistic data in a single command.

Common follow-ups: How do you keep seed data consistent and non-duplicating if the seed script is accidentally run more than once?;Should production databases ever run seed scripts, and if so, for what kind of data?