Home / Genel / Postgres de UUID yani GUID yaratmak

Postgres de UUID yani GUID yaratmak

Bir instert sorgusu yazarken ihtiyacim oldu, bu hep lazim olur diye buraya not alayim dedim.

https://stackoverflow.com/questions/12505158/generating-a-uuid-in-postgres-for-insert-statement


Without extensions (cheat)

If you need a valid v4 UUID

SELECT uuid_in(overlay(overlay(md5(random()::text || ':' || random()::text) placing '4' from 13) placing to_hex(floor(random()*(11-8+1) + 8)::int)::text from 17)::cstring);
enter image description here
  • Thanks to @Denis Stafichuk @Karsten and @autronix

Or you can simply get UUID-like value by doing this (if you don’t care about the validity):

SELECT uuid_in(md5(random()::text || random()::text)::cstring);

output>> c2d29867-3d0b-d497-9191-18a9d8ee7830

(works at least in 8.4)

Bir yanıt yazın

E-posta adresiniz yayınlanmayacak. Gerekli alanlar * ile işaretlenmişlerdir