You asked:
how I can generate UUIDs when performing a SQL INSERT
Call a function that generates a UUID value on-the-fly.
Postgres 18+
To generate a UUID value, you no longer need the uuid-ossp extension discussed in older Answers on Stack Overflow.
In Postgres 18+, use Version 7 UUID values generated by the built-in function, uuidv7. Version 7 is designed for efficient indexing in a database.
CREATE TABLE example_ (
id_ UUID PRIMARY KEY DEFAULT uuidv7() ,
…
);
For more details, see my Answer on similar Question.
Example usage
Define a table whose primary key is a UUID with Version 7 values generated by default.
CREATE TABLE example_ (
id_ UUID PRIMARY KEY DEFAULT uuidv7(),
description_ VARCHAR NOT NULL
);
Insert a row. Notice how the UUID is unspecified in the following SQL. Postgres will automatically call the uuidv7 function we defined as a default for our id_ column`.
INSERT INTO example_ (description_) VALUES ( 'alpha' ) ;
Dump to console.
SELECT * FROM example_ ;
id_ | description_
--------------------------------------+--------------
0199da69-65bb-70ff-b866-063dcd72ea01 | alpha
(1 row)
Delete that row.
DELETE FROM example_ WHERE id_ = '0199da69-65bb-70ff-b866-063dcd72ea01' :: UUID ;
The :: cast may not be required in all situations, but some folks recommend it for clarity.