PostgreSQL: How to store UUID value?

postgresql, uuid

Solution

The whole point of UUIDs is that they are automatically generated because the algorithm used virtually guarantees that they are unique in your table, your database, or even across databases. UUIDs are stored as 16-byte datums so you really only want to use them when one or more of the following holds true:

- You need to store data indefinitely, forever, always.

- When you have a highly distributed system of data generation (e.g. `INSERTS` in a single "system" which is distributed over multiple machines each having their own local database) where central ID generation is not feasible (e.g. a mobile data collection system with limited connectivity, later uploading to a central server).

- Certain scenarios in load-balancing, replication, etc.

If one of these cases applies to you, then you are best off using the UUID as a primary key and have it generated automagically:

CREATE TABLE test (
   uno     uuid PRIMARY KEY DEFAULT uuid_generate_v4(),
   name    text,
   address text
);

Like this you never have to worry about the UUIDs themselves, it is all done behind the scenes. Your `INSERT` statement would become:

INSERT INTO test (name, address) VALUES ('abc','xyz') RETURNING uno;

With the `RETURNING` clause obviously optional if you want to use that to reference related data.

Problem

I am trying to store UUID value into my table using PostgreSQL 9.3 version. Example: ``` create table test ( uno UUID, name text, address text ); insert into test values(1,'abc','xyz'); ``` Note: How to store integer value into UUID type?

Original source