If I added this hash values {"1"=>"1", "3"=>"3", "2"=>"2"} into hstore column into PostgreSql (9.4) (through rails 5}, this hash will be reorder under hstore column and looks like this : {"1"=>"1", "2"=>"2", "3"=>"3"}. Is it possible to prevent this?
Hash order is guaranteed in Ruby. I assume that because it's leaving Ruby and going to the database, you are not getting the guarantee anymore. I don't think postgres preserves the order source:
The order of the [hstore] pairs is not significant (and may not be reproduced on output)
If you really want the order to be preserved I think you're out of luck. If you want the order to be the same then you should sort the Ruby hash in a known way and then sort the SQL in the same way. I believe there's a way to sort an hstore column by keys: Order by a value of an arbitrary attribute in hstore