pg.ddx.io  pgsql-admin@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Steve Holdoway <steve.holdoway@firetrust.com>
To: pgsql-admin@postgresql.org
Subject: Re: example of insert timestamp
Date: Tue, 26 Feb 2008 14:26:29 +1300
Message-ID: <20080226142629.1bc12aa7.steve.holdoway@firetrust.com> (raw)
In-Reply-To: <200802251821.19453.david.bear@asu.edu>
References: <200802251821.19453.david.bear@asu.edu>

On Mon, 25 Feb 2008 18:21:19 -0700
David Bear <david.bear@asu.edu> wrote:

> I've been reading about triggers on insert and found the page at 
> http://www.postgresql.org/docs/8.1/interactive/triggers.html with some sample 
> code in the comments.
> 
> I'm thinking what I want can't really be this involved.
> 
> I want to have a table with a timestamp field that automatically gets the 
> value of now() on insert. The timestamp will never be updated. I assume I 
> need to create a trigger to do this. If there is an easier way, please 
> advise.
> 
> Otherwise, is there any sample code that would should be how to do this? The 
> sample on the page above looks like overkill.
> 
> -- 
> 
> David Bear
> College of Public Programs/ASU
> 411 N Central, Phoenix, AZ 85004
> 
> ---------------------------(end of broadcast)---------------------------
> TIP 3: Have you checked our extensive FAQ?
> 
>                http://www.postgresql.org/docs/faq
Here's a quick example which sets column dateadded to now on insert... can't get much simpler!


create or replace function surbl.AddUrl()
returns TRIGGER AS
$$
BEGIN
-- Must have an url to add
IF ( NEW.url IS NULL )
THEN
	RETURN NULL;
END IF;
NEW.dateadded := now();
RETURN NEW;
END;
$$ language 'plpgsql';

drop trigger addurl_trig ON surbl.rawdata;
create trigger addurl_trig BEFORE INSERT ON surbl.rawdata
FOR EACH ROW
EXECUTE PROCEDURE surbl.AddUrl();

view thread (7+ messages)  latest in thread

Message-ID: <20080226142629.1bc12aa7.steve.holdoway@firetrust.com>
Permalink:  ../20080226142629.1bc12aa7.steve.holdoway@firetrust.com/
Also on:    postgresql.org/message-id/20080226142629.1bc12aa7.steve.holdoway@firetrust.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-admin@postgresql.org
  Cc: steve.holdoway@firetrust.com
  Subject: Re: example of insert timestamp
  In-Reply-To: <20080226142629.1bc12aa7.steve.holdoway@firetrust.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox