Ho una colonna del tipo TIMESTAMP WITHOUT TIME ZONE
e vorrei avere quell'impostazione predefinita sull'ora corrente in UTC. Ottenere l'ora corrente in UTC è facile:Utilizzo dell'ora corrente in UTC come valore predefinito in PostgreSQL
postgres=# select now() at time zone 'utc';
timezone
----------------------------
2013-05-17 12:52:51.337466
(1 row)
Come è usando il timestamp corrente per una colonna:
postgres=# create temporary table test(id int, ts timestamp without time zone default current_timestamp);
CREATE TABLE
postgres=# insert into test values (1) returning ts;
ts
----------------------------
2013-05-17 14:54:33.072725
(1 row)
Ma che utilizza l'ora locale. Il tentativo di imporre che ai risultati UTC in un errore di sintassi:
postgres=# create temporary table test(id int, ts timestamp without time zone default now() at time zone 'utc');
ERROR: syntax error at or near "at"
LINE 1: ...int, ts timestamp without time zone default now() at time zo...
posso vedere un funciton come now_utc() brilla veramente quando si scrivono le query – misaxi
Questo funziona anche quando il tempo passa indietro di un'ora - ora() restituisce un timestamp che conosce la compensato da UTC. –