Database Seeding

Populate your database with initial or test data.

Overview

Prax provides flexible database seeding with support for multiple file formats. Use seeding to populate your database with initial data during development, create test fixtures, or set up demo environments.

Multiple Formats

.rs, .sql, .json, and .toml seed files

Environment Protection

Prevent accidental production seeding

Migration Integration

Auto-seed after migrations

Special Functions

now(), uuid() in declarative formats

Basic Usage

Configuration

Configure seeding in your prax.toml file:

Production Protection

By default, seeding is disabled for production and staging environments. Use --force to override, but be careful!

Supported Formats

Format Extension Description Best For
Rust .rs Full programmatic control with Prax client Complex seeding logic, computed data
SQL .sql Raw SQL statements executed directly Simple data, database-specific features
JSON .json Declarative data definition Structured data, API compatibility
TOML .toml Human-readable declarative format Configuration-like seed data

SQL Seed File

The simplest format - raw SQL executed against your database:

JSON Seed File

Declarative format with table-based structure:

TOML Seed File

Human-readable alternative to JSON:

Rust Seed Script

Full programmatic control using the Prax client:

Add a binary target to your Cargo.toml:

Special Functions

JSON and TOML seed files support special functions for dynamic values:

Migration Integration

Seeding integrates seamlessly with the migration workflow:

Seed File Locations

Prax automatically searches for seed files in common locations:

Commands Reference

Command Description
prax db seed Run the seed file
prax db seed --seed-file <path> Run a specific seed file
prax db seed --reset Reset database before seeding
prax db seed --environment <env> Seed for specific environment
prax db seed --force Force seeding even if disabled for environment
prax migrate dev Run migrations + seed
prax migrate dev --skip-seed Run migrations without seeding
prax migrate reset --seed Reset database and run seed

Best Practices

🎯 Use idempotent seeds

Write seeds that can run multiple times safely. Use ON CONFLICT DO NOTHING in SQL or check for existing records in Rust.

🔒 Protect production

Keep production = false in your seed configuration. Never seed production accidentally.

📦 Use Rust for complex logic

When you need computed values, relationships, or conditional logic, use a Rust seed script for full control.

📋 Specify table order

In JSON/TOML files, use the order array to ensure tables are seeded in the correct order for foreign key constraints.

Next Steps