Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PostgreSQL function gen_random_uuid() not working

I get function get_random_uuid() does not exist error when running this SQL:

ALTER TABLE "posts" ALTER COLUMN "id" SET DEFAULT gen_random_uuid()

I can see pgcrypto module listed in this query select * from pg_available_extensions;. I'm using PostgreSQL 9.4.5 on Ubuntu 14.04.

Did I miss something?

like image 878
Dida Avatar asked Mar 12 '16 15:03

Dida


People also ask

What is Gen_random_uuid ()?

gen_random_uuid() is a system function which generates a random UUID. gen_random_uuid() was added in PostgreSQL 13.


1 Answers

You need to load the pgcrypto extension in the current database/schema with

CREATE EXTENSION pgcrypto; 

like this (tested with PostgreSQL 12):

# SELECT gen_random_uuid(); ERROR:  function gen_random_uuid() does not exist LINE 1: select gen_random_uuid();                ^ HINT:  No function matches the given name and argument types. You might need to add explicit type casts. # CREATE EXTENSION pgcrypto; CREATE EXTENSION # SELECT gen_random_uuid();            gen_random_uuid             --------------------------------------  19a12b49-a57a-4f1e-8e66-152be08e6165 (1 row) 
like image 194
okket Avatar answered Sep 17 '22 00:09

okket