Home > Software design >  How to automatically generate new UUID in PostgreSQL?
How to automatically generate new UUID in PostgreSQL?

Time:07-06

I'm using PostgreSQL version 14.4. I installed the uuid-ossp extension.

I created a table like this:

CREATE TABLE reserved_words 
  ADD id uuid NOT NULL DEFAULT uuid_generate_v1()
  ADD word NOT NULL varchar(20);

Unfortunately, when I try adding a new record, rather than a new UUID being generated, instead the "uuid_generate_v1()" string is added in as the id!

I've scoured the Internet but can't find out how to alter things so that the function itself is executed. Any ideas?

CodePudding user response:

My apologies, it actually does work. What's happening is that in DBeaver, the DB client I user, it does at first show the UUID generation function but then when you save the new record, it creates the UUID correctly.

Note: I don't really understand the difference between uuid_generate_v1 and uuid_generate_v4 but am going to opt to use the latter one.

CodePudding user response:

uuid_generate_v1 () → uuid

Generates a version 1 UUID. This involves the MAC address of the computer and a time stamp. Note that UUIDs of this kind reveal the identity of the computer that created the identifier and the time at which it did so, which might make it unsuitable for certain security-sensitive applications.

uuid_generate_v4 () → uuid

Generates a version 4 UUID, which is derived entirely from random numbers.

source

  • Related