title: PostgreSQL INSERT INTO production table FROM staging table
body: <p>I created two tables using Django models and the scripts look something like this</p>
  
  <p>I am using PostgreSQL 10</p>
  
  <p>production table:</p>
  
  <pre><code>CREATE TABLE public.foods_food(
      id integer NOT NULL DEFAULT nextval('foods_food_id_seq'::regclass),
      code character varying(100) COLLATE pg_catalog."default",
      product_name character varying(255) COLLATE pg_catalog."default",
      brands character varying(255) COLLATE pg_catalog."default",
      quantity character varying(255) COLLATE pg_catalog."default",
      last_modified_datetime timestamp with time zone NOT NULL,
      created_at timestamp with time zone NOT NULL
  )
  </code></pre>
  
  <p>staging table:</p>
  
  <pre><code>CREATE TABLE public.foods_temp(
      id integer NOT NULL DEFAULT nextval('foods_temp_id_seq'::regclass),
      code character varying(100) COLLATE pg_catalog."default",
      product_name character varying(255) COLLATE pg_catalog."default",
  )
  </code></pre>
  
  <p>I copied a CSV file to the staging table and than I tried to copy the columns from the staging table to the production table using the following query.</p>
  
  <pre><code>INSERT INTO foods_food
     SELECT * FROM foods_temp;
  </code></pre>
  
  <p>But I got this error.</p>
  
  <pre><code>ERROR:  null value in column "created_at" violates not-null constraint
  </code></pre>
  
  <p>I can set the <code>created_at</code> column to accept null in order to make it work but I want the <code>created_at</code> values to be auto populated when entries are inserted. </p>
  
  <p>Is there other way to copy columns to the production table and automatically insert the timestamp?</p>