title: MySQL Trigger to PostgreSQL Trigger
body: <p>I have a trigger in MySQL written as such:</p>
  
  <pre><code>DELIMITER $$
  
  CREATE TRIGGER mytrigger 
  BEFORE INSERT ON people
  FOR EACH ROW
  BEGIN
  
     SET NEW.first_name  = SUBSTRING_INDEX(SUBSTRING_INDEX( NEW.name, ' ', 1), ' ', -1);
  
     SET NEW.last_name   = SUBSTRING_INDEX(SUBSTRING_INDEX( NEW.name, ' ', 3), ' ', -1);
  
     SET NEW.middle_name = IF(LENGTH( NEW.name) - LENGTH(REPLACE( NEW.name, ' ', ''))&gt;1
         ,SUBSTRING_INDEX(SUBSTRING_INDEX( NEW.name, ' ', 2), ' ', -1)
         ,NULL);
  
  END$$
  
  DELIMITER ;
  </code></pre>
  
  <p>I'm switching to PostgreSQL since Heroku's ClearDB option for MySQL requires a plan costing inordinate amounts of money to use triggers</p>
  
  <p>My question is: I've tried everything, how can I get this trigger to work in PostgreSQL?</p>