From 31f86b9e4b0e6c238babe74c307736dba5edfe23 Mon Sep 17 00:00:00 2001 From: jbranchaud Date: Tue, 22 Nov 2016 14:27:08 -0600 Subject: [PATCH] Add Generating UUIDs With pgcrypto as a postgres til --- README.md | 3 +- postgres/generating-uuids-with-pgcrypto.md | 33 ++++++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 postgres/generating-uuids-with-pgcrypto.md diff --git a/README.md b/README.md index d218de7..f5a9301 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ variety of languages and technologies. These are things that don't really warrant a full blog post. These are mostly things I learn by pairing with smart people at [Hashrocket](http://hashrocket.com/). -_486 TILs and counting..._ +_487 TILs and counting..._ --- @@ -240,6 +240,7 @@ _486 TILs and counting..._ - [Fizzbuzz With Common Table Expressions](postgres/fizzbuzz-with-common-table-expressions.md) - [Generate A UUID](postgres/generate-a-uuid.md) - [Generate Series Of Numbers](postgres/generate-series-of-numbers.md) +- [Generating UUIDs With pgcrypto](postgres/generating-uuids-with-pgcrypto.md) - [Get The Size Of A Database](postgres/get-the-size-of-a-database.md) - [Get The Size Of A Table](postgres/get-the-size-of-a-table.md) - [Get The Size Of An Index](postgres/get-the-size-of-an-index.md) diff --git a/postgres/generating-uuids-with-pgcrypto.md b/postgres/generating-uuids-with-pgcrypto.md new file mode 100644 index 0000000..d91f624 --- /dev/null +++ b/postgres/generating-uuids-with-pgcrypto.md @@ -0,0 +1,33 @@ +# Generating UUIDs With pgcrypto + +If you check out the docs for the [`uuid-ossp` +extension](https://www.postgresql.org/docs/current/static/uuid-ossp.html), +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](https://www.postgresql.org/docs/current/static/pgcrypto.html). It +comes with the `gen_random_uuid()` function which generates random v4 UUIDs. + +```sql +> create extension "pgcrypto"; +CREATE EXTENSION + +> select gen_random_uuid(); + gen_random_uuid +-------------------------------------- + 0a557c31-0632-4d3e-a349-e0adefb66a69 + +> select gen_random_uuid(); + gen_random_uuid +-------------------------------------- + 83cdd678-8198-4d56-935d-d052f2e9db37 +```