![]() PostgreSQL allows you store and compare UUID values but it does not include functions for generating the UUID values in its core. To stores UUID values in the PostgreSQL database, you use the UUID data type. The following shows some examples of the UUID values: 40e6215d-b5c6-4896-987c-f30f3678f608Ĭode language: SQL (Structured Query Language) ( sql )Īs you can see, a UUID is a sequence of 32 digits of hexadecimal digits represented in groups separated by hyphens.īecause of its uniqueness feature, you often found UUID in the distributed systems because it guarantees a better uniqueness than the SERIAL data type which generates only unique values within a single database. A UUID value is 128-bit quantity generated by an algorithm that make it unique in the known universe using the same algorithm. UUID stands for Universal Unique Identifier defined by RFC 4122 and other related standards. The application changes every PK / FK integer column to UUID, keeping the data and relationships.Summary: in this tutorial, you will learn about the PostgreSQL UUID data type and how to generate UUID values using a supplied module. That was a big task, but it was made easier because I ended up building an application that makes this change easer. The problem then was to convert the cloud application tables by turning the INT columns (PKs and FKs) into UUID columns without losing the table information. With this in mind we chose to switch cloud application PKs / FKs to UUID, since data coming from the desktop application had a UUID column. When migrating this data we had to change the values of the PKs / FKs for new sequences as the sequences could clash between the values of the desktop application and the values of the cloud application. This solved some of the problems because one of our desktop applications would have their data migrated to another cloud application, this cloud application also used PK / FK columns. ![]() In this scenario we decided to rewrite the APIs so that the UUID column was used. In some applications we have to find the ID (of the target application) to be sent via the API call, on the other hand our database tables, in all our applications have, in addition to the sequential PK / FK column, a UUID column, which was not used in API calls. This has worked perfectly, but in recent years when creating cloud applications where information will be exchanged between applications and we will have integrations between various applications developed by us, we realized that the use of sequential IDs in our APIs ended up creating an effort. You can now use the built-in function gen_random_uuid() to get a version 4 random UUID.įor many years I developed applications for databases using PKs and FKs as numerical sequential values. You can create a primary key like this: id uuid PRIMARY KEY DEFAULT uuid_generate_v4()Īnd then you will never have to worry about it anymore. The version 4 is then the best choice as it has 122 random bits (the other 6 are used for identification of the version). If you want to protect a table with user data from snooping hackers that are trying to guess other IDs, then the uuid type is an excellent choice. If you want to mask the ID of a certain user from other users, you should carefully manage the table privileges and/or hash the ID using - for instance - md5(). However, I don't see how this relates to the masking of a user ID. You can use a uuid as a primary key, just like most any other data type. A sequence is more efficient than a uuid because it is 8 bytes instead of 16 for the uuid. ![]() ![]() A sequence in PostgreSQL does exactly the same as AUTOINCREMENT in MySQL. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |