Brad Lucas

Programming, Clojure and other interests
September 24, 2009

Postgres Date Arithmetic

Postgres Date Arithmetic

Postgres Timestamp Addition

I created a table where I want to have two columns with dates. The first will be the date the record in the table is created. The other should be a week later. The table is for a contact management system where I'm going to keep information about when I emailed a contact and when I should follow up a week later.

In Postgres this turned out to be very easy. Look at the following table definition. You can easily create a date 7 days from now by adding "interval '7 days'" to now();

create table campaign_event (
       id serial,
       campaign_id int,
       contact_id int,
       add_date timestamp default now(),
       sent_flag boolean default false,
       followup_date timestamp default now() + interval '7 days',
       followup_flag boolean default false
);
Tags: postgres