How to Generate a UUID in JavaScript, Python, SQL or Online
One-line ways to create a UUID v4 in JavaScript, Node.js, Python and PostgreSQL, plus how to make hundreds at once for a spreadsheet or test data.
The short answer
Most platforms can create a random version 4 UUID in one line: crypto.randomUUID() in browsers and Node.js, uuid.uuid4() in Python, and gen_random_uuid() in PostgreSQL 13 or later. When you need UUIDs without writing code, for a spreadsheet, test data or a config file, the UUID Generator creates up to 1,000 at once in your browser, with options for uppercase or no hyphens.
What you are generating
A UUID (universally unique identifier) is 128 bits written as 32 hexadecimal characters in five groups: 8-4-4-4-12, for example f49923aa-ccca-41ba-914c-78746ddc19b8. In a version 4 UUID, almost all of those bits are random. You can recognise one by the 4 at the start of the third group, and by the first character of the fourth group, which is always 8, 9, a or b.
For the details of the format and why collisions are not a practical worry, see What Is a UUID v4? Format, Uniqueness and When to Use v7.
JavaScript in the browser
Use the built-in Web Crypto method:
const id = crypto.randomUUID();
It returns a lowercase v4 UUID string using a cryptographically secure random number generator. MDN notes that it is available only in secure contexts, which means pages served over HTTPS or from localhost. On a plain http:// page, crypto.randomUUID may be undefined.
If you must support a non-secure page, use crypto.getRandomValues() to fill 16 random bytes, set the version and variant bits, and format them as hex. That is exactly what the Lazyblink generator does internally.
Avoid building UUIDs from Math.random(). It is not designed to be unpredictable, and identifiers that might be guessed should come from a secure generator.
Node.js
The same method is available from the crypto module:
const { randomUUID } = require('crypto');console.log(randomUUID());
We ran this on Node.js 22 and it printed a standard lowercase v4 UUID. In modern Node versions you do not need the separate uuid package just to make v4 identifiers.
Python
Python's standard library has a uuid module:
import uuidprint(uuid.uuid4())
uuid.uuid4() returns a UUID object; wrap it in str() to get the text form, or use .hex for the 32 characters without hyphens.
Two newer additions according to the Python documentation:
- Command line (Python 3.12+):
python -m uuidprints a UUID without writing a script. By default it makes a version 4 UUID. - Version 7 (Python 3.14+):
uuid.uuid7()creates time-ordered UUIDs, which sort roughly by creation time. That can be friendlier for database indexes than fully random v4 values.
PostgreSQL
PostgreSQL 13 and later include gen_random_uuid(), which returns a version 4 UUID with no extension needed:
SELECT gen_random_uuid();
A common pattern is to use it as a column default, so every new row gets an ID automatically:
CREATE TABLE orders (id uuid PRIMARY KEY DEFAULT gen_random_uuid(), created_at timestamptz DEFAULT now());
PostgreSQL 18 adds uuidv4(), an alias that also generates version 4, and uuidv7(), which generates time-ordered version 7 UUIDs. Check your server version with SELECT version(); before relying on them. On versions older than 13, the uuid-ossp or pgcrypto extensions provide UUID functions.
Other databases and languages
Most environments have a built-in option, but names and UUID versions differ. For example, some database functions return older time-based versions rather than v4. Check your platform's documentation for which version a function returns before you depend on it for ordering, privacy or uniqueness assumptions.
Making many UUIDs at once without code
For a spreadsheet column, seed data, or a batch of test records:
uuids.txt.Paste the copied list into the first cell of a spreadsheet column and each UUID lands in its own row. The generator uses the browser's secure random source, so the IDs are not sent anywhere and are not reused.
Checking that a string is a valid v4 UUID
To validate input in code, a regular expression is usually enough. This pattern accepts a hyphenated version 4 UUID in either case:
^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$(use a case-insensitive flag)
We tested it against 10,000 UUIDs from crypto.randomUUID() and all of them matched, while a UUID with a different version digit in the third group did not. In Python, uuid.UUID(text) raises a ValueError for malformed input, and the resulting object's .version attribute tells you which version it is.
Which format should you use?
| Format | Example | Use when |
|---|---|---|
| Standard lowercase | f49923aa-ccca-41ba-914c-78746ddc19b8 | Default for most APIs, databases and JSON |
| UPPERCASE | F49923AA-CCCA-41BA-914C-78746DDC19B8 | A system or style guide requires capitals |
| No hyphens | f49923aaccca41ba914c78746ddc19b8 | A field limited to 32 characters, file names, compact keys |
UUID comparison should be case-insensitive according to the standard, but not every system follows that, so stick to one style within a project. If you are coming from Windows or .NET, where these are called GUIDs, they are the same thing; see UUID vs GUID: Is There Any Difference?.
Common mistakes
- Storing UUIDs as text when the database has a UUID type. A native
uuidcolumn in PostgreSQL uses 16 bytes and validates the format; a text column uses more space and accepts typos. - Mixing formats. One system writes uppercase, another lowercase, and a string comparison fails. Normalise to lowercase before comparing.
- Assuming v4 IDs are ordered. Version 4 values are random, so sorting by ID does not sort by creation time. Use a timestamp column or v7 if order matters.
- Using UUIDs as secrets. A UUID is an identifier, not a password or access token. Generate security tokens with tools designed for that purpose.
Frequently asked questions
How do I generate a UUID in JavaScript?
Call crypto.randomUUID(). It returns a random version 4 UUID in browsers on HTTPS or localhost pages and in Node.js.
How do I generate a UUID in Python?
Import the uuid module and call uuid.uuid4(). Use str() for the hyphenated text or .hex for 32 characters without hyphens. From Python 3.12, python -m uuid works on the command line.
How do I generate a UUID in PostgreSQL?
Use SELECT gen_random_uuid(); which is built in from PostgreSQL 13 and returns a version 4 UUID. PostgreSQL 18 also adds uuidv7() for time-ordered IDs.
How can I generate many UUIDs at once without coding?
Use an online UUID generator that supports bulk output. Lazyblink's creates up to 1,000 version 4 UUIDs at a time, which you can copy or download as a text file.
Put this guide into practice with our free online tool — no signup required.
Open tool