1

I have a table on my database, I fetched the first row of my table and the result looks like this:

(datetime.datetime(2018, 10, 3, 16, 27, 29, 198000), 'buy', Decimal('0.00276851'), Decimal('6.00000000'), 1, None)

However, on my database (datetime.datetime(2018, 10, 3, 16, 27, 29, 198000) is a timestamp but here it is a string. How can I transform this result into a timestamp format that I could use later on my code? Thanks!

This is my code so far:

conn = psycopg2.connect("dbname=monty user=postgres host=localhost password=*****")
cur = conn.cursor()
cur.execute("SELECT * FROM binance.zrxeth_aggregated FETCH first 1 rows only;")
row = cur.fetchone()
cur.close()
conn.close()
ADyson
  • 51,527
  • 13
  • 48
  • 61
Viktor.w
  • 1,397
  • 1
  • 13
  • 38

1 Answers1

1

According to the Python-to-Postgres types table psycopg2's documentation, this is the intended behavior. Postgres's timestamp becomes datetime object in Python.

Then, if you really need the timestamp value, it is just a matter of calling datetime.utcfromtimestamp() on your datetime object:

alecxe
  • 441,113
  • 110
  • 1,021
  • 1,148