0

I need to pass some insert data into postgres which also contains a timestamp. I am using psycopg2 for the same.

I have tried to follow the answer here upon getting the same error as the one asked in the question: Passing a datetime into psycopg2

My code which doesn't work:

recv_data = {"datetime":datetime.datetime(2019, 12, 5, 12, 56, 34, 617607)
             "temperature": 40, "humidity":80}

insert_stmt = "INSERT INTO temp_humidity (temperature,humidity,datetime) VALUES (%s,%s,%s)"
data = (recv_data["temperature"], recv_data["humidity"], recv_data["datetime"])
print(insert_stmt)
cursor.execute(insert_stmt, data)
connection.commit()

ERROR:

ERROR: current transaction is aborted, commands ignored until end of transaction block

STATEMENT: INSERT INTO temp_humidity (temperature,humidity,datetime) VALUES (42,79,'2019-12-05T05:55:45.135111'::timestamp)

Any solution would be appreciated.

Community
  • 1
  • 1
pissall
  • 7,109
  • 2
  • 25
  • 45

1 Answers1

0

The timestamp seems to be OK,

SELECT '2019-12-05T05:55:45.135111'::timestamp;
         timestamp          
----------------------------
 2019-12-05 05:55:45.135111
(1 row)

The problem seems to be elsewhere, maybe in a constraint check? Can you obtain the precise error message from psycopg2 exception or PostgreSQL logs?

Thomas B
  • 181
  • 1
  • 13
  • Apparently you have Auto commit/rollback from whatever IDE (or command line) you are using turned off. Postgres is waiting for commit or rollback. – Belayer Dec 05 '19 at 23:41