Today I Learned

A Hashrocket project

Ready to join Hashrocket? Find Openings here and apply today.

Generating UUIDs With pgcrypto

If you check out the docs for the uuid-ossp extension, you’ll come across the following message.

The OSSP UUID library… is not well maintained, and is becoming increasingly difficult to port to newer platforms.

A little bit later, it says:

If you only need randomly-generated (version 4) UUIDs, consider using the gen_random_uuid() function from the pgcrypto module instead.

So, if we are using the UUID data type and only need to generate random UUIDs, we can rely on the pgcrypto extension. It comes with the gen_random_uuid() function which generates random v4 UUIDs.

> create extension "pgcrypto";

> select gen_random_uuid();

> select gen_random_uuid();
Looking for help? Hashrocket developers believe that data quality is as important as code quality. We enjoy all the challenges of relational databases, from finding the fastest index, to structuring data to fit the needs of an application. We're eager to share our experiences; check out PG Casts, our series of free PostgreSQL screencasts.