title: Passing a datetime into psycopg2
body: <p>I am trying to insert a datetime value into my postgres database using Psycopg2.</p>
  
  <p>My code was working before, but I switched from %s notation to {} notation, and my code broke.</p>
  
  <p>here is my code</p>
  
  <pre><code>for j in eveLists.itemList:
      tempPrice = fetchSellPrice(i, j)
      database_name = eveLists.DatabaseDict[i]
      now = datetime.datetime.utcnow()
  
      cur.execute("INSERT INTO {0} VALUES ({1}, {2}, {3}, NULL, {4}, {5}, NULL);".format(
                                                                                         database_name,
                                                                                         str(i),
                                                                                         str(j),
                                                                                         float(tempPrice),
                                                                                         datetime.date.today(),
                                                                                         now))
  </code></pre>
  
  <p>I get the following error:</p>
  
  <pre><code>psycopg2.ProgrammingError: syntax error at or near "00"
  LINE 1: ...0000142, 2268, 3.11, NULL, 2017-05-09, 2017-05-10 00:40:03.3...
                                                               ^
  </code></pre>
  
  <p>It is treating the date and the time as two separate objects.</p>
  
  <p>I have tried several different ways which are commented out, and all throw various error messages. I have tried wrapping the datetime in quotes,</p>
  
  <pre><code>now = ("'%s'") % str(datetime.datetime.utcnow())
  </code></pre>
  
  <p>which give an error </p>
  
  <pre><code>psycopg2.ProgrammingError: column "mydate" is of type date but expression is of type integer
  LINE 1: ...NTO temp_jita VALUES (30000142, 2268, 3.11, NULL, 2017-05-09...
                                                               ^
  HINT:  You will need to rewrite or cast the expression.
  </code></pre>
  
  <p>It thinks my date is an integer, even though I wrapped it in quotes.</p>
  
  <p>I have also tried making a psycopg2 timestamp manually:</p>
  
  <pre><code>now = psycopg2.Timestamp(now.strftime("%Y"),
                                       now.strftime("%m"),
                                       now.strftime("%d"),
                                       now.strftime("%h"),
                                       now.strftime("%M"),
                                       int(now.strftime("%-S")))
  </code></pre>
  
  <p>Which gives the following error:</p>
  
  <pre><code>    int(now.strftime("%-S")))
  TypeError: an integer is required (got type str)
  </code></pre>
  
  <p>I don't know how it thinks this is a string, especially since I cast it as an int!</p>
  
  <p>Any help would be appreciated.</p>
  
  <p>EDIT: The reason I passed the variables using {} notation and not %s notation is because I received an error for the following code:</p>
  
  <pre><code>  for j in eveLists.itemList:
              tempPrice = fetchSellPrice(i, j)
              database_name = eveLists.DatabaseDict[i]
              now = datetime.datetime.utcnow()
              now = str(now)
  
              cur.execute("INSERT INTO %s VALUES (%s, %s, %s, NULL, %s, %s, NULL);", [database_name,
                                                                                      str(i),
                                                                                      str(j),
                                                                                      float(tempPrice),
                                                                                      datetime.date.today(),
                                                                                      now
                                                                                      ])
  
  psycopg2.ProgrammingError: syntax error at or near "'temp_jita'"
  LINE 1: INSERT INTO 'temp_jita' VALUES ('30000142', '2268', 3.03, NU...
                      ^
  </code></pre>
  
  <p>Please refer to my previous post on the subject: <a href="https://stackoverflow.com/questions/43877210/how-to-remove-the-quotes-from-a-string-for-sql-query-in-python">How to remove the quotes from a string for SQL query in Python?</a></p>
  
  <p>EDIT: following @Zorg 's advice from this link (<a href="http://initd.org/psycopg/docs/sql.html" rel="noreferrer">http://initd.org/psycopg/docs/sql.html</a>), this code worked for me:</p>
  
  <pre><code>cur.execute(sql.SQL("INSERT INTO {} VALUES (%s, %s, %s, NULL, %s, %s, NULL);").format(sql.Identifier(database_name)),[
                                                                                      str(i),
                                                                                      str(j),
                                                                                      float(tempPrice),
                                                                                      datetime.date.today(),
                                                                                      now
                                                                                      ])
  </code></pre>