I am exploring django with postgres. I have encountered a strange thing. TIMEZONE
setting for my postgres connection(I am not talking about TIME_ZONE
in settings.py) is surprisingly set to None. I have set timezone to 'Asia/Kolkata'
and USE_TZ
is also True. As per django docs, when USE_TZ
is True, the connection timezone 'UTC'
by default, while it'll be equal to the value of TIME_ZONE
in settings.py, when USE_TZ
is False. But, for me in both the cases, the value of connection.timezone is empty.
I have tried with sqlite3 backend, where I am getting expected results. I don't know, why I am not getting expected behaviour with postgres.
I am using django==1.10.5 and postgres is 9.5 on 64 bit machine. I am using following command to get connection's timezone.
from django.db import connection
print (connection.timezone)
Thanks in advance.
django.db.connection
is a common abstraction that wraps the database-specific connection.
You can access the underlying database connection with connection.connection
.
In the case of PostgreSQL that will be a psycopg2 connection. The documented way to get the connection timezone with psycopg2 is get_parameter_status("TimeZone")
.
Putting it all together, this should work:
connection.connection.get_parameter_status("TimeZone")
But wait! Database connections are created lazily, so that will only work if you've already performed some kind of database operation. To be sure you have a connection you can call connection.ensure_connection()
, or, if you want a single expression:
>>> from django.db import connection
>>> connection.cursor().connection.get_parameter_status("TimeZone")
'UTC'
For reference, see the ensure_timezone()
method of the PostgreSQL database wrapper.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.