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

孤街浪徒 提交于 2019-12-09 14:09:24

问题


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.

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)

回答1:


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();

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



回答2:


Use now() or CURRENT_TIMESTAMP.



来源:https://stackoverflow.com/questions/15530899/insert-statement-in-postgres-for-data-type-timestamp-without-time-zone-not-null

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!