August 4, 2026

How to Generate a UUID in JavaScript, Python, and SQL

In JavaScript, call crypto.randomUUID() — built into every modern browser and Node.js, no library required. In Python, uuid.uuid4() from the standard library. In PostgreSQL, gen_random_uuid() as a column default. All three generate a cryptographically random v4 UUID with no setup.

UUID Generator showing a randomly generated v4 UUID with a one-click copy option

JavaScript

const id = crypto.randomUUID();
// e.g. "3fa85f64-5717-4562-b3fc-2c963f66afa6"

crypto.randomUUID() has been available in browsers and Node.js for years now and needs no import in the browser. One real constraint: it only works in a secure context (HTTPS or localhost) — calling it from a plain HTTP page will throw. For older environments, crypto.getRandomValues() can generate the same v4 format manually.

Python

import uuid
id = str(uuid.uuid4())
# e.g. "3fa85f64-5717-4562-b3fc-2c963f66afa6"

Python's uuid module is part of the standard library — no pip install needed. uuid4() generates a random v4 UUID; the module also supports uuid1() (MAC address plus timestamp based, rarely what you want for public-facing IDs) if you specifically need that variant.

SQL

CREATE TABLE users (
  id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
  email VARCHAR(255) NOT NULL
);

PostgreSQL's gen_random_uuid() (built in since Postgres 13) generates a v4 UUID as a column default, so every insert gets one automatically without application code needing to generate it. Older Postgres versions need the pgcrypto extension enabled first; MySQL uses UUID() instead, which — worth noting — generates a v1 (timestamp-based) UUID by default, not v4.

The one thing all of these have in common

Every method above draws from a cryptographically secure random source, not a predictable one — that matters if these IDs will ever be exposed publicly, since a guessable generation scheme can leak information about ordering or volume. If you need the IDs to also sort by creation time, look at UUID v7 instead of v4 specifically.

Want to try this yourself?

Open UUID Generator