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

> One-line ways to generate a random UUID (v4) or a time-ordered UUID (v7) in JavaScript, Python, Java, C#, Go, PostgreSQL, SQL Server, MySQL and the terminal.

Source: https://widgetpantry.com/en/how-to-generate-uuid · Türkçe: https://widgetpantry.com/tr/uuid-nasil-olusturulur

Published 2026-11-20

Most languages and databases can generate a UUID in one line, with no extra library. Below are copy-paste examples for the common ones. If you just need a few without writing code, the [UUID Generator](https://widgetpantry.com/en/uuid-generator) makes up to 100 v4 or v7 UUIDs at once in your browser. For v4 versus v7, see [What is a UUID?](https://widgetpantry.com/en/what-is-a-uuid)

## JavaScript / TypeScript

crypto.randomUUID() is built into every current browser and Node.js and uses a cryptographically secure random source. In browsers it only works on HTTPS (or localhost) pages.

```
// Browsers and Node.js 19+
const id = crypto.randomUUID();
// "3b241101-e2bb-4255-8caf-4136c566a962"

// Older Node.js (14.17+)
import { randomUUID } from "node:crypto";
const id2 = randomUUID();
```

## Python

The standard uuid module is all you need. uuid7() arrived in Python 3.14; on older versions use uuid4().

```
import uuid

str(uuid.uuid4())   # random, version 4
str(uuid.uuid7())   # time-ordered, version 7 (Python 3.14+)
```

## Java

java.util.UUID generates random (version 4) UUIDs with no extra dependency.

```
import java.util.UUID;

String id = UUID.randomUUID().toString();
```

## C# / .NET

Microsoft calls UUIDs GUIDs; the format is the same. Guid.CreateVersion7() was added in .NET 9.

```
var id = Guid.NewGuid();            // version 4
var ordered = Guid.CreateVersion7(); // version 7 (.NET 9+)
```

## Go

Go's standard library has no UUID package; google/uuid is the usual choice.

```
// go get github.com/google/uuid
import "github.com/google/uuid"

id := uuid.NewString()        // version 4
v7, err := uuid.NewV7()       // version 7
```

## SQL (PostgreSQL, SQL Server, MySQL)

Generating the ID in the database makes it easy to use as a column default. Note that MySQL's UUID() returns version 1 (time and machine based), not version 4.

```
-- PostgreSQL 13+
SELECT gen_random_uuid();
-- PostgreSQL 18+: time-ordered
SELECT uuidv7();

-- SQL Server
SELECT NEWID();

-- MySQL (returns a version 1 UUID)
SELECT UUID();
```

## Terminal

For a quick value on the command line:

```
# Linux / macOS
uuidgen

# PowerShell
[guid]::NewGuid().ToString()
```

## Frequently asked questions

### Is a GUID the same as a UUID?

Yes. GUID is Microsoft's name for the same 128-bit format; Guid.NewGuid() returns a random version 4 UUID.

### Can I generate a UUID with Math.random?

You shouldn't. Math.random is not cryptographically secure and makes collisions more likely. Use crypto.randomUUID() in browsers and Node.js.

### Should I use v4 or v7 as a database primary key?

For large tables v7 is better: it is time-ordered, so new rows go to the end of the index and inserts stay fast. v4 is supported everywhere and does not reveal when the ID was created.

## Use the tool

- [UUID Generator](https://widgetpantry.com/en/uuid-generator): Create up to 100 UUID v4 or v7 values at once using your browser's secure random generator. Uppercase and no-hyphen formats, one-click copy.
