title: What is the correct way to Insert an image into Postgres with python?
body: <p>I attempted to adapt the examples <a href="http://iamtgc.com/using-python-to-load-binary-files-into-postgres/" rel="nofollow noreferrer">here</a>  and <a href="https://stackoverflow.com/a/13342031/443779">here</a></p>
  
  <pre><code>import psycopg2
  #Given media_id and image_url and conn object
  image_data = urllib2.urlopen(image_url).read()
  sql =("INSERT INTO images (media_id, data) 
  SELECT %s
  WHERE 
  NOT EXISTS (SELECT media_id FROM images WHERE media_is = CAST(%s as TEXT) ")
  data_insert_image = (media_id, psycopg2.Binary(image_data))
  cursor.execute(sql_insert_image, data_insert_image)
  conn.commit()
  </code></pre>
  
  <p>Results is:</p>
  
  <pre><code>TypeError: not all arguments converted during string formatting
  </code></pre>
  
  <p>Which makes sense to me as the image is not a <code>String</code>; however, I don't know how to insert properly. How should the insertion be performed?</p>