Search code examples
ruby-on-railsarraysjsonpostgresqlhstore

choosing between arrays, json and hstore datatypes in postgres and rails


I have some fields that are related to other fields and I think to put it in one database column will be much cleaner.

For example lets say I have these fields: parking_available:boolean paid_parking:boolean free_parking:boolean parking_price:integer and reservation_required:boolean

So if I use arrays,json or hstore then I may store and fetch the values for above parameters in order as strings without considering the datatypes. But I am confused on which one is better here or stick with the individual database fields. Could someone explain the use cases for arrays,json and hstore and performace benefits of these?


Solution

  • Although, it directly does not answer your question, How to persist hashes in rails application with postgresql stated about hstore, JSON and JSONB

    Although it may not be a common case to store other types than string in a dictionary, it is still worth to have in mind that hstore supports only string data.

    ...

    it (JSON) supports nested objects and more datatypes. It also has more operators that are very well described in documentation. If you are using JSON somewhere in your application already and want to store it directly in database, then the JSON datatype is perfect choice.

    ...

    If you still wonder whether MongoDB will be better choice for you needs, check JSONB support introduced in 9.4 version of postgres, which is real data type with binary storage and indexing, a structured format for storing json

    According to my interpretation of the above source, you should use JSON or JSONB (if you already using PostgreSQL 9.4+ or no inhibiting factor to change to 9.4+)