5

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?

2 Answers 2

5

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Ok, can I use CURRENT_TIMESTAMP as Oracle Timestamp?
Now() returns TIME STAMP WITHOUT TIME ZONE, note that according to his table LAST_LOGIN column is of type Date. This won't make much of a difference for this specific query but it is better to use current_date.
1

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

UPDATE USERS SET LAST_LOGIN = CURRENT_TIMESTAMP WHERE USER_NAME = ?

1 Comment

@Peter Penzov, even if my answer is earlier, set sagi's answer as accepted since it's more detailed and has correct referencing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.