Używanie bieżącego czasu w UTC jako domyślnej wartości w PostgreSQL

Mam kolumnę typu TIMESTAMP WITHOUT TIME ZONE i chciałbym, aby była ona domyślna dla bieżącego czasu w UTC. Uzyskanie aktualnego czasu w UTC jest łatwe:

postgres=# select now() at time zone 'utc';
          timezone          
----------------------------
 2013-05-17 12:52:51.337466
(1 row)

Jak używa bieżącego znacznika czasu dla kolumny:

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)

Ale To używa czasu lokalnego. Próba wymuszenia tego NA UTC powoduje błąd składni:

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...
Author: Wichert Akkerman, 2013-05-17

5 answers

Funkcja nie jest nawet potrzebna. Po prostu umieść nawiasy wokół domyślnego wyrażenia:

create temporary table test(
    id int, 
    ts timestamp without time zone default (now() at time zone 'utc')
);
 210
Author: Daniel Vérité,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-05-17 13:30:51

Zawiń go w funkcję:

create function now_utc() returns timestamp as $$
  select now() at time zone 'utc';
$$ language sql;

create temporary table test(
  id int,
  ts timestamp without time zone default now_utc()
);
 19
Author: Denis de Bernardy,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-05-17 13:04:15

A co z

now()::timestamp

Jeśli twój drugi znacznik czasu jest bez strefy czasowej, to ten rzut da Typ dopasowania "znacznik czasu bez strefy czasowej" dla bieżącego czasu.

Chciałbym jednak przeczytać, co inni myślą o tej opcji. Nadal nie ufam mojemu zrozumieniu tej strefy czasowej" z/bez".
 12
Author: Risadinha,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-03-29 10:57:45

Jeszcze inne rozwiązanie:

timezone('utc', now())
 11
Author: martti,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-01-05 19:46:29

Funkcja już istnieje: timezone ('UTC':: text, now ())

 0
Author: user10259440,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-08-22 11:26:42