Javatpoint Logo
Javatpoint Logo

PostgreSQL UUID

In this section, we are going to understand the working of the PostgreSQL UUID data type. And we also see examples of the UUID data type, and we also see some how to create UUID values with the help of a supplied module.

What is PostgreSQL UUID Data Type?

In PostgreSQL, the next data type is a Universally Unique Identifier, which is also known as UUID specified by RFC 4122 and another connected standard.

The UUID value is a 128-bit long value created by an algorithm, making it unique in the known universe with the help of a similar algorithm to identify the information.

Let us see some sample examples of the UUID values as we can see in the below representation:

As we can see in the above examples, a UUID is an arrangement of 32 digits of hexadecimal digits along with four hyphens (-). A UUID may be nil, where all bits are set to zero.

We frequently identify the UUID in the distributed systems for its unique feature. It also makes sure a better individuality as compared to the PostgreSQL SERIAL data type, which creates unique values within a single database.

Note: In the PostgreSQL database, we can use the UUID data type to store the UUID values and offers the modules to produce them.

How to create UUID values in PostgreSQL

PostgreSQL enables us to store and equate the UUID values, but it does not contain the functions, and creates the UUID values in its core.

And rather than it depends on the third-party modules that deliver the particular algorithms to create the UUIDs, such as the uuid-ossp module contains some accessible functions, which perform standard algorithms for creating UUIDs.

We will use the following CREATE EXTENSION command to install the uuid-ossp module in the Javatpoint Database.

Output

After implementing the above command, we will get the below message: the uuid_ossp has been installed successfully with the Create Extension command.

PostgreSQL UUID

We have used the IF NOT EXISTS clause in the above command, which provides us to avoid re-installing the module.

We can use the uuid_generate_v1() function to create the UUID values depends on the current timestamp, the grouping of the computer's MAC address, and a random value:

Output

We will get the following result on executing the above command, which displays UUID's value created by the uuid_generate_v1() function.

PostgreSQL UUID

In the following command, we will use the uuid_generate_v4() function to create a UUID value uniquely depends on random numbers:

Output

After successfully executing the above command, we will get the following result, which displays UUID's value created by the uuid_generate_v1() function.

PostgreSQL UUID

Example of PostgreSQL UUID data type

Let us see one sample examples to understand how the PostgreSQL UUID data type works.

We are creating one new table as the Client and UUID columns with the CREATE command's help and inserting some values using the INSERT command.

The Client table contains the various columns such as Client_id, client_first_name, client_last_name, client_email_id, and client_address.

For the client_id column, we use the UUID data type, which is also the primary key. Furthermore, the primary fundamental column values will be created automatically with the uuid_generate_v4() function.

To create a Client table into a similar database which is Javatpoint, where we install the uuid_ossp module, we use the CREATE command as we can see in the following command:

Output

On executing the above command, we will get the following message, which displays that the Client table has been created successfully.

PostgreSQL UUID

In the above command, we have used the UUID data type for the Client_id column where the Client_id column has a default value given by the uuid_generate_v4() function.

Hence, the PostgreSQL will call the uuid_generate_v4() function to create the Client_id value if we insert a new row without defining the Client_id column's value.

When the Client table is created successfully, we will insert some values into it with the INSERT command's help.

Output

After implementing the above command, we will get the following message window, which displays that the specified values have been inserted successfully into the Client table.

PostgreSQL UUID

After creating and inserting the Client table's values, we will use the SELECT command to return all rows of the Client table:

Output

After successfully implementing the above command, we will get the below output, which displays all the data present in the Client table:

PostgreSQL UUID

As we can see in the above screenshot that the Client_id column has been populated by the UUID values created by the uuid_generate_v4() function.

Overview

In the PostgreSQL UUID data type section, we have learned the following topics:

  • The PostgreSQL UUID data type is used to store the UUID values for a specified column.
  • We can use the CREATE Extension command to install the uuid-ossp module to creates the UUID values.
  • We can use the uuid_generate_v4() functionto automatically retrieve the UUID values for the table's particular column.

Next TopicPostgreSQL JSON




Youtube For Videos Join Our Youtube Channel: Join Now

Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA