superset: Postgresql DB Engine Error: '>=' not supported between instances of 'datetime.timedelta' and 'int'
There seems to be a problem in retrieving and using any column that is of the format: TIMESTAMP WITH TIME ZONE or just TIMESTAMP
A relational table with a column of type TIMESTAMP WITH TIME ZONE would error when run a SELECT * ... or SELECT <column_with_timestamp_type> FROM...
The SQL editor errors even with SELECT now()
The error is:
DB engine Error
postgresql error: '>=' not supported between instances of 'datetime.timedelta' and 'int'
This may be triggered by:
Issue 1002 - The database returned an unexpected error.
Expected results
Would expect the column to return properly and be usable in exploration
Actual results & Screenshots

How to reproduce the bug
- Go to SQL Lab
- run the query:
SELECT now()
Environment
my requirements.txt:
aiohttp==3.7.4.post0
alembic==1.6.5
amqp==2.6.1
apache-superset==1.2.0
apispec==3.3.2
async-timeout==3.0.1
attrs==21.2.0
Babel==2.9.1
backoff==1.11.1
billiard==3.6.4.0
bleach==3.3.1
Brotli==1.0.9
cachelib==0.1.1
celery==4.4.7
cffi==1.14.6
chardet==4.0.0
click==7.1.2
colorama==0.4.4
contextlib2==21.6.0
convertdate==2.3.2
cron-descriptor==1.2.24
croniter==1.0.15
cryptography==3.4.7
decorator==5.0.9
defusedxml==0.7.1
dnspython==2.1.0
email-validator==1.1.3
Flask==1.1.4
Flask-AppBuilder==3.3.1
Flask-Babel==1.0.0
Flask-Caching==1.10.1
Flask-Compress==1.10.1
Flask-JWT-Extended==3.25.1
Flask-Login==0.4.1
Flask-Migrate==3.0.1
Flask-OpenID==1.2.5
Flask-SQLAlchemy==2.5.1
flask-talisman==0.8.1
Flask-WTF==0.14.3
geographiclib==1.52
geopy==2.2.0
graphlib-backport==1.0.3
gunicorn==20.0.4
holidays==0.10.3
humanize==3.10.0
idna==3.2
isodate==0.6.0
itsdangerous==1.1.0
Jinja2==2.11.3
jsonschema==3.2.0
kombu==4.6.11
korean-lunar-calendar==0.2.1
Mako==1.1.4
Markdown==3.3.4
MarkupSafe==2.0.1
marshmallow==3.12.2
marshmallow-enum==1.5.1
marshmallow-sqlalchemy==0.23.1
msgpack==1.0.2
multidict==5.1.0
numpy==1.21.0
packaging==21.0
pandas==1.2.5
parsedatetime==2.6
pathlib2==2.3.6
pgsanity==0.2.9
polyline==1.4.0
prison==0.1.3
psycopg2==2.9.1
py==1.10.0
pyarrow==3.0.0
pycparser==2.20
PyJWT==1.7.1
PyMeeus==0.5.11
pyparsing==2.4.7
pyrsistent==0.18.0
python-dateutil==2.8.2
python-dotenv==0.18.0
python-editor==1.0.4
python-geohash==0.8.5
python3-openid==3.2.0
pytz==2021.1
PyYAML==5.4.1
redis==3.5.3
retry==0.9.2
selenium==3.141.0
simplejson==3.17.3
six==1.16.0
slackclient==2.5.0
SQLAlchemy==1.3.24
SQLAlchemy-Utils==0.36.8
sqlparse==0.3.0
typing-extensions==3.10.0.0
urllib3==1.26.6
vine==1.3.0
webencodings==0.5.1
Werkzeug==1.0.1
WTForms==2.3.3
WTForms-JSON==0.3.3
yarl==1.6.3
Checklist
Make sure to follow these steps before submitting your issue - thank you!
- I have checked the superset logs for python stacktraces and included it here as text if there are any.
- I have reproduced the issue with at least the latest released version of superset.
- I have checked the issue tracker for the same issue and I haven’t found one similar.
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Reactions: 3
- Comments: 36 (6 by maintainers)
Commits related to this issue
- fix #15768 by use default psycopg2 tzinfo_factory in postgres db engine with psycopg2==2.9.1 — committed to lsyarn/superset by lsyarn 3 years ago
- chore: bump py version for integration test (#16213) * bump py version for integration test * bump py * bump py * remove files * lock pylint * add not-callable — committed to apache/superset by hughhhh 3 years ago
- Downgrade psycopg2-binary to 2.8.x to avoid DATETIME column bug Issue with psycopg2 version workaround: https://github.com/apache/superset/issues/15768 Fixed in (unreleased) Superset: https://github.... — committed to scie-nz/walden by nickbp 2 years ago
- fix issue #15768 by downgrading psycopg2 * Downgrade pyscopg2 to 2.8.6, because 2.9 use datetime.timezone as tz property of datetime object, we do not need pytz._FixedOffset any more See details at... — committed to grebaza/superset by grebaza 2 years ago
- Downgraded psycopg2 version based on https://github.com/apache/superset/issues/15768 — committed to basaravikiran/working_superset by basaravikiran 2 years ago
I downgraded the version of psycopg2 and psycopg2-binary to 2.8.6 and the problem was fixed. I assume there is a problem with version 2.9.X.
Work fine after modify postgres db engine, and timezone offset problem in explore solved. Steps with docker-compose instance(version=1.3.1).
psycopg 2.9 release note psycopg2 updated to use
datetime.timezoneastzproperty of datetime object, we do not needpytz._FixedOffsetany more.update 2022-01-11
Should have been resolved by this https://github.com/apache/superset/pull/17713
After further experimentation, the issue seems to be timezones. If I removed the timezone offset with to_char(), my query works.
Update:
I was able to work around the issue with the following query:
This hack would allow me to create time-series charts where as using to_char would not. Setting a timezone and then localizing by removing the timezone is not ideal for the long term.
Dowgrading from 2.9.1 -> 2.8.6 was also the solution in my case.
Could quite possibly be related to this change listed in the 2.9 release notes?
@santicomp2014,
You should just be able to:
Following on your suggestion, I fixed it by editing the date column expression in the dataset editor window:
This has been fixed on master branch here: #17713 , closing
Glad to see there are workarounds possible, however I’d prefer to use the official Docker image posted to Dockerhub. Any idea when this fix can be made available there?
Hi !
For docker-compose-non-dev, here is my “hack”:
Before the hack
After the hack
Is this an issue that will be fixed soon? In its current state, Superset charts and datasets don’t work with any Postgresql columns of type “timestamp with time zone”.
I can replicate this issue using a fresh install of the latest docker image per the instructions here.
Screenshot below.
According to a pip freeze in the app container (superset_app), this version is using psycopg2 v. 2.9.1.
Manually replacing version 2.9.1 with 2.8.6 as suggested above resolves the issue.
@mail2lawi’s solution above (time at time zone ‘CEST’)… also was an effective fix, leading me to support the hypothesis that this issue is specifically related to columns which contain timestamptz values.
Thank you for your help all!
This library (psycopg2-binary) was not installed at all.
For me, this didn’t work. I found this “hack” : https://github.com/apache/superset/issues/15768#issuecomment-987887089
String solution does not work well, it will cause other problems with some visualizations. The real issue is that (with Postgres) dates can be missing the timezone in some way, and forcing a timezone into the output field fixes the date issue properly without making it a string. Making the main temporal date field into a string, will cause other problems.
So, for date fields, don’t use…
select date_fld, ...Add the timezone to the field…select date_fld::timestamptz at time zone 'EST',I have 2 superset’s connected in same database.
Node 1:
Node 2:
Query error:
Query 56: <class 'TypeError'> 2021-07-19 17:06:23,715:ERROR:superset.sql_lab:Query 56: <class 'TypeError'> postgresql error: '>=' not supported between instances of 'datetime.timedelta' and 'int' 2021-07-19 17:06:23,840:WARNING:superset.views.base:postgresql error: '>=' not supported between instances of 'datetime.timedelta' and 'int'This error appears when there is a column of type date/timestamp, when I remove this column it works