title: Create a trigger in Postgres that will insert a record to another table with added column
body: <p>I have these 2 tables created in postgres,</p>
  
  <pre><code>CREATE TABLE EMPLOYEE(
     ID           INT             PRIMARY KEY NOT NULL,
     NAME         VARCHAR(100)    NOT NULL,
     ADDRESS      VARCHAR (250)   NOT NULL
  );
  
  
  CREATE TABLE HIST_EMPLOYEE(
     HISTORYNUM   INT             NOT NULL,
     ID           INT             NOT NULL,
     NAME         VARCHAR(100)    NOT NULL,
     ADDRESS      VARCHAR (250)   NOT NULL
  );
  </code></pre>
  
  <p>..so whenever I want to insert, update, or delete a record in my <code>EMPLOYEE</code> table, that record should be inserted in <code>HIST_EMPLOYEE</code> table. regardless if it is single or multiple rows</p>
  
  <p>..I know that there are lots of answers in this site..but the problem is I added a column in my HISTORY table <code>HIST_EMPLOYEE</code>..that's why I cant used this script that most of the answers posted,</p>
  
  <pre><code>insert into hist_employee select * from employee
  </code></pre>
  
  <p>...this is what i've started so far, but it has so many errors..</p>
  
  <p><strong><em>TRIGGER</em></strong></p>
  
  <pre><code>CREATE TRIGGER insertHistory
      AFTER INSERT OR UPDATE ON employee   --MAIN TABLE
          EXECUTE PROCEDURE insertHistory('hist_employee');  --HISTORY TABLE
  </code></pre>
  
  <p><strong><em>TRIGGER FUNCTION</em></strong></p>
  
  <pre><code>CREATE OR REPLACE FUNCTION insertHistory() RETURNS TRIGGER AS 
  $func$
      BEGIN
         EXECUTE format('insert into %I select * from %I', TG_ARGV[0] ,TG_TABLE_NAME );
         return null;
      END;
  $func$ 
  LANGUAGE plpgsql;
  </code></pre>
  
  <blockquote>
    <p>NOTE: </p>
    
    <ol>
    <li>This trigger should be also applicable or flexible enough to be implemented on other tables with different set of columns. </li>
    <li><p>HISTORYNUM column is unique per id, </p></li>
    <li><p>And, is it possible to combine these 3, <strong>AFTER INSERT, AFTER UPDATE, BEFORE DELETE</strong> .. in one trigger ??</p></li>
    </ol>
  </blockquote>
  
  <p>thanks to all who will respond..sorry if you find my question too vague</p>