Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Possible to automatically create a timestamp record on insert with Postgres?

Tags:

postgresql

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!

like image 919
hidace Avatar asked Dec 23 '16 20:12

hidace


1 Answers

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.

like image 55
faintsignal Avatar answered Dec 26 '22 16:12

faintsignal