Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Insert timestamp into PostgreSQL table

Tags:

sql

postgresql

I would like to insert TIMESTAMP into column LAST_LOGIN

CREATE TABLE USER(
 ID INTEGER NOT NULL,
 USER_NAME TEXT,
 FIRST_NAME TEXT,
 LAST_NAME TEXT,
 LAST_LOGIN DATE,
 DATE_REGISTERED DATE,
 ROLE INTEGER,
 CAN_LOGIN INTEGER
)
;

ALTER TABLE USER ADD CONSTRAINT KEY1 PRIMARY KEY (ID)
;

I tried this:

UPDATE USERS SET LAST_LOGIN = TIMESTAMP WHERE USER_NAME = ?

But I get org.postgresql.util.PSQLException: ERROR: column "timestamp" does not exist Position: 31

What is the correct way to insert current time into table column LAST_LOGIN?

like image 232
Peter Penzov Avatar asked Mar 28 '16 11:03

Peter Penzov


2 Answers

TIMESTAMP is not a known function is POSTGRES , therefore, it recognize it as a column .

POSTGRES dates/time functions:

NOW();
current_date;
current_time;
current_timestamp;

So your correct query should be

UPDATE USERS SET LAST_LOGIN = now() WHERE USER_NAME = ?

You can read all about postgres time/date functions in this document.

like image 159
sagi Avatar answered Sep 28 '22 09:09

sagi


Maybe the error comes from using the wrong function. Try this instead:

UPDATE USERS SET LAST_LOGIN = CURRENT_TIMESTAMP WHERE USER_NAME = ?
like image 30
Eduard Uta Avatar answered Sep 28 '22 07:09

Eduard Uta