Question

I am trying to store the current timestamp as a default value in an hstore. I tried using now() but all that is stored is "last_collected"=>"now()". Here is what I have:

'"level"=>"1", "last_collected"=>now()'::hstore

Is there a correct way to do this or is it even possible? Thanks!

Was it helpful?

Solution

Using one of the hstore constructor functions would probably be easier:

hstore(text[]): construct an hstore from an array, which may be either a key/value array, or a two-dimensional array.
hstore(text[], text[]): construct an hstore from separate key and value arrays.

So you could use one of these:

hstore(array['level', '1', 'last_collected', now()::text])
hstore(array[array['level', '1'], array['last_collected', now()::text]])
hstore(array['level', 'last_collected'], array['1', now()::text])

Keep in mind that hstore uses text for both the keys and values so your timestamp will be a string. You might want to use to_char instead of ::text to convert your timestamp to a string, that will give you more control over the precision and format.

hstore(ARRAY['level', '1', 'last_collected', to_char(CURRENT_TIMESTAMP, 'HH12:MI:SS')]);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top