title: Creating a table with PSQL as clone of another table but excluding a column
body: <p><strong>Short Version:</strong>
  What psql command should I use to create temporary table, which is a clone of a given table, excluding a given column?</p>
  
  <pre><code>create temp table bar_temp as &lt;columns from foo.bar excluding 'insert_date'&gt;
  </code></pre>
  
  <p><strong>Reasoning:</strong>
  I have some very large CSV files I want to bulk upload into a Postges DB (currently 8.4 - migrating to 9.x).</p>
  
  <p>When uploading these CSV files I want to add an additional field for example a "date" field.</p>
  
  <p>Using this post as inspiration: (<a href="https://stackoverflow.com/questions/8910494/how-to-update-selected-rows-with-values-from-a-csv-file-in-postgres">How to update selected rows with values from a CSV file in Postgres?</a>) I'm trying to do the following:</p>
  
  <ol>
  <li>Create a temp table from the schema of the target table excluding the "date" column.</li>
  <li>Run '\COPY' command to upload into the temp table.</li>
  <li>Update the entries in the target table with the entries in the temp table + the additional date column entry.</li>
  </ol>
  
  <p>This is all straight forward except for step 1. If the column names are known in advance this line can be hard coded. In my case I have a number of similar input files with corresponding tables and want the solution to be flexible to handle changes in schema.</p>
  
  <p>My hacky solution is to have a pre-processing script that determines the temp table schema then writes that CREATE command to the SQL script before running, eg:</p>
  
  <p><code>"SELECT 'SELECT ' || array_to_string(ARRAY(SELECT 'o' || '.' || c.column_name FROM information_schema.columns As c WHERE table_name = '$table' AND  c.column_name NOT IN('dt')), ',') || ' FROM $schema.$table As o'"</code> </p>
  
  <p>which gives me something like  </p>
  
  <p><code>SELECT o.name,o.address from foo.bar As o</code></p>
  
  <p>which can then be used in the create statement </p>
  
  <p><code>create temp table temp_tbl as SELECT o.name,o.address from foo.bar As o</code></p>
  
  <p>However I think that there should be away of doing this schema discovery step within PSQL script itself. 
  I've looked at using <a href="http://www.postgresql.org/docs/8.4/static/ecpg-dynamic.html" rel="nofollow noreferrer">"EXEC"</a> but as far as I can see I would need to put this in a function, the return type of this needs to list the table columns!</p>
  
  <p>Any suggestions?</p>