Thread: Date column that defaults to 'now'

Date column that defaults to 'now'

From
John Siracusa
Date:
How can I create a non-null date column that defaults to 'now' as computed
at the time the row is inserted?

-John


Re: Date column that defaults to 'now'

From
Oliver Elphick
Date:
On Mon, 2004-01-05 at 21:00, John Siracusa wrote:
> How can I create a non-null date column that defaults to 'now' as computed
> at the time the row is inserted?

The default should be either CURRENT_DATE or timeofday()::DATE

The difference is that CURRENT_TIME, CURRENT_TIMESTAMP and CURRENT_DATE
remain the same within a transaction even if the time or date changes,
whereas timeofday() always returns the current clock time.

--
Oliver Elphick                                Oliver.Elphick@lfix.co.uk
Isle of Wight, UK                             http://www.lfix.co.uk/oliver
GPG: 1024D/3E1D0C1C: CA12 09E0 E8D5 8870 5839  932A 614D 4C34 3E1D 0C1C
                 ========================================
     "And thou shalt love the LORD thy God with all thine
      heart, and with all thy soul, and with all thy might."
                 Deuteronomy 6:5


Re: Date column that defaults to 'now'

From
Michael Glaesemann
Date:
Hi John!

On Jan 5, 2004, at 3:00 PM, John Siracusa wrote:

> How can I create a non-null date column that defaults to 'now' as
> computed
> at the time the row is inserted?

How about this?

test=# create table johns (comment text not null,this_time timestamp
not null default now(), this_date date not null default now());
CREATE TABLE
test=# insert into johns (comment) values ('Ars Technica rocks!');
INSERT 1196312 1
test=# select * from johns;
        comment       |         this_time          | this_date
---------------------+----------------------------+------------
  Ars Technica rocks! | 2004-01-05 15:25:52.501707 | 2004-01-05
(1 row)

(If you're not the John Siracusa who writes for Ars Technica, the
sentiment still holds. :) )

Regards,

Michael Glaesemann
grzm myrealbox com


Re: Date column that defaults to 'now'

From
John Siracusa
Date:
On 1/5/04 4:29 PM, Michael Glaesemann wrote:
> (If you're not the John Siracusa who writes for Ars Technica, the
> sentiment still holds. :) )

I am everywhere!

(worked, thanks to both of you who replied :)
-John


Re: Date column that defaults to 'now'

From
Gaetano Mendola
Date:
John Siracusa wrote:

 > On 1/5/04 4:29 PM, Michael Glaesemann wrote:
 >
 >>(If you're not the John Siracusa who writes for Ars Technica, the
 >>sentiment still holds. :) )
 >
 >
 > I am everywhere!
 >
 > (worked, thanks to both of you who replied :)

Anyway the two solution solve different problems:

1) DEFAULT now()
    you'll have the timestamp of transaction

2) DEFAULT timeofday()
    you'll have the timestamp of insertion



Regards
Gaetano Mendola