title: Psycopg2 insert python dictionary in postgres database
body: <p>In python 3+, I want to insert values from a dictionary (or pandas dataframe) into a database. I have opted for psycopg2 with a postgres database.</p>
  
  <p>The problems is that I cannot figure out the proper way to do this. I can easily concatenate a SQL string to execute, but the psycopg2 documentation explicitly warns against this. Ideally I wanted to do something like this:</p>
  
  <pre><code>cur.execute("INSERT INTO table VALUES (%s);", dict_data)
  </code></pre>
  
  <p>and hoped that the execute could figure out that the keys of the dict matches the columns in the table. This did not work. From the examples of the psycopg2 documentation I got to this approach</p>
  
  <pre><code>cur.execute("INSERT INTO table (" + ", ".join(dict_data.keys()) + ") VALUES (" + ", ".join(["%s" for pair in dict_data]) + ");", dict_data)
  </code></pre>
  
  <p>from which I get a</p>
  
  <pre><code>TypeError: 'dict' object does not support indexing
  </code></pre>
  
  <p>What is the most phytonic way of inserting a dictionary into a table with matching column names?</p>