PostgreSQL hstore

In this section, we are going to understand the working of the PostgreSQL hstore data type, examples of the hstore data type, and we also see the example of the hstore with WHERE clause, which helps us to handle the hstore data values more resourcefully.

We will understand how we can add, update, deleting the key-values pairs in the PostgreSQL hstore data type.

We will discuss the following operators, such as -> Operator (to select the data for a specific value), ? Operator (to check for an explicit key in an hstore column), @> operator (to check the key-value pair), ?& and ?|Operator ( to select the rows whose hstore column is having various keys).

And also see the usage of avals(), svals(), akeys() skeys(), EACH(), hstore_to_json() functions.

What is PostgreSQL hstore Data Type?

In PostgreSQL, the next data type that we are going to understand is the hstore, which is used to store the key-value pair in the unit value. It is always the right approach for various cases, like semi-structured data or rows, with serval attributes, which are uncommonly enquired.

Note: Basically, the keys and values are the text strings.

Syntax of PostgreSQL hstore data type

The syntax of the PostgreSQL hstore data type is as follows:

Before we start learning the hstore data type, we must first allow the hstore extension, which loads the contrib module for our PostgreSQL illustration.

Enable PostgreSQL hstore extension

To enable the PostgreSQL hstore extension for our PostgreSQL design, we can use the CREATE EXTENSION command in the Organization database, as we can see in the following statement:

Output

After executing the above command, we will get the below message: the hstore extension has been installed successfully with the Create Extension command.

PostgreSQL hstore

Example of PostgreSQL hstore data type

Let us see one sample example to understand how the PostgreSQL hstore data type works.

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

The Movie table contains the various columns such as Movie_id, Movie_name, Movie_attr and that have the following properties:

  • The Movie_idis the primary key, which is used to find the movies.
  • Movie_nameis other name of the Movies
  • And the Movie_attrcolumn is used to contain the attributes of the Movies, for example, rating, movie_genres, language, running_time, and release year.

For the Movie_attr column, we have used the hstore data type.

To create a Movie table into a similar database which is Organization, where we enable the hstore extension, 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 Movie table has been created successfully.

PostgreSQL hstore

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

Inserting a value into PostgreSQL hstore column

To insert the value into the hstore column, we are using the INSERT command as shown in the below statement:

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 Movie table.

PostgreSQL hstore

Note: As we can see in the above insert command, the values we inserted into the hstore column are a list of comma-separated key => value pairs. And both keys and values are quoted using double quotes (").

After creating and inserting the values in the Movie table, we will use the SELECT command to retrieve all data from the Movie table:

Selecting values from an hstore column

Selecting the values from an hstore column is equal to selecting a value from a column with native data type with the help of the SELECT command, as shown below:

Output

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

PostgreSQL hstore

Selecting data for a specific key

PostgreSQL hstore data type allows us to use the -> operator to select a particular key's data values from an hstore column (Movie_Attr).

In the below example, we are using the -> operator to identify the running_time of all available movies in the Movie table, as shown in the below command:

Output

We will get the following output after implementing the above command, which displays each movie's running_time from the Movie table.

PostgreSQL hstore

Using hstore data value in the WHERE clause

To filter the rows whose values of the hstore column match the input value. So, for this case, we can use the -> operator in the WHERE clause.

Let us see one sample example for our better understanding:

In the below example, we are trying to get Movie_name and Movie_genres of the movie, which has Relases_year value matches 2019:

Output

After implementing the above command, we will get the value of the hstore column using the WHERE clause in the output, as shown below:

PostgreSQL hstore

Retrieve all values from an hstore column

To retrieve all the data values from the hstore column, we can use the avals() function in arrays.

In the following example, we are getting all the values from an hstore column in the Movie table with the help of the avals() function:

Output

After executing the above command, we will get the following output, which shows all the Moive_attr values available in the Movie table:

PostgreSQL hstore

OR

If we want to retrieve the output as a set, we can use the svals() function.

In the following command, we are using the svals() function instead of avals() to get the output as a set:

Output

On implementing the above command, we will get the following output as a set:

PostgreSQL hstore

Retrieve all keys from an hstore column

Like we can get all the values from the hstore column using avals() and svals() function, we can use the akeys() function for retrieving all keys from an hstore column:

Output

On implementing the above command, we will get the following output, which shows all the key values of the Movie_attr column available in the Movie table:

PostgreSQL hstore

OR

If we want that the PostgreSQL retrieve the output as a set, we can use the skey() function.

In the following command, we are using the skey() function instead of akeys() to get the output as a set:

Output

After executing the above command, we will get the following output as a set:

PostgreSQL hstore

Modify hstore data to sets

We can use the EACH() function to modify the hstore data to sets as we can see in the following command:

Output

We will get the following output after successfully executing the above command, which displays the modified hstore data into sets:

PostgreSQL hstore

Modifying the hstore data to JSON

To change the hstore data to JSON, PostgreSQL offers the hstore_to_json() function.

In the below command, we are using the hstore_to_json() function for converting the hstore data value into json:

Output

We will get the following result after executing the above command, which displays that we have successfully modified the hstore data to the json:

PostgreSQL hstore

Add key-value pairs to current rows

We can add a new key-value pair to current rows with the hstore column.

For example, we can add the Country key to the Movie_attr column of the Movie table as we can see in the following command:

Output

After executing the above command, we will get the below result, which shows that the three columns have been updated successfully.

PostgreSQL hstore

Now, if we want to see that the "Country"=>"United States" value has been updated successfully.

Output

We will get the following result on implementing the above command, which displays that the Country Movie_attr value United States has been updated for the specified values.

PostgreSQL hstore

Update current key-value pair

We can update the current key-value pair with the help of the UPDATE command.

For example: In the below command, we will update the value of the "Country" key to "USA".

Output

After implementing the above command, we will get the below message window, which displays that the values have been updated successfully.

PostgreSQL hstore

Deleting the current key-value pair

PostgreSQL provides us with deleting the current key-value pair from an hstore column.

Let us see one sample example for our better understanding:

In the below command, we are deleting the "Country"=>"USA" key-value pair in the Movie_ attr column.

Output

After implementing the above command, we will get the below message window, which says that specified values have been updated successfully.

PostgreSQL hstore

Check for an explicit key in the hstore column

The ? operator is used to check for an explicit key in an hstore column within the WHERE clause.

In the following example, the below command retrieves all rows with Movie_attr, which contains key Movie_genres.

Output

We will get the following result on implementing the above command, which shows the specified key in the hstore column.

PostgreSQL hstore

Check for a key-value pair

We can use the @> operator to SELECT the hstore key-value pair.

Let us see one example to see the usage of the @> operator:

The below command is used to return all the rows containing a key-value pair from the Movie_attr column, and also matches "Language"=>"English".

Output

After successfully executing the above command, we will get the below result, representing the hstore key-value pair.

PostgreSQL hstore

Selecting rows which having various specified keys

The ?& operator is used to select the rows whose hstore column having various keys.

In the following example, we can get Movie where Movie_attr column contains both running_time and rating keys:

Output

After executing the above command, we will get the below output:

PostgreSQL hstore

Note: We can also use ?| operator in the ?& operator's place to check if a row whose hstore column contains any key from a list of keys.

In the following command, we are using ?| operator instead of ?& to get those rows whose hstore column contains any key from a list of keys:

Output

After implementing the above command, we will get a similar result as compared to the output of ?& operator:

PostgreSQL hstore

Overview

In the PostgreSQL hstore data type section to the most useful operations, we can perform against the hstore data type.

We have learned the following topics:

  • The PostgreSQL hstore data type is used to store the key-value pair in the unit value.
  • We enabled the PostgreSQL hstore using the Create Extension
  • With the help of the -> operator, we can select the data for a specific value.
  • We have used the hstore data type within the WHERE
  • We can also retrieve the values from the hstore column using the avals() function.
  • And to get the output as a set, we can use the svals() function.
  • We can also return all the keys from the hstore value with the help of akeys()
  • And to return the output as a set, we can use the skeys() function.
  • We have used the EACH() function for modifying the hstore data into sets.
  • In PostgreSQL, the hstore data type section, we can also change the hstore data to json using hstore_to_json() function.
  • We can also add, update, delete the key-values pairs in the PostgreSQL hstore data type.
  • We have also discussed the following operators, such as, ? (to check for an explicit key in an hstore column), @>(to check the key-value pair), ?&, and ?|( to select the rows whose hstore column having various keys).

Next TopicPostgreSQL Array




Contact US

Email:[email protected]

PostgreSQL hstore
10/30