Skip to content
Postgres extensions

uuid-ossp

What it is

uuid-ossp is a PostgreSQL-supplied extension for generating UUIDs using standard version 1, 3, 4, and 5 algorithms. Modern PostgreSQL provides random UUID generation in core. This extension remains useful for namespace-derived and other specialized UUID forms.

What it provides

  • Random version 4 UUIDs
  • Deterministic version 3 and 5 UUIDs derived from a namespace and name
  • Version 1 timestamp-and-MAC-based UUIDs and standard namespace constants

Use it for importing systems that already rely on UUID algorithms, generating the same ID for the same logical name, or creating IDs through a database default.

Use it with Rent

extension uuid_ossp {
  name = "uuid-ossp"
}

The RSL declaration makes installation and migration intent part of the reviewed application schema. Register the typed pack at runtime to call its APIs:

let uuids = extensions.register_pack::<UuidOssp>()?;
let random_default = uuids.v4();
let stable = uuids.v5(
    Namespace::Dns,
    "customer.example.com",
);

let id = uuids
    .client(&pool)
    .v4()
    .await?;

Run the application

Add the extension declaration above to your application's schema. Confirm that your PostgreSQL provider offers the extension and that your migration role can install it, then apply the migration and run your application:

rent migrate dev --name enable_uuid_ossp
cargo run

Namespace-derived IDs are deterministic identifiers, not secret tokens. Keep the namespace and input normalization stable when using them for deduplication.

Complete example

Download this project, or copy the files below into an empty directory.

Replace RENT_REPOSITORY_URL in Cargo.toml with the URL supplied with your preview access. Use the matching Rent CLI release.

Use an empty PostgreSQL database with the extension binaries available on its server. Set DATABASE_URL to that database and follow any server setup requirements described above.

rent extension check --provider self-hosted
rent generate
rent migrate dev --name initial
cargo run

If your host preinstalls other extensions, declare those in RSL as well. For a database with existing application tables, baseline it first.

Cargo.toml
[package]
name = "rent-extension-uuid-ossp"
version = "0.1.0"
edition = "2024"
[workspace]
[[bin]]
name = "rent-extension-uuid-ossp"
path = "main.rs"
[dependencies]
rent = { git = "RENT_REPOSITORY_URL", tag = "v0.8.6", default-features = false, features = ["postgres", "extensions", "migrate"] }
rent-ext-uuid-ossp = { git = "RENT_REPOSITORY_URL", tag = "v0.8.6" }
anyhow = "1.0.104"
tokio = { version = "1.53.1", features = ["full"] }
rent.toml
schema = "schema.rsl"
generated = "generated"
migrations = "migrations"

[database]
dialect = "postgres"
backends = ["postgres"]
schema.rsl
datasource db {
  provider = "postgresql"
}

extension uuid_ossp {
  name = "uuid-ossp"
}

model Post {
  id    BigInt  @id
  title String
  body  String  @default("")
}
main.rs
use anyhow::Result;
use rent::{driver::DatabasePool, extension::ExtensionRegistry};
#[allow(dead_code, unused_imports)]
#[rustfmt::skip]
#[path = "generated/mod.rs"]
mod generated;
#[tokio::main]
async fn main() -> Result<()> {
    let pool = DatabasePool::connect_url(&std::env::var("DATABASE_URL")?).await?;
    let mut extensions = ExtensionRegistry::default();
    use rent_ext_uuid_ossp::{Namespace, UuidOssp, UuidOsspExt};
    let pack = extensions.register_pack::<UuidOssp>()?;
    let stable = pack.v5(Namespace::Dns, "customer.example.com");
    let uuid = pack.client(&pool).query_required::<rent::uuid::Uuid>(stable).await?;
    println!("Namespace identifier: {uuid}");
    Ok(())
}