199

I have a table that has thousands of rows. Since the table wasn't constructed with created_at column initially, there is no way of getting their creation timestamp. It is crucial though to start getting the timestamps for future rows.

Is there a way I can add a timestamp column with default value NOW() so that it won't populate the values to previous rows but only for the future ones?

If I do the ALTER query, it populates all rows with timestamp:

ALTER TABLE mytable ADD COLUMN created_at TIMESTAMP DEFAULT NOW()
0

5 Answers 5

265

You need to add the column with a default of null, then alter the column to have default now().

ALTER TABLE mytable ADD COLUMN created_at TIMESTAMP;
ALTER TABLE mytable ALTER COLUMN created_at SET DEFAULT now();
73

You could add the default rule with the alter table,

ALTER TABLE mytable ADD COLUMN created_at TIMESTAMP DEFAULT NOW()

then immediately set to null all the current existing rows:

UPDATE mytable SET created_at = NULL

Then from this point on the DEFAULT will take effect.

2
  • 3
    It's good in principle, though it does carry the weight of doing an update which may fire triggers. Aug 12, 2016 at 11:02
  • 14
    @Artur: The solution Philip presented is the way to go. UPDATE is not necessary. If you add a column default to an existing column, already existing rows are not affected. The default is only filled in if you add column and default in the same command. Aug 13, 2016 at 0:58
44

For example, I will create a table called users as below and give a column named date a default value NOW()

create table users_parent (
    user_id     varchar(50),
    full_name   varchar(240),
    login_id_1  varchar(50),
    date        timestamp NOT NULL DEFAULT NOW()
);

Thanks

1
  • 1
    This does not answer the original question at all. The question is about altering an existing table, not creating a new table.
    – E-Riz
    Feb 16 at 18:43
3

minor optimization.

select pg_typeof(now());   --returns: timestamp with time zone. So now include timezone.        

So better with timestamptz.

begin;
ALTER TABLE mytable ADD COLUMN created_at TIMESTAMPTZ;
ALTER TABLE mytable ALTER COLUMN created_at SET DEFAULT now();
commit;
2
  • 1
    how to set default time that is not now but like zero time? so that way the column does not contain null value? and one can calculate time difference? what will that be? now() gives current time
    – uberrebu
    Jul 15, 2022 at 7:53
  • 1
    ALTER TABLE mytable ALTER COLUMN created_at SET DEFAULT TIMESTAMP WITH TIME ZONE '1970-01-01 00:00:00 UTC';
    – jian
    Jul 15, 2022 at 9:06
-1

Try something like:-

ALTER TABLE table_name ADD  CONSTRAINT [DF_table_name_Created] 
DEFAULT (getdate()) FOR [created_at];

replacing table_name with the name of your table.

1
  • 2
    what is the benefit of such a CONTSTRAINT?
    – rubo77
    Jun 16, 2020 at 6:33

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.