I want to generate uuid like this e58ed763-928c-4155-bee9-fdbaaadc15f3
when data is inserted into table:
CREATE TABLE IF NOT EXISTS bin_lists
(
id uuid NOT NULL,
bin integer,
card_brand character varying(64) COLLATE pg_catalog."default",
status character varying(255) COLLATE pg_catalog."default" NOT NULL,
CONSTRAINT bin_lists_pkey PRIMARY KEY (id)
)
I tried to use this SQL query:
INSERT INTO bin_lists(id, bin, card_brand, status) VALUES(uuid_generate_v4(), 122088, 'UATP', 'ACTIVE');
I get error: ERROR: function uuid_generate_v4() does not exist
Do you know how I can solve this?
Is it better to use this column definition id UUID DEFAULT gen_random_uuid (),
and let database to generate uuid number for each insert?
2
Answers
I can’t say which uuid function you should use, but if you want to use the uuid_generate_v4() function, I believe it exists in the uuid-ossp extension. If the extension is available in your server, run:
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
To get
uuid_generate_v4()
, you would have to create the"uuid-ossp"
extension. But if all you need is a version 4 UUID, and you are using a sufficiently recent version of PostgreSQL, you might as well use the built-in functiongen_random_uuid()
that does exactly the same thing.