Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

postgresql

pg

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)
like image 332
Doublespeed Avatar asked Mar 20 '13 17:03

Doublespeed


People also ask

What is timestamp without timezone in PostgreSQL?

In a literal that has been determined to be timestamp without time zone , PostgreSQL will silently ignore any time zone indication. That is, the resulting value is derived from the date/time fields in the input value, and is not adjusted for time zone.

Is timestamp stored without timezone?

The timestamp datatype allows you to store both date and time. However, it does not have any time zone data. It means that when you change the timezone of your database server, the timestamp value stored in the database will not change automatically.

How do I select a timestamp in PostgreSQL?

select 'date: time' :: timestamp; select CURRENT_TIMESTAMP :: timestamp; Below is the parameter description of the above syntax are as follows: Select: Select is used to select timestamp value in timestamp syntax.


2 Answers

Use now() or CURRENT_TIMESTAMP.

like image 68
C. Ramseyer Avatar answered Oct 05 '22 17:10

C. Ramseyer


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
like image 28
Michael Granger Avatar answered Oct 05 '22 19:10

Michael Granger