11

My tables make use of UUID values. I am inserting rows using SQL SQLWorkbench/J but I don't see how I can generate UUIDs when performing a SQL INSERT INTO (-want-to-generate-uuid-here, '5', 'some value'). Is there a way to generate UUIDs with SQLWorkBench/J?

Thanks

2

4 Answers 4

12

Use

CREATE EXTENSION IF NOT EXISTS "uuid-ossp";

then you can use uuid_generate_v4().

Sign up to request clarification or add additional context in comments.

2 Comments

With Postgres 13 you don't need the extension any more
Update in 2025: In Postgres 18+, that uuid_generate_v4() function has an alternative name, uuidv4. Either function name generates a Version 4 UUID value. See docs. But use Version 7 UUIDs instead, for efficient indexing, per my Answer.
10

Since Postgres 13, UUIDs can be geneerated with gen_random_uuid() without needing the uuid-ossp extension. This returns a Version 4 UUID value.

Since Postgres 18, UUIDs can be generated with uuidv4() (an alias for gen_random_uuid()) and uuidv7() for UUID version 4 and 7 respectively.

Comments

1

The simplest solution is gen_random_uuid():

insert into tableName (id)
values (gen_random_uuid())

Comments

1

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.

5 Comments

when we insert how should we generate the UUID ? do we need to call any special function ?
@ozkanpakdil If you set the DEFAULT as seen here, the function is called automatically for each INSERT. Or the calling app can supply an UUID value. Otherwise, you need to call the function explicitly where you need a value. For example: SELECT uuidv7() ;
so insert into example values (DEFAULT) correct or insert into example values(uuidv7()), let me rephrase my question, how do we write insert statement for the table you defined in the answer ? actually it would help others if you added into the answer for future reference, not many people read comments. and thank you
@ozkanpakdil I added a section Example usage.
You can also use the default keyword in your values list: INSERT INTO example_ VALUES (DEFAULT, 'alpha' );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.