Re: Default timestamp value - Mailing list pgsql-sql

From Jeff Hoffmann
Subject Re: Default timestamp value
Date
Msg-id 393BCCC6.7D394758@propertykey.com
Whole thread Raw
In response to Default timestamp value  ("Rob S." <rslifka@home.com>)
List pgsql-sql
Tom Lane wrote:
> 
> "Rob S." <rslifka@home.com> writes:
> > ...but I still don't see how to have the default value of a timestamp to be
> > the time at which the individual record is inserted.  I just get the time I
> > created the table.  

> Versions before 7.0 are not entirely consistent about this, but I
> believe the explicit function call now() will work the way you want
> in any version.

you can also use the value CURRENT_TIMESTAMP, which is standard SQL. 
you have to be careful about using 'now' as a default, though.  i have a
tendancy to put type qualifications on most of the things i write ever
since i had problems with getting an index to be used on an int2 field. 
the key to the faq is that you can't put a type qualification after the
'now', otherwise it won't work.  so in 7.0, if you create a table like
any of these:

create table foo (f1 int, f2 timestamp default now());
create table foo (f1 int, f2 timestamp default 'now');
create table foo (f1 int, f2 timestamp default CURRENT_TIMESTAMP);

it will work.  if you do it like this, it won't:

create table foo (f1 int, f2 timestamp default 'now'::datetime);
create table foo (f1 int, f2 timestamp default 'now'::timestamp);

it will give you the time that the table was created.  it's just a
little gotcha that might frustrate people in a similar situation.

jeff


pgsql-sql by date:

Previous
From: Tom Lane
Date:
Subject: Re: cron job INSERT appears to bail.
Next
From: Tom Lane
Date:
Subject: Re: SQL functions - bug?