title: How can I update multiple columns on multiple rows in postgresql using psycopg2
body: <p>I have a somewhat complex sql query that should update multiple columns on multiple rows in a table. Am trying to pass the multiple parameters to the query and also loop though the data to be updated through psycopg2 but I can't figure out a way to do this.</p>
  
  <p>Here is the sample data I want to loop through.</p>
  
  <pre><code> data = [(214, 'Feb', 545), (215, 'March', 466)]
  </code></pre>
  
  <p>So far here is the sql query I have</p>
  
  <pre><code>    query = """
        UPDATE table_1
        SET 
        date_from = 
         (CASE version 
             WHEN 1 THEN '1900-01-01' ELSE 
         ( SELECT date_to 
               FROM table_1 
               WHERE month = data.month
               AND cust_key = data.cust_key 
               AND prod_key = data.prod_key
           AND version = (
                 SELECT version-1 
                 FROM table_1 
                 WHERE month = data.month 
                 AND cust_key = data.cust_key 
                 AND prod_key = data.prod_key
             ORDER BY version DESC LIMIT 1)
          ) 
      END), 
        date_to = current_date
        FROM (VALUES %s) AS data(cust_key, month, prod_key)
        WHERE month = data.month
        AND cust_key = data.cust_key 
        AND prod_key = data.prod_key
       """
  </code></pre>
  
  <p>Here is how I am passing my parameters</p>
  
  <pre class="lang-sql prettyprint-override"><code>    WHERE month = data.month
      AND cust_key = data.cust_key 
      AND prod_key = data.prod_key
  </code></pre>
  
  <pre class="lang-sql prettyprint-override"><code>    FROM (VALUES %s) AS data(cust_key, month, prod_key)
  </code></pre>
  
  <p>And this is how I am executing the query</p>
  
  <pre class="lang-py prettyprint-override"><code>    cursor = db.cursor()
      execute_values(cursor, query, (data,))
      db.commit()
      return True
  </code></pre>
  
  <p>When I execute the query, I get this error  <strong><em>psycopg2.errors.InvalidColumnReference: table &quot;data&quot; has 2 columns available but 3 columns specified</em></strong> I have gone through multiple solutions on this site but none seems to work for me.</p>
  
  <p>Is there a way around this?</p>