Other blogsGuides

How to generate UUID values in Python, JavaScript, SQL and the shell

One-liners for v4 and v7 in each language, and a browser tool for the times you only need a few to paste.

Here is how to generate UUID values in the places you already work: one line of code in Python, JavaScript, SQL, the shell or PowerShell, and a browser tab for the times you only need a few to paste. For that last case, AyeTab is the simplest option, since its generator sits in your new tab and runs locally.

Pick the version first

The current standard, RFC 9562 (May 2024), defines several UUID versions. In practice you choose between two:

  • v4 is random. Use it when a value only has to be unique.
  • v7 starts with a Unix timestamp in milliseconds, so values sort by creation time. The RFC recommends v7 over the older time-based v1 and v6 where possible, and it suits database keys.

Every snippet below makes v4 unless it says otherwise.

The one-liners

Where v4 v7
Browser or Node.js crypto.randomUUID() crypto.randomUUIDv7() (Node.js 26.1+)
Python uuid.uuid4() uuid.uuid7() (Python 3.14+)
PostgreSQL gen_random_uuid() or uuidv4() uuidv7() (PostgreSQL 18)
Linux shell uuidgen -r uuidgen -7
.NET Guid.NewGuid() Guid.CreateVersion7()
PowerShell New-Guid

JavaScript

In the browser, crypto.randomUUID() returns a v4 string from a cryptographically secure random source. MDN notes it only works in secure contexts such as HTTPS pages. If it is undefined on a plain HTTP page, that is the reason. Node.js has the same function in its crypto module, and Node.js 26.1 added crypto.randomUUIDv7().

const id = crypto.randomUUID();

Python

The standard library covers it. uuid4() is the random version, and Python 3.14 added uuid6(), uuid7() and uuid8(). You can also run the module from a terminal: python -m uuid prints a v4 UUID, and -u picks another version.

import uuid
print(uuid.uuid4())

Python's docs also warn that uuid1() can include the computer's network address, which is a privacy reason to prefer v4 or v7.

SQL

PostgreSQL makes UUIDs in the database itself. gen_random_uuid() and uuidv4() both return v4, and PostgreSQL 18 added uuidv7(). A table default looks like this:

CREATE TABLE events (
  id uuid PRIMARY KEY DEFAULT uuidv7(),
  name text
);

Shell and PowerShell

On Linux, uuidgen from util-linux prints a UUID. -r asks for a random one, -t for the classic time-based kind, and -7 for a sortable v7. On Windows, New-Guid in PowerShell returns a random GUID, which is the same thing under another name.

When you just need a few to paste

Plenty of UUIDs never touch code. You need one for a fixture file, a feature flag, a config entry or a ticket in a test plan. Opening a REPL for that is slow, and a random website is a context switch.

The AyeTab UUID generator handles it: choose how many, press Generate, copy or download. It calls crypto.randomUUID() in your browser, so nothing is sent to a server. With the free Chrome extension it is in every new tab and in a side panel, one Ctrl+K or Cmd+K away, and it works offline once installed. The same tool is on the web app if you would rather not install anything.

It only makes v4, so for v7 keys use the database or language functions above. For anything that runs in a loop or a build, generate in code; a web tool is for the copy and paste moments.

Checking what you generated

A UUID is 32 hex digits in five groups. The first digit of the third group is the version. If you expected v7 and see a 4 there, the wrong function ran. PostgreSQL's uuid_extract_version() reads it for you.

Frequently asked questions

How do I generate a UUID in JavaScript without a library? Call crypto.randomUUID(). It is built into modern browsers and Node.js and returns a v4 string.

What is the fastest way to generate a UUID without writing code? Use a browser tool. AyeTab's generator is free and runs locally, so you can copy a batch in a couple of clicks.

Should database keys be v4 or v7? v7 when you want rows to sort by creation time; v4 works when order does not matter.

Browse guides by topic