Generate database seed/sample data
✓Works with OpenClaudeYou are a database developer building seed data generators. The user wants to create realistic sample data for database tables to populate development and testing environments.
What to check first
- Run
npm listto verify you have a database driver installed (e.g.,mysql2,pg,sqlite3,mongodb) - Confirm your database connection string or credentials are available in
.envor config files - Check that your target database exists and you have write permissions
Steps
- Install a seed data library like
faker(generates realistic fake data) withnpm install @faker-js/faker - Create a
seedsorseedersdirectory in your project root to organize seed files - Import your database connection module and the Faker library at the top of your seed script
- Define a main seeding function that connects to the database and creates sample records
- Use Faker methods like
faker.person.name(),faker.internet.email(),faker.location.city()to generate realistic values - Loop to insert multiple records with
INSERTstatements or ORM methods (e.g.,.create()in Sequelize) - Add error handling with try-catch blocks and log success/failure messages
- Execute the seed file with
node seeds/seedDatabase.jsor add it as an npm script inpackage.json
Code
const { faker } = require('@faker-js/faker');
const mysql = require('mysql2/promise');
require('dotenv').config();
const pool = mysql.createPool({
host: process.env.DB_HOST,
user: process.env.DB_USER,
password: process.env.DB_PASSWORD,
database: process.env.DB_NAME,
waitForConnections: true,
connectionLimit: 10,
queueLimit: 0,
});
async function seedUsers(count = 50) {
const connection = await pool.getConnection();
try {
console.log(`Seeding ${count} users...`);
for (let i = 0; i < count; i++) {
const firstName = faker.person.firstName();
const lastName = faker.person.lastName();
const email = faker.internet.email({ firstName, lastName });
const phone = faker.phone.number();
const createdAt = faker.date.past({ years: 2 });
const query = 'INSERT INTO users (first_name, last_name, email, phone, created_at) VALUES (?, ?, ?, ?, ?)';
await connection.execute(query, [firstName, lastName, email, phone, createdAt]);
}
console.log(`✓ Successfully seeded ${count} users`);
} catch (error) {
console.error('Error seeding users:', error.message);
throw error;
} finally {
connection.release();
}
}
async function seedProducts(count = 30) {
Note: this example was truncated in the source. See the GitHub repo for the latest full version.
Common Pitfalls
- Treating this skill as a one-shot solution — most workflows need iteration and verification
- Skipping the verification steps — you don't know it worked until you measure
- Applying this skill without understanding the underlying problem — read the related docs first
When NOT to Use This Skill
- When a simpler manual approach would take less than 10 minutes
- On critical production systems without testing in staging first
- When you don't have permission or authorization to make these changes
How to Verify It Worked
- Run the verification steps documented above
- Compare the output against your expected baseline
- Check logs for any warnings or errors — silent failures are the worst kind
Production Considerations
- Test in staging before deploying to production
- Have a rollback plan — every change should be reversible
- Monitor the affected systems for at least 24 hours after the change
Related Database Skills
Other Claude Code skills in the same category — free to download.
Migration Generator
Generate database migration files
Query Optimizer
Analyze and optimize slow database queries
Schema Designer
Design database schema from requirements
Index Advisor
Suggest database indexes based on query patterns
ORM Model Generator
Generate ORM models from database schema
SQL to ORM
Convert raw SQL queries to ORM syntax
Database Backup Script
Create database backup and restore scripts
Connection Pool Setup
Configure database connection pooling
Want a Database skill personalized to YOUR project?
This is a generic skill that works for everyone. Our AI can generate one tailored to your exact tech stack, naming conventions, folder structure, and coding patterns — with 3x more detail.