Tutorials

How to Generate a UUID in JavaScript, Python, Java, C# and SQL

Marcus Brennanยทยท9 min read

The safest way to generate a UUID is usually one line of standard-library code. The important decision is not how to assemble the dashes; it is which UUID version fits the job and where the ID should be created. This guide gives you copy-ready examples and the trade-offs behind them.

Generate a UUID online

For a quick value, test fixture, or database seed, use the online UUID Generator. Select UUID v4 for random IDs or UUID v7 for time-ordered IDs, choose a quantity, and copy or download the list. Generation happens locally in your browser.

Generate a UUID in JavaScript

Modern browsers and recent Node.js versions expose a secure v4 generator through Web Crypto:

const id = crypto.randomUUID();
console.log(id);

Do not replace it with a snippet based on Math.random(). Web Crypto uses a cryptographically secure source and sets the UUID version and variant bits correctly. If your project needs UUID v7, use a maintained package that explicitly implements RFC 9562.

Generate a UUID in Python

Python's standard uuid module generates v4 values:

import uuid

record_id = uuid.uuid4()
print(record_id)

Keep the UUID object while working in Python and convert it with str(record_id) only when a string is required. For v7, check the Python version and library documentation rather than attempting to encode the timestamp bits manually.

Generate a UUID in Java

Java's standard library provides random v4 UUIDs:

import java.util.UUID;

UUID recordId = UUID.randomUUID();
System.out.println(recordId);

Pass the UUID value to a database driver as a UUID when the driver supports it. Turning it into text too early can lose native-type and storage benefits.

Generate a GUID in C#

C# and .NET use the name GUID for the same identifier format:

Guid recordId = Guid.NewGuid();
Console.WriteLine(recordId);

There is no conversion step between the result and a UUID. The terminology difference is explained in GUID vs UUID.

Generate UUIDs in PostgreSQL and MySQL

PostgreSQL can generate a random UUID with gen_random_uuid() and store it in the native uuid type:

CREATE TABLE orders (
  id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
  created_at timestamptz NOT NULL DEFAULT now()
);

In MySQL, UUID() generates a UUID string. Prefer compact binary storage for large indexed tables and use the conversion functions supported by your MySQL version:

CREATE TABLE orders (
  id BINARY(16) PRIMARY KEY DEFAULT (UUID_TO_BIN(UUID())),
  created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);

Database capabilities change by release, especially around v7, so verify the function in the documentation for the version you deploy.

UUID v4 or v7?

Choose v4 when you need a random, opaque ID with broad runtime support. Choose v7 when IDs will be high-volume database keys and approximate creation order is useful. UUID v7 exposes its creation time, so v4 remains preferable when that metadata should stay hidden. The complete comparison is in UUID v4 vs v7.

Where should the UUID be generated?

Application-side generation lets a service assign an ID before it talks to the database, which is useful for offline work, distributed writes, and event creation. Database-side defaults reduce application code and centralize the rule. Both approaches work; consistency across the system matters more than the location.

Common mistakes to avoid

  • Do not build production UUIDs with Math.random() or another weak random source.
  • Do not use a UUID as a secret; identifiers are names, not credentials. Generate an API key for authentication.
  • Do not store UUID text in VARCHAR(36) when the database offers a native UUID or binary type.
  • Do not remove a database unique constraint merely because collisions are unlikely.
  • Do not assume a v7 value hides creation time; the timestamp is part of the identifier.

The practical default

Start with a securely generated v4 UUID unless you have a concrete reason to choose another version. For a write-heavy indexed table, evaluate v7. Whichever version you use, rely on a standard implementation, store it efficiently, and enforce uniqueness in the database. When you just need values now, the bulk UUID Generator creates up to 500 at once.

Try the tools

Frequently Asked Questions

How do I generate a UUID in JavaScript?

In a modern browser or recent Node.js release, call crypto.randomUUID() to create a standards-compliant random UUID v4. Use a maintained UUID library when you need v7 or support for older runtimes.

Should UUIDs be generated in the app or database?

Either is valid. Generate them in the app when you need the identifier before an insert or create records across multiple services. A database default is simpler when all writes pass through one database.

Can I use Math.random() to generate a UUID?

Do not use Math.random() for production UUIDs. It is not a cryptographically secure source and can make collisions or predictable values more likely. Use Web Crypto or your platform's standard UUID package.

Which UUID version should I generate?

Use v4 for random, opaque general-purpose IDs. Consider v7 for high-write database primary keys where time ordering improves index locality. Use v5 only when the same namespace and name must always create the same ID.

MB

Marcus Brennan writes for CodeUtilityKit, where the team builds free, privacy-first developer tools that run entirely in your browser. Every guide is written and reviewed by developers who use these tools daily.