Creating an Auto Generating GUID Primary Key in PostgreSQL 9.x

Here's a quick step-by-step on creating an auto generating GUID Primary Key in PostgreSQL 9.3. The table we're creating is a simple logging table.

Ignore the uuid_generate_v4() default value for the log_guid field. We'll cover that shortly. Our goal is to enter rows in the log table with simple insert statements like the following.

To auto generate the log_guid primary key we must first install a module not included in PostgreSQL 9.x by default, uuid-ossp. Here's the uuid-ossp module page in the PostgreSQL manual for more info. We install it in PostgreSQL with the following SQL command.

CREATE EXTENSION "uuid-ossp";

To confirm the module is installed, use SELECT * FROM pg_extension;

With the uuid-ossp module installed, we we set the default value of the GUID Primary Key to uuid_generate_v4(). We saw that above in the table GUI dialog. Here it is in SQL.

And that's it! fe60a141-7a7b-4f4d-aaa8-6d42563f5e51!

Posted November 03, 2014 08:52 PM EST

More Like This Post