16

I'm trying to create a UUID id in a table with PostgreSQL. I tried with:

id uuid PRIMARY KEY DEFAULT uuid_generate_v4()

But I get:

ERROR: function uuid_generate_v4() does not exist HINT: No function matches the given name and argument types. You might need to add explicit type casts.

I tried with adding the schema like: id uuid PRIMARY KEY DEFAULT public.uuid_generate_v4() (as seen in a comment here)

I also checked if the extension is there (SELECT * FROM pg_available_extensions;), and yes I have it installed in the PostgreSQL database:

enter image description here

I read that if the Postgres is runing in --single mode, this may not work, but I don't know how to test it or if there is any way to do it.

Somebody knows how I can resolve the problem? Or any other option? Is it a good idea to use like this:

SET DEFAULT uuid_in(md5(random()::text || now()::text)::cstring);
3
  • 3
    "Note: If you only need randomly-generated (version 4) UUIDs, consider using the gen_random_uuid() function from the pgcrypto module instead." Commented Apr 28, 2017 at 17:38
  • 1
    i don't think you have uuid-ossp loaded Commented Apr 28, 2017 at 17:42
  • 1
    Did you maybe install uuid-ossp into a separate schema that is not on the search_path? Commented Apr 28, 2017 at 17:56

5 Answers 5

37

Because the function uuid_generate_v4 is not found, it suggests that the extension uuid-ossp is not loaded

pg_available_extensions lists the extensions available, but not necessarily loaded.

to see the list of loaded extensions query the view pg_extension as such:

select * from pg_extension;

To load the uuid-ossp extension run the following:

CREATE EXTENSION "uuid-ossp";

note: this will require super user privileges.

After the uuid-ossp extension is successfully loaded, you should see it in the pg_extension view & the function uuid_generate_v4 should be available.

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

3 Comments

If I use: select * from pg_extensions; I get the error: ERROR: relation "pg_extensions" does not exist. For the moment I'm using: uuid_in(md5(random()::text || now()::text)::cstring)
I'm sorry, that was a typo on my part. The view is actually called pg_extension, updating post
Also make sure you are in the right database before doing this. Extensions seem to be loaded on a per-database level.
2

In my case I needed to add the schema to the function call like this: app.uuid_generate_v4()

instead of this: uuid_generate_v4()

I found the schema for each extension by running this query:

SELECT 
pge.extname,
pge.extversion,
pn.nspname AS schema
FROM pg_extension pge 
JOIN pg_catalog.pg_namespace pn ON pge.extnamespace = pn."oid" ;

Comments

2

I was facing the same problem. I just deleted extension and run:

CREATE EXTENSION IF NOT EXISTS "uuid-ossp";

And my problem solved.

Comments

1

Sometime you've just to run the line:

CREATE EXTENSION IF NOT EXISTS "uuid-ossp";

just before the creation of the table

Comments

0

tl;dr

Just use uuidv7 or uuidv4 built-in functions rather than calling the uuid-ossp extension.

Use Version 7 UUIDs for efficient indexing.

CREATE TABLE example_ (
    id_ UUID PRIMARY KEY DEFAULT uuidv7()
);

Now built-in

Postgres 18+ comes with a Version 7 (time + random) UUID generating function, uuidv7.

Postgres 18+ and many earlier versions come with a Version 4 (entirely random) UUID generating function, gen_random_uuid (also known as uuidv4 in Postgres 18+).

So you may no longer need the uuid-ossp extension.

Version 7 UUID

In 2024, RFC 9562 invented two new Versions of UUID. Version 6 is a reworking of Version 1, both based on points in time and space. Version 7 is brand-new, based on a point in time plus random bits.

Index-friendly

Both Versions 6 & 7 have a bit-layout designed to be friendly to indexing engines. These Versions solve the indexing inefficiency problem encountered with larger tables using Versions 1-5 UUIDs.

Prefer Version 7 over Version 1 & 6 if possible, such as greenfield projects.

Here is the bit-layout of Version 7 UUIDs.

 0                   1                   2                   3
 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|                           unix_ts_ms                          |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|          unix_ts_ms           |  ver  |       rand_a          |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|var|                        rand_b                             |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|                            rand_b                             |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

The implementation of the Postgres function uuidv7 takes the specification-granted option of using a sub-millisecond timestamp fraction in place of rand_a seen in diagram above.

Comments

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.