pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: Rich Shepard <rshepard@appl-ecosys.com>
Cc: pgsql-general@postgresql.org
Subject: Re: Default for date field: today vs CURRENT_DATE
Date: Wed, 02 Jan 2019 17:01:14 -0500
Message-ID: <10029.1546466474@sss.pgh.pa.us> (raw)
In-Reply-To: <alpine.LNX.2.20.1901021343520.20959@salmo.appl-ecosys.com>
References: <alpine.LNX.2.20.1901021343520.20959@salmo.appl-ecosys.com>

Rich Shepard <rshepard@appl-ecosys.com> writes:
>    Reading the manual I saw that 'today' is a special value, but it did not
> work when I used it as a column default; e.g.,
> start_date date DEFAULT today,
>    Appending parentheses also failed. But, changing today to CURRENT_DATE
> worked. I've not found an explanation and would appreciate learning why
> 'today' fails.

'today' is special as a date input string, so you can use it as a literal:

regression=# select 'today'::date;
    date    
------------
 2019-01-02
(1 row)

But it's not a SQL keyword, nor a function name, so you can't write it
without quotes.

Also, it wouldn't be very useful for this purpose, because it's resolved
on sight in date_in().  Thus

regression=# create table wrong_thing (start_date date DEFAULT 'today');
CREATE TABLE
regression=# \d wrong_thing
                  Table "public.wrong_thing"
   Column   | Type | Collation | Nullable |      Default       
------------+------+-----------+----------+--------------------
 start_date | date |           |          | '2019-01-02'::date

The default would effectively be the creation date of the table,
not the insertion date of any particular row.

So CURRENT_DATE or one of its sibling functions is what you want
here.  On the other hand, something like

INSERT INTO my_table VALUES ('today', ...);

might be perfectly sensible code.

			regards, tom lane




view thread (3+ messages)  latest in thread

Message-ID: <10029.1546466474@sss.pgh.pa.us>
Permalink:  ../10029.1546466474@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/10029.1546466474@sss.pgh.pa.us

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: tgl@sss.pgh.pa.us, rshepard@appl-ecosys.com
  Subject: Re: Default for date field: today vs CURRENT_DATE
  In-Reply-To: <10029.1546466474@sss.pgh.pa.us>
  References: <alpine.LNX.2.20.1901021343520.20959@salmo.appl-ecosys.com> <10029.1546466474@sss.pgh.pa.us>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox