Default UUID in Postgres

Started by Swaminathan Saikumaralmost 18 years ago2 messagesgeneral
Jump to latest
#1Swaminathan Saikumar
swami@giveexam.com

Hello,

I created a Postgres table with a UUID. I want the UUID to be populated by
default.

Sample table:
CREATE TABLE "t"
(
token uuid NOT NULL,
CONSTRAINT unique_token UNIQUE (token)
);

I tried out this post:
http://archives.postgresql.org/pgsql-general/2006-08/msg01452.php
CREATE TABLE t (id UUID DEFAULT 'uuid(4)' PRIMARY KEY);

and got an error that uuid(4) is undefined.

I then tried out:
http://www.postgresql.org/docs/8.3/static/uuid-ossp.html
using uuid_generate_v4() for the default.

That didn't work as well.

Can anyone help please?

Thanks!

#2Christophe Pettus
xof@thebuild.com
In reply to: Swaminathan Saikumar (#1)
Re: Default UUID in Postgres

On Jul 9, 2008, at 4:32 PM, Swaminathan Saikumar wrote:

Hello,

I created a Postgres table with a UUID. I want the UUID to be
populated by default.

PostgreSQL doesn't have built-in functions for generating UUIDs, but
there is a module in contrib that will do so:

http://www.postgresql.org/docs/current/static/datatype-uuid.html