Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to generate uuid with PostgreSQL 8.4.4 on Ubuntu 10.04?

I am running PostgreSQL 8.4.4 with Ubuntu 10.04.

I am trying to generate uuid but can't find a way to do it.

I do have the uuid-ossp.sql in /usr/share/postgresql/8.4/contrib/uuid-ossp.sql

When I try this is what I get :

postgres=# SELECT uuid_generate_v1();
ERROR:  function uuid_generate_v1() does not exist
LINE 1: SELECT uuid_generate_v1();
               ^
HINT:  No function matches the given name and argument types. You might need to add explicit type casts.

Any idea ?

like image 802
Spredzy Avatar asked Sep 30 '10 14:09

Spredzy


People also ask

Can Postgres generate UUID?

Unfortunately, while PostgreSQL is great for storing and comparing UUID data, it lacks capabilities for creating UUID values in its core. Instead, it relies on third-party modules to create UUIDs using specified techniques.

What is UUID in Postgres?

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 store UUID?

PostgreSQL allows you store and compare UUID values but it does not include functions for generating the UUID values in its core. Instead, it relies on the third-party modules that provide specific algorithms to generate UUIDs.


2 Answers

The stuff in contrib aren't run automatically. You have to run it yourself to install the functions. I don't know about the 8.4 version, but in the 8.3 version it appears to only install it per-database, so open up the database you're using in psql and issue the command \i /usr/share/postgresql/8.4/contrib/uuid-ossp.sql

like image 78
Paul Tomblin Avatar answered Sep 24 '22 05:09

Paul Tomblin


I saw this in my PostgreSQL travels. It requires the pgcrypto contrib module.

CREATE OR REPLACE FUNCTION generate_uuid() RETURNS UUID AS
$$
SELECT ENCODE(GEN_RANDOM_BYTES(16), 'hex')::UUID
$$ LANGUAGE SQL IMMUTABLE;
like image 34
Gary Chambers Avatar answered Sep 22 '22 05:09

Gary Chambers