A PostgreSQL extension for encoding UUIDs to Base62 alphabet and decoding Base62 back to UUID. It is built in Rust with pgrx.
This extension provides functions to convert PostgreSQL UUIDs to and from Base62 encoded strings. Base62 encoding uses alphanumeric characters (0-9, A-Z, a-z) to create shorter, URL-safe representations of UUIDs.
It provides two functions:
base62_encode(uuid) → textbase62_decode(text) → uuid
Encodes a UUID into a Base62 string representation.
Parameters:
uuid: A PostgreSQL UUID value
Returns:
text: The Base62 encoded representation as a URL-safe string
Example:
SELECT base62_encode('f81d4fae-7dec-11d0-a765-00a0c91e6bf6'::uuid);Decodes a Base62 encoded string back to a UUID.
Parameters:
text: Base62 encoded representation created bybase62_encode
Returns:
uuid: The decoded UUID value
- PostgreSQL 18 or later (Note: theoretically this can be everything from PostgreSQL 14 upwards. I just didn't test this)
- Rust toolchain
- pgrx development tools
# Install pgrx if not already installed
cargo install --locked cargo-pgrx
# Initialize pgrx for your PostgreSQL version
cargo pgrx init
# Build and install the extension
cargo pgrx installcreate extension pg_base62;Use it like this:
select base62_encode('0199a3e9-85b2-764a-8ff0-a1fcd5f9a3b2'::uuid);
base62_encode
───────────────────────
31CmN3LMJd6n2qPHOlsuY
(1 row)
select base62_decode('31CmN3LMJd6n2qPHOlsuY');
base62_decode
──────────────────────────────────────
0199a3e9-85b2-764a-8ff0-a1fcd5f9a3b2
(1 row)
# Run Rust unit tests
cargo test
# Run PostgreSQL integration tests
cargo pgrx testsrc/lib.rs- Main extension code with encoding functionssrc/error.rs- Custom error typessrc/bin/pgrx_embed.rs- pgrx embedding binarypg_base62.control- PostgreSQL extension control file
-
Base62 Character Set:
0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz - UUID Representation: UUIDs are converted to 128-bit integers before encoding
- Output Length: Base62 encoded UUIDs are up to 22 characters long (to be precise: $\log_{62}(2^{128})$)
MIT License - see the repository for full license text.