A seed script should be safe to run twice. Idempotency means that repeating the same operation converges on the intended dataset without duplicating rows or silently resetting unrelated user changes. It is not a blanket permission to overwrite every field.
Choose a stable key
Use a unique domain key that remains stable across runs, such as a known slug or external fixture ID. Do not identify seed rows by a display name that may change or collide. Before writing, inspect the schema’s unique constraints and make the script’s identity rule match them.
Separate create behavior from update behavior. On creation, populate the full fixture. On update, change only fields the seed owns. For example, a documentation fixture may own title and body but not view counts or a manually changed publication date. If the script publishes content, require an explicit target and state the number of rows it expects to affect.
Protect the environment
Refuse to run against production unless the user supplies an explicit production flag and the action is designed for that environment. Print the database host/database name with credentials redacted. Wrap related writes in a transaction and fail if the count or current state differs from expectations. Never delete rows as a hidden cleanup step.
Test the script against an empty database and then run it a second time. Compare row counts and selected fields after both runs. Add a dry-run mode that reports proposed changes when the operation is significant. For generated or curated content, keep the source fixtures in version control; do not regenerate them from generic boilerplate at runtime.
Choose what happens to an edited seed row
PostgreSQL uses the conflict target in INSERT ON CONFLICT to identify a unique constraint or matching index. With DO NOTHING, a conflicting row is preserved. With DO UPDATE, the statement applies the chosen assignments. Seed fixtures need a deliberate choice because a development editor can change the title of content that was originally inserted by a seed. PostgreSQL INSERT
A PostgreSQL 16.14 test seeded an existing slug with the title “Editorial title,” then ran an INSERT with the same slug and a new seed title. ON CONFLICT DO NOTHING left one row with the editorial title. This gives a useful default for seed values owned by another part of the application after insertion. For configuration where the seed truly owns particular fields, use an explicit assignment list rather than replacing the record. Run the test on a disposable database and use the schema's actual NOT NULL requirements.
Worked example
The conflict target is the existing stable slug. In a disposable session, compare the title before and after the second write.
sqlCREATE TEMP TABLE seed_article (slug text PRIMARY KEY, title text NOT NULL); INSERT INTO seed_article VALUES ('first-article', 'Editorial title'); INSERT INTO seed_article VALUES ('first-article', 'Seed default') ON CONFLICT (slug) DO NOTHING; SELECT slug, title FROM seed_article;




Community
Discussion
Loading comments…