Database
Seeding
How to seed the database with initial data.
Default seed
The seed script creates an admin user for initial access. Run it with:
npm run db:seedThis executes tsx lib/db/seed.ts, which creates:
- Email:
admin@example.com - Password:
password123(hashed with bcrypt, cost factor 12) - Role:
admin - Email verified: Yes (set to current date)
The script uses onConflictDoNothing on the email column, so it's safe to run multiple times — it won't duplicate the user.
Seed script
The full script at lib/db/seed.ts:
import "dotenv/config";
import { drizzle } from "drizzle-orm/postgres-js";
import postgres from "postgres";
import { users } from "./schema";
import { hash } from "bcryptjs";
async function seed() {
const client = postgres(process.env.DATABASE_URL!, { prepare: false });
const db = drizzle(client);
const email = "admin@example.com";
const password = "password123";
const hashedPassword = await hash(password, 12);
await db
.insert(users)
.values({
name: "Admin",
email,
password: hashedPassword,
role: "admin",
emailVerified: new Date(),
})
.onConflictDoNothing({ target: users.email });
console.log(`Admin user seeded (${email})`);
await client.end();
}
seed().catch((err) => {
console.error("Seed failed:", err);
process.exit(1);
});Customizing the seed
To change the default admin credentials, edit the email and password variables in lib/db/seed.ts before running the seed.
To seed additional data (test users, sample orders, etc.), add more db.insert() calls to the seed() function.
Important notes
- The seed script creates its own database connection (separate from the app's
lib/db/index.tsclient) and closes it when done - It loads environment variables from
.envor.env.localviadotenv/config - Always change the default admin credentials after your first login in a production environment