NEWSEQUENTIALID Function - SQL Server to PostgreSQL Migration

In SQL Server, the NEWSEQUENTIALID function generates a UUID that is greater than any UUID previously generated since the OS was started.

In PostgreSQL, you can use the GEN_RANDOM_UUID function, but the generated UUIDs are not sequential.

SQL Server:

  -- Sample table (NEWSEQUENTIALID can be used in DEFAULT only)
  CREATE TABLE t1
  (
    c1 UNIQUEIDENTIFIER DEFAULT NEWSEQUENTIALID()
  );
 
  -- Sample rows
  INSERT INTO t1 VALUES (DEFAULT);
  INSERT INTO t1 VALUES (DEFAULT);
  INSERT INTO t1 VALUES (DEFAULT);

Inserted values:

c1
D403819C-BC94-F111-AF50-FC5CEEFDF1CD
39B95CC7-BC94-F111-AF50-FC5CEEFDF1CD
3AB95CC7-BC94-F111-AF50-FC5CEEFDF1CD

PostgreSQL:

  -- Sample table
  CREATE TABLE t1
  (
    c1 UUID DEFAULT GEN_RANDOM_UUID()
  );
 
  -- Sample rows
  INSERT INTO t1 VALUES (DEFAULT);
  INSERT INTO t1 VALUES (DEFAULT);
  INSERT INTO t1 VALUES (DEFAULT);

Inserted values:

c1
c3d6abb0-2a76-474f-8414-68ba66e4663c
7f693e37-dd6b-4844-aca9-43a28d878e0a
aa2b72d7-b770-49d2-a113-b4b1932db2e1

For more information, see SQL Server to PostgreSQL Migration.