CREATE TABLE categories ( catName text NOT NULL, sortOrder integer, parent text DEFAULT 'TOPLEVEL' NOT NULL, category_template text, category_template_timestamp timestamp, primary key (catName), foreign key (catName) references categorie_names(catName), foreign key (parent) references categorie_names(catName) ); CREATE OR REPLACE FUNCTION categories_template_timstamp() RETURNS trigger AS ' BEGIN NEW.category_template_timestamp := \'now\'; RETURN NEW; END; ' LANGUAGE plpgsql; CREATE TRIGGER categories_template_timstamp AFTER INSERT OR UPDATE ON categories FOR EACH ROW EXECUTE PROCEDURE categories_template_timstamp();