'Deleting table rows older than x days in PostgreSQL database [duplicate]
This question has been asked and answered before, but I am getting a syntax error with prior answers. Perhaps I am missing something?
query = "DELETE FROM currentvoltage WHERE timestamp < UNIX_TIMESTAMP(DATE_SUB(NOW() - INTERVAL 1 DAY))"
cursor.execute(query)
currentvoltage is table
timestamp is row with timestamp in yyyy-mm-dd hh:mm:ss:ffff
Getting
42601 syntax error "at or near" the "1" for 1 Day
EDIT:
After everyone's assistance, the following was used.
query = "DELETE FROM {} WHERE timestamp < NOW() - INTERVAL '{} DAY'".format(tableName, truncate)
cursor.execute(query)
note: I am pulling the table name and the interval from global variables
Solution 1:[1]
Put 1 day in quotations (')
INTERVAL '1 DAY'
So finally
query = "DELETE FROM currentvoltage WHERE timestamp < UNIX_TIMESTAMP(DATE_SUB(NOW() - INTERVAL '1 DAY'))"
Note that there is no function UNIX_TIMESTAMP & DATE_SUB in PgSQL
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
