在postgres中的插入语句,用于数据类型为timestamp的时间戳,不带时区NOT NULL, [英] insert statement in postgres for data type timestamp without time zone NOT NULL,

查看:701
本文介绍了在postgres中的插入语句,用于数据类型为timestamp的时间戳,不带时区NOT NULL,的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

数据库新手警报:
我正在尝试插入postgres表中。
我要插入的字段称为 make_date

字段类型为<没有时区的code>时间戳记NOT NULL ,
如何在其中插入今天的日期?就像现在的日期和时间一样?类似于下面的内容,但是我无法为dateTime.now获取正确的数据类型插入格式。

The field type is timestamp without time zone NOT NULL, How do I insert today's date in there? Like right now's date and time? Something like the below but I am unable to get the right datatype insert format for dateTime.now

insert into inventory_tbl (make_date) values (dateTime.now)


推荐答案

除了C. Ramseyer的解决方案(正确),如果您始终(甚至通常)希望将 make_date 作为创建记录的日期,则可以将其默认设置为 now()

In addition to C. Ramseyer's solution (which is right), if you always (or even usually) want make_date to be the date the record was created, you can set its default to now():

alter table inventory_tbl alter make_date set default now();

然后,如果您不将其包括在插入内容的列列表中,自动设置为 now()

Then if you don't include it in the list of columns in your insert, it'll be automatically set to now():

test=> insert into inventory_tbl ( name ) values ('brick'), ('sand'), ('obsidian') returning *;
         make_date          |   name   
----------------------------+----------
 2013-03-21 09:10:59.897666 | brick
 2013-03-21 09:10:59.897666 | sand
 2013-03-21 09:10:59.897666 | obsidian
(3 rows)

INSERT 0 3

这篇关于在postgres中的插入语句,用于数据类型为timestamp的时间戳,不带时区NOT NULL,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆