postgresql 在 postgres 中为没有时区的数据类型时间戳插入语句 NOT NULL,

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15530899/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 00:05:57  来源:igfitidea点击:

insert statement in postgres for data type timestamp without time zone NOT NULL,

postgresqlpg

提问by Doublespeed

Database noob alert: I am trying to insert into a postgres table. The field that I would like to insert in is called make_date.

数据库菜鸟警报:我正在尝试插入 postgres 表。我要插入的字段称为make_date.

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

字段类型是timestamp without time zone NOT NULL,如何在其中插入今天的日期?比如现在的日期和时间?类似于下面的内容,但我无法为 dateTime.now 获得正确的数据类型插入格式

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

回答by C. Ramseyer

Use now()or CURRENT_TIMESTAMP.

使用now()CURRENT_TIMESTAMP

回答by Michael Granger

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

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

alter table inventory_tbl alter make_date set default now();

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

然后,如果您不将其包含在插入的列列表中,它将自动设置为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