ToolSite
All posts

How to Bulk-Generate UUIDs for Database Seeding

Bulk generate UUID v4 values for database seeding, test fixtures, and load testing. Batch produce hundreds of unique IDs with our free UUID generator tool.

By ToolSite5 min readguides

Why Bulk UUID Generation

You're writing a database seed script, populating test fixtures, or preparing a load-test dataset. You need 10,000 unique user IDs. Auto-incrementing integers won't work because the production system uses UUIDs and you need the seeded data to match the real schema.

Manually generating UUIDs one at a time isn't practical. You need a way to produce them in bulk, and the method has to be fast. Generating 10,000 UUIDs should take under a second, not under a minute.

UUIDs (Universally Unique Identifiers) are 128-bit values typically displayed as 36-character strings with four hyphens:

550e8400-e29b-41d4-a716-446655440000

UUID v4 is the most common variant for database primary keys. It's generated from random numbers (122 of the 128 bits are random, 6 are version/variant markers). The random source matters. If the random number generator is weak, your UUIDs aren't as unique as you think.

Browser-Based Generation

The UUID Generator supports batch generation. Set the count to however many you need, click Generate, and copy the list. All UUIDs are generated locally in your browser using crypto.getRandomValues(), the same CSPRNG that backs production UUID libraries.

The output is one UUID per line, ready to paste into a SQL INSERT script, a CSV file, or a JSON array.

This approach is best when you need a one-off batch of UUIDs and don't want to write a script. For programmatic generation or integration into build pipelines, use the language-native approaches below.

Single-UUID vs Bulk Generation in Code

Generating a single UUID is trivial in every language:

import uuid
print(uuid.uuid4())  # '550e8400-e29b-41d4-a716-446655440000'
const { randomUUID } = require("crypto");
console.log(randomUUID());  // '550e8400-e29b-41d4-a716-446655440000'

Bulk generation needs a loop, and the performance differences between languages can matter when you're generating millions of UUIDs.

SQL

For PostgreSQL with the built-in gen_random_uuid() (available in PG 13+):

-- Generate 100 UUID v4 values
SELECT gen_random_uuid()
FROM generate_series(1, 100);

For seeding a table directly:

INSERT INTO users (id, name, email)
SELECT
  gen_random_uuid(),
  'user_' || i,
  'user_' || i || '@example.com'
FROM generate_series(1, 10000) AS i;

For PostgreSQL with the uuid-ossp extension (older versions):

CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
SELECT uuid_generate_v4()
FROM generate_series(1, 100);

For MySQL 8.0+:

-- Generate a single UUID (returns as string)
SELECT UUID();

-- For bulk, use a recursive CTE or a numbers table
WITH RECURSIVE seq(n) AS (
  SELECT 1
  UNION ALL
  SELECT n + 1 FROM seq WHERE n < 100
)
SELECT UUID() FROM seq;

MySQL's UUID() function returns UUID v1 (time-based, not random). For v4 (random UUID), you'll need a user-defined function or generate them in application code.

Python

import uuid

# Generate 10,000 UUIDs
uuids = [str(uuid.uuid4()) for _ in range(10_000)]

# Write to file
with open("uuids.txt", "w") as f:
    for u in uuids:
        f.write(u + "\n")

# Or directly into a PostgreSQL insert
insert_sql = "INSERT INTO users (id) VALUES "
values = ", ".join(f"('{u}')" for u in uuids)
# Be careful with SQL injection. Use parameterized queries in production.

The uuid module is in the standard library. No dependencies.

For very high volumes (millions), use uuid.uuid4().hex to get the 32-character hex string without hyphens. It's slightly faster and shorter for storage. Add the hyphens back when you need to display or validate the UUID.

Node.js

const { randomUUID } = require("crypto");
const fs = require("fs");

const uuids = Array.from({ length: 10_000 }, () => randomUUID());

// Write to file
fs.writeFileSync("uuids.txt", uuids.join("\n"));

// Or prepare for SQL insert
const values = uuids.map(u => `('${u}')`).join(", ");

crypto.randomUUID() is available in Node.js 15.6+ and uses the system CSPRNG. For older Node.js versions, use the uuid npm package:

const { v4: uuidv4 } = require("uuid");
const uuids = Array.from({ length: 10_000 }, () => uuidv4());

Go

package main

import (
    "fmt"
    "github.com/google/uuid"
)

func main() {
    for i := 0; i < 10000; i++ {
        fmt.Println(uuid.New().String())
    }
}

The github.com/google/uuid package is the standard Go UUID library. It uses crypto/rand internally, which is the OS CSPRNG.

Performance Considerations

Generating 10,000 UUIDs in any language takes under a second. At 1 million UUIDs, differences emerge:

  • Python's uuid.uuid4(): about 0.8 seconds per million on modern hardware
  • Node.js crypto.randomUUID(): about 0.3 seconds per million
  • Go uuid.New(): about 0.2 seconds per million

The bottleneck is the CSPRNG, not the UUID formatting. If you need raw unique values at maximum speed and don't need the UUID string format, consider crypto.randomBytes(16) (Node.js) or os.urandom(16) (Python) and convert to hex. That's the raw 128-bit random value without the UUID version/variant formatting overhead.

Use Cases

  • Database seeding: populate development and staging databases with realistic UUID primary keys
  • Test fixtures: create deterministic test data with known UUIDs by generating once and checking the fixture into version control
  • Load testing: generate thousands of unique identifiers for request payloads without collisions
  • CSV exports: add a unique identifier column to exported data
  • Distributed systems: generate IDs that are unique across multiple nodes without coordination. This is the core advantage of UUIDs over auto-increment.

Try it yourself: open the UUID Generator. Set the quantity to 10 and click Generate. Observe that every UUID has a 4 in the third group (the version marker) and one of 8, 9, a, or b as the first character of the fourth group (the variant marker). Generate 100 and scan for any duplicates. There won't be any.

Related Reading