By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,178 Members | 987 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,178 IT Pros & Developers. It's quick & easy.

Date column that defaults to 'now'

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

-John
---------------------------(end of broadcast)---------------------------
TIP 3: if posting/reading through Usenet, please send an appropriate
subscribe-nomail command to ma*******@postgresql.org so that your
message can get through to the mailing list cleanly

Nov 12 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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 Ol************@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
---------------------------(end of broadcast)---------------------------
TIP 9: the planner will ignore your desire to choose an index scan if your
joining column's datatypes do not match

Nov 12 '05 #2

P: n/a
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
---------------------------(end of broadcast)---------------------------
TIP 9: the planner will ignore your desire to choose an index scan if your
joining column's datatypes do not match

Nov 12 '05 #3

P: n/a
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
---------------------------(end of broadcast)---------------------------
TIP 3: if posting/reading through Usenet, please send an appropriate
subscribe-nomail command to ma*******@postgresql.org so that your
message can get through to the mailing list cleanly

Nov 12 '05 #4

P: n/a
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
Nov 12 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.