Code Guide

How to Generate a UUID in JavaScript, Python, and SQL

๐Ÿ“… September 2026โฑ๏ธ 5 min read
Every major language and database now has built-in UUID v4 generation โ€” no external library required in most cases. Here's the copy-paste snippet for each.

JavaScript / Node.js

Modern browsers and Node.js (18+) support crypto.randomUUID() natively โ€” no package install needed:

// Browser or Node.js 18+
const id = crypto.randomUUID();
console.log(id); // e.g. "3fa85f64-5717-4562-b3fc-2c963f66afa6"

For older Node.js versions, use the built-in crypto module or the popular uuid npm package:

// Node.js < 18, via built-in crypto module
const { randomUUID } = require('crypto');
const id = randomUUID();

// Or via the "uuid" npm package (npm install uuid)
import { v4 as uuidv4 } from 'uuid';
const id2 = uuidv4();

Python

Python's standard library has had UUID support built in since Python 2.5 โ€” no pip install required:

import uuid

id = uuid.uuid4()
print(id)          # 3fa85f64-5717-4562-b3fc-2c963f66afa6
print(str(id))      # as a string
print(id.hex)        # without hyphens: 3fa85f6457174562b3fc2c963f66afa6

SQL

PostgreSQL

-- Postgres 13+: built-in, no extension needed
SELECT gen_random_uuid();

-- Older Postgres: enable the extension once
CREATE EXTENSION IF NOT EXISTS "pgcrypto";
SELECT gen_random_uuid();

-- As a table column default
CREATE TABLE users (
  id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
  name TEXT
);

MySQL

-- Built-in function, returns a UUID v1 (timestamp + MAC) by default
SELECT UUID();

-- MySQL stores it as a string (CHAR(36)) unless you convert it
INSERT INTO users (id, name) VALUES (UUID(), 'Alice');

SQL Server

-- Returns a GUID (Microsoft's UUID implementation)
SELECT NEWID();

-- As a table column default
CREATE TABLE users (
  id UNIQUEIDENTIFIER DEFAULT NEWID(),
  name NVARCHAR(100)
);

SQLite

-- No native UUID function โ€” generate in application code
-- and store as TEXT, or use a random-blob expression:
SELECT lower(hex(randomblob(4)) || '-' || hex(randomblob(2)) || '-4' ||
  substr(hex(randomblob(2)),2) || '-' ||
  substr('89ab',abs(random()) % 4 + 1, 1) || substr(hex(randomblob(2)),2) || '-' ||
  hex(randomblob(6)));
๐Ÿ’ก Don't need to write code right now?

If you just need a batch of UUIDs to paste into a seed file, test fixture, or spreadsheet, skip the code entirely โ€” generate them directly in your browser instead.

Generate UUID v4 Now

Bulk-generate up to 50 random UUID v4 identifiers, free and instant, no code required.

Open UUID Generator โ†’

Frequently Asked Questions

Does MySQL's UUID() function generate a v4 (random) UUID?
No โ€” MySQL's built-in UUID() generates a version 1 UUID (timestamp + MAC address based). If you need a v4 random UUID in MySQL 8.0+, use UUID_TO_BIN(UUID(), 1) is not a v4 converter โ€” instead generate it in application code with your language's UUID library and pass it in, or use REPLACE(UUID(), '-', '') combined with random functions if you specifically need randomness at the database layer.
How do I store a UUID efficiently in a database?
Postgres and SQL Server have native UUID/UNIQUEIDENTIFIER column types that store the value as raw 16 bytes instead of a 36-character string, which is both smaller and faster to index. MySQL and SQLite lack a native type โ€” if storage efficiency matters, convert the UUID to a 16-byte BINARY(16) column instead of storing the hyphenated string directly.
Can I generate a UUID without installing any library?
Yes, in most modern stacks: JavaScript/Node 18+ and Python both have UUID v4 generation built into their standard library, and Postgres/SQL Server have it built into the database engine. No install needed unless you're on an older runtime or need MySQL to produce v4 instead of its default v1.