9

I've just created a Postgres database and table ('users'). My table has a created_at column of type:

TIMESTAMP WITH TIME ZONE NOT NULL;

I basically want to use this column to keep track of when a new user is created. Is there a way to have Postgres automatically update this with each insert or do I have to supply it a value for this on every insert?

(I want to say when using it before with Sequelize, it created this automatically - so wondering if this was done through Sequelize as a default when inserting).

Thank you!

dace
  • 5,819
  • 13
  • 44
  • 74
  • You might also want to look at this answer, which talks about how to create a trigger based on update, create, or other events. These types of triggers can be used to update/create a timestamp when a row is created, or any time it's updated: http://stackoverflow.com/questions/31147796/postgresql-trigger-update-timestamp-on-field-update – Matt Morgan Dec 23 '16 at 20:52
  • Possible duplicate of [How do I automatically update a timestamp in PostgreSQL](http://stackoverflow.com/questions/9556474/how-do-i-automatically-update-a-timestamp-in-postgresql) – faintsignal Dec 23 '16 at 23:00

1 Answers1

20

You can declare a timestamp field to automatically be filled with the current time on INSERT by including the DEFAULT now() clause in your column definition.

faintsignal
  • 1,828
  • 3
  • 22
  • 30