Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1geoZm-0006DR-7i for pgsql-general@arkaria.postgresql.org; Wed, 02 Jan 2019 22:01:22 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1geoZk-0001uq-RK for pgsql-general@arkaria.postgresql.org; Wed, 02 Jan 2019 22:01:20 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1geoZk-0001uj-FQ for pgsql-general@lists.postgresql.org; Wed, 02 Jan 2019 22:01:20 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1geoZi-0006Uu-2C for pgsql-general@postgresql.org; Wed, 02 Jan 2019 22:01:19 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id x02M1EAQ010030; Wed, 2 Jan 2019 17:01:14 -0500 From: Tom Lane To: Rich Shepard cc: pgsql-general@postgresql.org Subject: Re: Default for date field: today vs CURRENT_DATE In-reply-to: References: Comments: In-reply-to Rich Shepard message dated "Wed, 02 Jan 2019 13:49:21 -0800" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <10028.1546466474.1@sss.pgh.pa.us> Date: Wed, 02 Jan 2019 17:01:14 -0500 Message-ID: <10029.1546466474@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Rich Shepard 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