NiroshanJ
NiroshanJ

Reputation: 578

Is it possible to set current time as default value in a column with data type time in PostgreSQL?

I was trying to create a column with data type time and trying to set the default value as the current time using HeidiSql. Below is the altered code.

ALTER TABLE "user"
    ADD "user" TIME NULL DEFAULT CURRENT_TIME();

But it output an error as below.

syntax error at or near ")"
LINE 2:  ADD "created_time" TIME NULL DEFAULT CURRENT_TIME()

Any idea about my mess?

Upvotes: 2

Views: 635

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1269803

This works fine:

create table users (user_id serial);

ALTER TABLE users
    ADD create_Time TIME NULL DEFAULT CURRENT_TIME;

You don't need parentheses around CURRENT_TIME.

Some other things:

  • The error in your question is not generated by the SQL in the question; the column names are different.
  • It is highly unusual to put the time in without the date. Are you sure you don't want now()/CURRENT_TIMESTAMP?
  • Don't use escapes around names. They are just cumbersome to read and clunky to write.

Upvotes: 2

Related Questions