title: Postgresql: how to copy a column from a table to another using psycopg2?
body: <p>I am using <code>psycopg2</code> in <code>python</code> to manage my database.</p>
  
  <p>I created two tables. One called <code>data</code> that I want to populate with some data. For doing so I created a temporary table called <code>temporarytable</code> from a <code>.csv</code> file.</p>
  
  <p>I want to copy the column <code>numberofinhabitants</code>. So what I am doing is the following:</p>
  
  <pre><code>### Add Column
  import psycopg2 as ps    
  stat1 = """  ALTER TABLE data ADD COLUMN numberofinhabitants integer"""
  con = ps.connect(dbname = 'mydb', user='postgres', host='localhost', password='mypd')
  con.autocommit = True
  cur = con.cursor()
  cur.execute(stat1)
  cur.close()
  con.close()
  
  
  ### Copy Column
  stat2 = """INSERT INTO data (numberofinhabitants) SELECT numberofinhabitants FROM temporarytable"""
  con = ps.connect(dbname = 'mydb', user='postgres', host='localhost', password='mypd')
  con.autocommit = True
  cur = con.cursor()
  cur.execute(stat2)
  cur.close()
  con.close()
  </code></pre>
  
  <p>but I get the following error</p>
  
  <pre><code>ProgrammingError: column "numberofinhabitants" does not exist
  LINE 1: INSERT INTO data (numberofinhabitants) SELECT numberofinhabi...
                                                        ^
  HINT:  There is a column named "numberofinhabitants" in table "data", but it cannot be referenced from this part of the query.
  </code></pre>
  
  <p>Below a screenshot from <code>pgAdmin3</code> after <code>SELECT * FROM temporarytable;</code></p>
  
  <p><a href="https://i.stack.imgur.com/UNLdx.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/UNLdx.png" alt="enter image description here"></a></p>