File tree 2 files changed +35
-1
lines changed 2 files changed +35
-1
lines changed Original file line number Diff line number Diff line change @@ -7,7 +7,7 @@ variety of languages and technologies. These are things that don't really
7
7
warrant a full blog post. These are mostly things I learn by pairing with
8
8
smart people at [ Hashrocket] ( http://hashrocket.com/ ) .
9
9
10
- _ 486 TILs and counting..._
10
+ _ 487 TILs and counting..._
11
11
12
12
---
13
13
@@ -240,6 +240,7 @@ _486 TILs and counting..._
240
240
- [ Fizzbuzz With Common Table Expressions] ( postgres/fizzbuzz-with-common-table-expressions.md )
241
241
- [ Generate A UUID] ( postgres/generate-a-uuid.md )
242
242
- [ Generate Series Of Numbers] ( postgres/generate-series-of-numbers.md )
243
+ - [ Generating UUIDs With pgcrypto] ( postgres/generating-uuids-with-pgcrypto.md )
243
244
- [ Get The Size Of A Database] ( postgres/get-the-size-of-a-database.md )
244
245
- [ Get The Size Of A Table] ( postgres/get-the-size-of-a-table.md )
245
246
- [ Get The Size Of An Index] ( postgres/get-the-size-of-an-index.md )
Original file line number Diff line number Diff line change
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
+ ```
You can’t perform that action at this time.
0 commit comments