Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Postgres UUID type error

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);
like image 718
JP. Aulet Avatar asked Apr 28 '17 17:04

JP. Aulet


People also ask

What is the data type for UUID in PostgreSQL?

The data type uuid stores Universally Unique Identifiers (UUID) as defined by RFC 4122, ISO/IEC 9834-8:2005, and related standards. (Some systems refer to this data type as a globally unique identifier, or GUID, instead.)

What is the datatype of UUID?

The UUID data type is considered a subtype of the STRING data type, because UUID values are displayed in their canonical textual format and, in general, behave the same as string values in the various SQL operators and expressions.

Does PostgreSQL support UUID?

UUID is an abbreviation for Universal Unique Identifier defined by RFC 4122 and has a size of 128-bit. It is created using internal algorithms that always generate a unique value. PostgreSQL has its own UUID data type and provides modules to generate them.

How does Postgres order UUID?

This means that UUIDs are not sorted by their time component in PostgreSQL. Internally, PostgreSQL uses memcmp to sort UUIDs by their memory layout. Save this answer.


2 Answers

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.

like image 50
Haleemur Ali Avatar answered Oct 20 '22 06:10

Haleemur Ali


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" ;
like image 21
JtD Avatar answered Oct 20 '22 08:10

JtD