Skip to content

Commit 31f86b9

Browse files
committed
Add Generating UUIDs With pgcrypto as a postgres til
1 parent 87f2fed commit 31f86b9

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ variety of languages and technologies. These are things that don't really
77
warrant a full blog post. These are mostly things I learn by pairing with
88
smart people at [Hashrocket](http://hashrocket.com/).
99

10-
_486 TILs and counting..._
10+
_487 TILs and counting..._
1111

1212
---
1313

@@ -240,6 +240,7 @@ _486 TILs and counting..._
240240
- [Fizzbuzz With Common Table Expressions](postgres/fizzbuzz-with-common-table-expressions.md)
241241
- [Generate A UUID](postgres/generate-a-uuid.md)
242242
- [Generate Series Of Numbers](postgres/generate-series-of-numbers.md)
243+
- [Generating UUIDs With pgcrypto](postgres/generating-uuids-with-pgcrypto.md)
243244
- [Get The Size Of A Database](postgres/get-the-size-of-a-database.md)
244245
- [Get The Size Of A Table](postgres/get-the-size-of-a-table.md)
245246
- [Get The Size Of An Index](postgres/get-the-size-of-an-index.md)
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Generating UUIDs With pgcrypto
2+
3+
If you check out the docs for the [`uuid-ossp`
4+
extension](https://www.postgresql.org/docs/current/static/uuid-ossp.html),
5+
you'll come across the following message.
6+
7+
> The OSSP UUID library... is not well maintained, and is becoming
8+
> increasingly difficult to port to newer platforms.
9+
10+
A little bit later, it says:
11+
12+
> If you only need randomly-generated (version 4) UUIDs, consider using the
13+
> gen_random_uuid() function from the pgcrypto module instead.
14+
15+
So, if we are using the UUID data type and only need to generate random
16+
UUIDs, we can rely on the [`pgcrypto`
17+
extension](https://www.postgresql.org/docs/current/static/pgcrypto.html). It
18+
comes with the `gen_random_uuid()` function which generates random v4 UUIDs.
19+
20+
```sql
21+
> create extension "pgcrypto";
22+
CREATE EXTENSION
23+
24+
> select gen_random_uuid();
25+
gen_random_uuid
26+
--------------------------------------
27+
0a557c31-0632-4d3e-a349-e0adefb66a69
28+
29+
> select gen_random_uuid();
30+
gen_random_uuid
31+
--------------------------------------
32+
83cdd678-8198-4d56-935d-d052f2e9db37
33+
```

0 commit comments

Comments
 (0)