PostgreSQL - hstore Data Type Last Updated : 12 Aug, 2024 Comments Improve Suggest changes Like Article Like Report The 'hstore' module in PostgreSQL is designed to implement a data type for storing key-value pairs within a single value. This feature is particularly useful for handling semi-structured data or cases where attributes are rarely queried individually. The 'hstore' data type excels in scenarios involving multiple rows with numerous attributes that might not be frequently accessed.Syntaxvariable_name hstore;To use the hstore data type, you first need to enable the hstore extension. It's pretty simple to enable the 'hstore' extension for using the hstore data type using the below command: CREATE EXTENSION hstore;PostgreSQL hstore Data Type ExamplesNow let's look into a few examples for better understanding. Example 1: Creating and Populating a TableFirst we create a 'books' table with 'id' as the primary key that identifies the book, the 'title' as the title of the products and 'attr' that stores attributes of the book such as ISBN, weight, and paperback. The data type of the attr column is the hstore using the below command: PostgreSQL CREATE TABLE books ( id serial PRIMARY KEY, title VARCHAR(255), attr hstore ); INSERT INTO books (title, attr) VALUES ( 'Winds Of Winter', '"paperback" => "2403", "publisher" => "Bantam Spectra/US & Voyager Books/UK", "language" => "English", "ISBN-13" => "978-1449370000", "weight" => "13.2 ounces"' ), ( 'A Dance with Dragons', '"paperback" => "2553", "publisher" => "Bantam Spectra/US & Voyager Books/UK", "language" => "English", "ISBN-13" => "978-1449370001", "weight" => "14.2 ounces"' ), ( 'A Dream of Spring', '"paperback" => "2683", "publisher" => "Bantam Spectra/US & Voyager Books/UK", "language" => "English", "ISBN-13" => "978-1449370002", "weight" => "15.7 ounces"' ); SELECT attr FROM books; Output:Example 2: Querying Specific Key-Value PairsPostgreSQL's 'hstore' module also supports the -> operator, which allows you to extract the value associated with a specific key from an 'hstore' column. For instance, to get the ISBN-13 values for all books, use the following query:SELECT attr -> 'ISBN-13' AS isbnFROM books;Output:Important Points About PostgreSQL hstore Data TypeIdeal for scenarios where the schema may change frequently or where attributes are not uniformly applicable to all rows.Useful for storing and querying semi-structured data without the need for extensive schema changes.You can query specific key-value pairs using operators like '->' to extract values and '@>' to check for the presence of specific key-value pairs.The hstore type is best for relatively small sets of key-value pairs. For more complex hierarchical data or large-scale use, consider JSONB or other types. Comment More info R rajukumar19 Follow Improve Article Tags : PostgreSQL postgreSQL postgreSQL-dataTypes Explore BasicsPostgreSQL Tutorial8 min readWhat is PostgreSQL - Introduction2 min readInstall PostgreSQL on Windows2 min readInstall PostgreSQL on Mac3 min readDatabase OperationsPostgreSQL - Create Database5 min readPostgreSQL - Loading a Database3 min readPostgreSQL ALTER DATABASE3 min readPostgreSQL - Rename Database4 min readPostgreSQL - Show Databases3 min readData TypesPostgreSQL - Data Types5 min readPostgreSQL - Boolean Data Type4 min readPostgreSQL - CHAR Data Type5 min readPostgreSQL - VARCHAR Data Type3 min readPostgreSQL - NUMERIC Data Type5 min readPostgreSQL - Date Data Type4 min readPostgreSQL - TIME Data Type4 min readPostgreSQL - JSON Data Type4 min readPostgreSQL - CREATE DOMAIN3 min readQuerying TablesPostgreSQL - SELECT3 min readPostgreSQL - ORDER BY clause2 min readPostgreSQL - WHERE clause6 min readPostgreSQL FETCH Clause4 min readPostgreSQL - IN operator4 min readPostgreSQL - HAVING clause4 min readPostgreSQL - GROUP BY clause4 min readPostgreSQL - LIKE operator5 min readPostgreSQL - BETWEEN Operator3 min readTable OperationsPostgreSQL - CREATE TABLE5 min readPostgreSQL - SELECT INTO4 min readPostgreSQL - CREATE SEQUENCE4 min readPostgreSQL - ALTER TABLE6 min readPostgreSQL - ADD COLUMN4 min readPostgreSQL - DROP COLUMN2 min readPostgreSQL - Rename Table2 min readPostgreSQL - DROP TABLE5 min readPostgreSQL - TRUNCATE TABLE4 min readPostgreSQL - Copy a Table3 min readPostgreSQL - Comparing Tables3 min readPostgreSQL - Show Tables4 min readModifying DataPostgreSQL - INSERT4 min readPostgreSQL - Insert Multiple Values in Various Rows3 min readPostgreSQL UPDATE Statement5 min readPostgreSQL - DELETE4 min readPostgreSQL - Upsert4 min readConditionalsPostgreSQL - CASE3 min readPostgreSQL COALESCE5 min readPostgreSQL - NULLIF() Function4 min readPostgreSQL - CAST3 min readControl FlowPostgreSQL - IF Statement5 min readPostgreSQL - CASE Statement4 min readPostgreSQL - Loop Statement3 min readPostgreSQL - While Loops4 min readPostgreSQL - Exit Statement3 min readPostgreSQL - Continue3 min readTransactions & ConstraintsPostgreSQL - Transactions4 min readPostgreSQL - COMMIT4 min readPostgreSQL - Primary Key4 min readPostgreSQL - Foreign Key5 min readPostgreSQL - CHECK Constraint2 min readPostgreSQL - UNIQUE Constraint3 min readPostgreSQL - NOT NULL Constraint3 min readJOINS & SchemasPostgreSQL - Joins5 min readPostgreSQL - LEFT JOIN5 min readPostgreSQL - INNER JOIN2 min readPostgreSQL - FULL OUTER JOIN4 min readPostgreSQL - SELF JOIN4 min readPostgreSQL - Schema5 min readPostgreSQL - CREATE SCHEMA5 min readPostgreSQL - DROP SCHEMA4 min readPostgreSQL - ALTER SCHEMA3 min read Like