How to add a new column (Python list) to a Postgresql table?
<p>I have a Python list <code>newcol</code> that I want to add to an existing Postgresql table. I have used the following code:</p>

<pre><code>conn = psycopg2.connect(host='***', database='***', user='***', password='***')
cur = conn.cursor()
cur.execute('ALTER TABLE %s ADD COLUMN %s text' % ('mytable', 'newcol'))
conn.commit()
</code></pre>

<p>This added the list <code>newcol</code> to my table, however the new column has no values in it. In python, when I print the the list in python, it is a populated list.</p>

<p>Also, the number of rows in the table and in the list I want to add are the same. I'm a little confused.</p>

<p>Thanks in advance for the help.</p>

<p><code>ALTER TABLE</code> only changes table schema -- in your case it will create the new column and initialize it with empty (NULL) values.</p>

<p>To add list of values to this column you can do:
<code>UPDATE TABLE &lt;table&gt; SET ...</code> in a loop.</p>

python postgresql