These examples generate UUIDs in ten languages and databases.

Support depends on the runtime or library, so check the requirements below. Here are the snippets you will actually use, grouped by language.

Each language also has its own page with v4, v7, v5 and validation in one place: JavaScript, Python, Java, Go, C#, PHP, Ruby, Rust, PostgreSQL and MySQL.

JavaScript in the browser

const id = crypto.randomUUID();
// "e40ad4eb-1e83-43e4-90d7-3e3b3180d7cf"  (v4)

crypto.randomUUID() is available in every current browser. It only works on secure origins, meaning https or localhost.

For v7 you need a library:

import { v4, v7 } from "uuid";
v4();  // random
v7();  // time ordered

Node.js

import { randomUUID } from "node:crypto";
const id = randomUUID();  // v4, built in since Node 14.17

The same uuid npm package used in the browser works in Node and adds v1, v3, v5, v6 and v7.

Python

import uuid

uuid.uuid4()   # UUID('9c5b94b1-35ad-49bb-b118-8e8fc24abf80')
str(uuid.uuid4())   # as a string
uuid.uuid5(uuid.NAMESPACE_URL, "https://mybittools.com")   # deterministic

Python 3.14 added uuid.uuid7() to the standard library. On older versions install the uuid7 or uuid-utils package.

Go

import "github.com/google/uuid"

id := uuid.New()          // v4
id7, err := uuid.NewV7()  // v7
s := id.String()

The standard library has no UUID package. The google/uuid documentation lists its generation and parsing functions.

Java and Kotlin

import java.util.UUID;

UUID id = UUID.randomUUID();   // v4
String s = id.toString();

The JDK only ships v4 and v3. For v7 use a library such as uuid-creator or java-uuid-generator.

C# and .NET

Guid id = Guid.NewGuid();          // v4
Guid id7 = Guid.CreateVersion7();  // v7, .NET 9 and later
string s = id.ToString();

.NET calls them GUIDs but they are ordinary UUIDs. See UUID vs GUID for the one byte order quirk to watch for.

Rust

use uuid::Uuid;

let id = Uuid::new_v4();
let id7 = Uuid::now_v7();

Enable the v4 and v7 features of the uuid crate in Cargo.toml.

PostgreSQL

SELECT gen_random_uuid();   -- v4, built in since PostgreSQL 13
SELECT uuidv7();            -- v7, PostgreSQL 18 and later

CREATE TABLE orders (
id uuid PRIMARY KEY DEFAULT uuidv7(),
total numeric
);

MySQL and MariaDB

SELECT UUID();                        -- v1 as a string
SELECT UUID_TO_BIN(UUID(), 1);        -- 16 byte binary, timestamp first
SELECT BIN_TO_UUID(id, 1) FROM t;     -- back to text

MySQL only generates v1. Generate v7 in your application and insert it as BINARY(16).

MariaDB 10.7 added a native UUID column type.

SQLite

No built in generator. Create the UUID in your application.

If you must do it in SQL, this produces a valid v4:

SELECT lower(hex(randomblob(4))) || '-' || lower(hex(randomblob(2))) || '-4' ||
substr(lower(hex(randomblob(2))), 2) || '-' ||
substr('89ab', abs(random()) % 4 + 1, 1) ||
substr(lower(hex(randomblob(2))), 2) || '-' || lower(hex(randomblob(6)));

Command line

uuidgen                  # macOS and most Linux, v4
uuidgen -t               # v1 on Linux
cat /proc/sys/kernel/random/uuid   # Linux, v4
python3 -c "import uuid; print(uuid.uuid4())"

On Windows PowerShell, [guid]::NewGuid() does the same.

Without any code

When you just need a handful of IDs for a test fixture or a config file, our online UUID generator makes up to 1000 UUIDs at a time using v1, v3, v4, v5, v6 or v7 with one click copy.