EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home Data Science Data Science Tutorials PostgreSQL Tutorial hstore in PostgreSQL
 

hstore in PostgreSQL

Sohel Sayyad
Article bySohel Sayyad
EDUCBA
Reviewed byRavi Rathore

Updated May 15, 2023

hstore in PostgreSQL

 

 

Introduction to hstore in PostgreSQL

In order to consider the key-value pair as a single entity, the PostgreSQL hstore module implements the hstore data type, which can be used in various cases, like semi-structured data or a row with multiple attributes we cannot try to fetch very often. The data type of keys and values is a string. The PostgreSQL hstore data type is a similar dictionary we use with other programming languages; The PostgreSQL hstore is specific to the column. It is not necessary to define the keys beforehand.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

Syntax

column_name hstore

Explanation: The name of the column whose data type will be store.

How hstore data type works in PostgreSQL?

We need to enable the hstore extension in order to use the hstore data type. We can load the contrib module to the PostgreSQL instance with the help of the hstore extension. Syntax to create the extension for hstore is as follows:

CREATE EXTENSION hstore;

Hstore store the key and value pair where we have to add a double quote around both key and value fields as follows:

"<key>":"<value>"

The PostgreSQL hstore data type is used if the column’s value does not fit into the relational column. The PostgreSQL hstore column works dynamically, which means you do not need to specify the key prior to the table creation; you can create a table with the hstore column and insert values with different keys later.

Examples to Implement hstore data type in PostgreSQL

In order to understand the examples of the hstore data type, we will create a table named ’employee’ containing the hstore data type.

Create a table ’employee’ with hstore data type using CREATE TABLE statement as follows:

CREATE TABLE employee (
id serial primary key,
name VARCHAR (255),
data hstore
);

the data column will have the properties of the employee, like job type, salary, and contact number. The data column of the employee table is of the hstore data type.

Now, we will insert rows into the employee table with the help of the following INSERT INTO statement as follows:

INSERT INTO employee (name, data)
VALUES
(
'Oliver Jake',
'"contact_number" => "9912002430",
"salary" => "30000",'
);

Here you can see the list of comma-separated key-value pairs data is inserted into the hstore column. Illustrate the content of the employee table using the following SELECT statement and snapshot:

SELECT * FROM employee;

hstore in PostgreSQL1

Now we will insert another row using the INSERT INTO statement as follows.

INSERT INTO employee (name, data)
VALUES
(
'Jacob John',
'"contact_number" => "9912002440",
"salary" => "40000",'
);

Illustrate the content of the employee table using the following SELECT statement and snapshot:

SELECT * FROM employee;

hstore in PostgreSQL2

Example #1 – Retrieve the data from hstore column

We can fetch the data from the hstore column by using the following statement.

Code:

SELECT
data
FROM
employee;

Output:

hstore in PostgreSQL3

Example #2 – Retrieve the data for the specific key of hstore

In order to fetch the particular key of the data column of the hstore type, we can use the arrow (->) operator as follows:

Code:

SELECT
data -> 'salary' AS salary
FROM
employee;

Output:

hstore in PostgreSQL4

Example #3 – Where clause with key-value

We can add a condition in the where clause in order to filter the rows with the help of the arrow (->) operator:

Code:

SELECT
name,
data -> 'contact_number' AS Contact
FROM
employee
WHERE
data -> 'salary' = '30000';

Output:

hstore in PostgreSQL5

Example #4 – INSERT a key-value pair

We can add a key-value pair by using the hstore column. Here we will add the job type in the data column of the Employee table by using the following statement:

Code:

SELECT
name,
data -> 'job_type' AS JobType
FROM
Employee;

Output:

hstore in PostgreSQL6

Example #5 – Update the key-value pair

We can modify the key-value pair of the hstore column. Use the following statement to update the value of the “job_type” key to “Part”.

Code:

UPDATE employee
SET data = data || '"job_type"=>"Part"' :: hstore;

Output:

Update the key-value pair

Example #6 – Delete the key-value pair

We can delete the key-value pair from the data column, which is of type store, by using the following statement.

Here we will delete the key-value pair “job_type” => “Part” in the data column:

Code:

UPDATE employee
SET data = delete(data, 'job_type');

Output:

Delete the key-value pair

Example #7 – Fetch all keys stored in hstore column

In order to fetch all keys stored in hstore type column, we have to use the keys() function or skey() function as follows:

Code:

SELECT
akeys (data)
FROM
employee;

Output:

keys stored

The skey() function provided by PostgreSQL is used to fetch the result as a set:

Code:

SELECT
skeys (data)
FROM
employee;

Output:

hstore in PostgreSQL10

Example #8 – Fetch all values from the data column

Similar to the keys, we can fetch all values from the data column, which is of hstore type, by using the avals() function or svals() function provided by PostgreSQL:

Code:

SELECT
avals (data)
FROM
employee;

Output:

data column

The svals() function provided by PostgreSQL is used to fetch the result as a set:

Code:

SELECT
svals (data)
FROM
employee;

Output:

hstore in PostgreSQL12

Example #9 – Convert hstore data to JSON data

We can use PostgreSQL’s hstore_to_json() function for converting the hstore data to JSON:

Code:

SELECT
name,
hstore_to_json (data) json_data
FROM
employee;

Output:

JSON data

Example #10 – Convert hstore data to sets

We can use PostgreSQL’s each() function for converting the hstore data to sets.

Code:

SELECT
name,
(EACH(data) ).*
FROM
employee;

Output:

sets

Conclusion

We hope from the above article you have understood how to use the PostgreSQL hstore data type and how the PostgreSQL hstore data type works to store the data in key-value pair. Also, we have added some examples of the PostgreSQL hstore to understand it in detail.

Recommended Articles

We hope that this EDUCBA information on “hstore in PostgreSQL” was beneficial to you. You can view EDUCBA’s recommended articles for more information.

  1. PostgreSQL IF Statement
  2. PostgreSQL For Loop
  3. Primary Key PostgreSQL
  4. PostgreSQL List Tables

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

🚀 Limited Time Offer! - ENROLL NOW