Free Tools
Blog

How to Generate a UUID in JavaScript, Python and SQL

By Admin Editor · October 13, 2026

You need a unique ID for a database row, a file name or a test fixture. A UUID v4 is the standard answer: 128 random bits written as 36 characters, like 3f2b8c1e-9d4a-4f6b-8e2a-7c5d1b9e0a43. Every major language can make one in a single line.

Colourful code on a monitor

Quickest option: generate one online

  1. Open the free UUID Generator.
  2. Copy the UUID v4 shown — or set a count and click Generate to get many at once for test data.

UUIDs are created in your browser with a cryptographically secure random source.

JavaScript (browser and Node.js)

const id = crypto.randomUUID();

Works in all modern browsers (on HTTPS pages) and in Node.js 19+ globally; in older Node versions use require('crypto').randomUUID() (14.17+).

Python

import uuid

id = uuid.uuid4()       # UUID object
id_str = str(uuid.uuid4())

PostgreSQL

SELECT gen_random_uuid();

CREATE TABLE orders (
  id uuid PRIMARY KEY DEFAULT gen_random_uuid()
);

gen_random_uuid() is built in from PostgreSQL 13. Version 18 also adds uuidv7().

MySQL

SELECT UUID();

MySQL's UUID() returns a version 1 (time-based) UUID, not v4. Store it as BINARY(16) with UUID_TO_BIN(UUID(), 1) to save space and keep indexes fast.

A laptop showing code on a desk

Other languages

  • Java: UUID.randomUUID().toString()
  • C#: Guid.NewGuid()
  • Go: uuid.NewString() with the github.com/google/uuid package
  • PHP: Str::uuid() in Laravel, or ramsey/uuid
  • Command line: uuidgen on macOS and Linux

v4 or v7?

v4 is fully random — perfect for IDs that must not reveal anything. v7 starts with a timestamp, so new rows sort in order and database indexes stay compact. For a new primary key in a large table, v7 is worth a look; for everything else, v4 is fine.

FAQ

Can two UUID v4s collide? In theory, yes; in practice, no. You'd need to generate billions per second for many years to have a meaningful chance.

Are UUIDs secure tokens? v4 UUIDs from a secure generator are unguessable, but for passwords and API keys use a purpose-built generator such as the Password Generator.

Try it now

Get one or a thousand IDs with the free UUID Generator.

Related reading

See What Is a UUID and When to Use One and What Is a Hash?. Browse the full blog for more guides.