• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 297
  • Last Modified:

Timestamp in Postgresql table...

Is there a way to add some type of timestamp or date to a column in a table automatically via the DB itself verses Inserting using the timestamp from my application that is performing the DB operation?

1 Solution
Set the default value of the timestamp column to use the "now()" function.  Then when a record is added and no value is set for that column, PostgreSQL will use the value from 'now()'.  

Of course, this method relies on NULL being set for the column in question when being inserted into the table.  If the application still sets a value for this, that value will be used instead of what would come from 'now()'.
# new table
CREATE TABLE your_table (
 created_on timestamp DEFAULT now(),

# change an existing table
ALTER TABLE your_old_table ALTER COLUMN created_on SET DEFAULT now();

Open in new window

Joel_SiskoAuthor Commented:
Awesome thanks.

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now