2

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.

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.