0

Is there any way to create an array in PostgreSQL which contains multiple data types in form type:value?

For example, one of the table records should be an array with values height:190, color:black etc. If it isn't possible with arrays, how could I mannage this other way?

4
  • you are probably looking for hstore or json Commented May 9, 2017 at 10:07
  • you can use json as column type. You read about the same on postgres official page. Commented May 9, 2017 at 10:08
  • "how could I manage this other way?" -- Captain Obvious here ... By using multiple columns, and possibly an associated table. (If you don't want to go the JSON route, that is.) Commented May 9, 2017 at 10:08
  • This data structure called associative array BTW. Commented May 9, 2017 at 10:50

1 Answer 1

1

https://www.postgresql.org/docs/current/static/hstore.html

This module implements the hstore data type for storing sets of key/value pairs within a single PostgreSQL value

t=# select ('height=>190, color=>black')::hstore;
              hstore
-----------------------------------
 "color"=>"black", "height"=>"190"
(1 row)

https://www.postgresql.org/docs/current/static/datatype-json.html

JSON data types are for storing JSON (JavaScript Object Notation) data, as specified in RFC 7159. Such data can also be stored as text, but the JSON data types have the advantage of enforcing that each stored value is valid according to the JSON rules.

t=# select '{"height":190, "color":"black"}'::json;
              json
---------------------------------
 {"height":190, "color":"black"}
(1 row)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.