How to set default timestamp to now() + n days?

前端 未结 2 1501
执念已碎
执念已碎 2021-01-26 07:38

I am developing an auth flow, where tokens with expiry timestamp are used. When a user logs in successfully, a token is generated and saved in the DB.

I know I can add

相关标签:
2条回答
  • 2021-01-26 08:12

    eg:

    t=# create table t5(t timestamp default now() + '1 day'::interval);
    CREATE TABLE
    t=# insert into t5 default values;
    INSERT 0 1
    t=# select now(),t from t5;
                  now              |             t
    -------------------------------+----------------------------
     2018-01-26 08:05:06.641249+00 | 2018-01-27 08:04:57.035831
    (1 row)
    
    0 讨论(0)
  • 2021-01-26 08:15

    Just use that as the default expression:

    create table token
    (
       id integer primary key,
       token text,
       expires timestamp default current_timestamp + interval '8' hour
    );
    
    0 讨论(0)
提交回复
热议问题