UUID Generator: How to Generate and Use UUIDs in Python, JavaScript, and SQL

UUIDs (Universally Unique Identifiers) solve a classic programming problem: how do two different systems create IDs that will never collide, without asking a central server for permission? The answer is a 128-bit identifier that any machine can generate independently. This guide covers the practical side — generating UUIDs in the languages you actually use, and knowing which version fits which job.

If you just need a quick ID without writing code, the free UUID Generator at Today Calculator creates UUIDs in multiple versions instantly, with bulk generation support.

UUID Versions at a Glance

VersionHow It Is GeneratedBest Use Case
Version 1Timestamp + MAC addressTime-ordered sequencing, legacy systems
Version 4Random numbersGeneral purpose (most common)
Version 5SHA-1 hash of namespace + nameDeterministic IDs from a name
Version 7Timestamp + random (newer)Database-friendly time-sorted UUIDs

Generating UUIDs in Python

Python ships with the uuid module in the standard library — no pip install needed.

import uuid

# Random (v4) — the default choice
print(uuid.uuid4())
# e.g. f47ac10b-58cc-4372-a567-0e02b2c3d479

# Time-sorted (v7) — better for database indexes
print(uuid.uuid7())

# Deterministic (v5) from a name
print(uuid.uuid5(uuid.NAMESPACE_URL, "example.com"))

Generating UUIDs in JavaScript

Modern browsers and Node.js 16.7+ include crypto.randomUUID(), which generates a v4 UUID with no dependencies.

// Browser or Node.js (v16.7+)
const id = crypto.randomUUID();
// e.g. 9b1deb4d-3b7d-4bad-9bdd-2b0d7b3dcb6d

// Node.js also supports v1 via the uuid package
// npm install uuid
const { v4: uuidv4 } = require('uuid');
console.log(uuidv4());

Generating UUIDs in SQL

Databases that need distributed, collision-free primary keys can generate UUIDs natively:

-- PostgreSQL
SELECT gen_random_uuid();  -- v4

-- MySQL 8+
SELECT UUID();  -- v1-style

-- SQL Server
SELECT NEWID();  -- v4

Best Practices

  • Use v4 for general-purpose IDs — it is the safest default in most applications.
  • Consider v7 for database primary keys — time-ordered UUIDs reduce index fragmentation and improve insert performance.
  • Store as binary (16 bytes) instead of a 36-character string when storage matters.
  • Never rely on UUIDs for secrecy — they are unique, not private. Use them as identifiers, not tokens.
  • Test your database driver — some ORMs handle UUID columns automatically, others need explicit type mapping.

Whether you are generating a single ID for a test or thousands for a distributed system, the free UUID Generator is a handy fallback when you are away from your code editor.

Leave a Reply