I have following columns:
key | value
-----------
foo | 1
bar | 2
Is there a recommended way to turn this into hstore
?
{foo => 1, bar => 2}
aggregatehstorepostgresql
I have following columns:
key | value
-----------
foo | 1
bar | 2
Is there a recommended way to turn this into hstore
?
{foo => 1, bar => 2}
Best Answer
Just use the function
hstore()
.There are several overlaoded versions of that functions. One takes a single text array with keys and values. Another one takes two text arrays, one with keys, the other one with values. Either works for you. Demo:
option1
requires Postgres 9.5 or later, wherearray_agg()
for multidimensional arrays was introduced. For older versions see:If
value
is not typetext
, you may need the cast I added:value::text
The additional module hstore has to be installed in the database, of course.
Related: