0

So I have some confusion regarding Use_Tz in Django.

When the project was first started, USE_TZ was set to True, and we also had USE_I18N set to True.

However, last week it was decided to set these settings to False. According to the docs here, Postgresql should be able to switch between True and False automatically depending the settings.py file.

However, the date time fields in my tables show a timezone stamp of +01 at the end.

Also, when I go into the database directly, using the command \d survey_server_app_activitylog, I see the following:

 date_reg       | timestamp with time zone |           

All fields with date time show the same entry. But, I believe that they should be set as timestamp without time zone

So, what gives? Am I totally interpreting USE_TZ = False incorrectly? Do I need to manually set the columns in the postgres database?

Nathan
  • 291
  • 2
  • 5
  • 17

1 Answers1

1

See the section of the timezone documentation dealing specifically with PostgreSQL:

The PostgreSQL backend stores datetimes as timestamp with time zone. In practice, this means it converts datetimes from the connection’s time zone to UTC on storage, and from UTC to the connection’s time zone on retrieval.

As a consequence, if you’re using PostgreSQL, you can switch between USE_TZ = False and USE_TZ = True freely. The database connection’s time zone will be set to TIME_ZONE or UTC respectively, so that Django obtains correct datetimes in all cases. You don’t need to perform any data conversions.

That answers your specific question about why the column type hasn't changed. It's not clear what problem this is actually presenting for you, so it's hard to give further advice. If you try to change the type of the column outside of Django I would not expect correct behavior.

Community
  • 1
  • 1
Kevin Christopher Henry
  • 46,175
  • 7
  • 116
  • 102
  • Perhaps I'm interpreting it wrong then. So, am I correct in saying that PostgreSQL is always, _always_ set with ```timestamp with time zone?``` But, if that's the case, then Django's USE_TZ False/True setting will have absolutely no effect if there is no Timezone specified; in other words, it will always default to UTC if USE_TZ = False. – Nathan Mar 10 '19 at 13:04
  • @Nathan: Yes, it will always be `timestamp with time zone`. If the `TIME_ZONE` setting isn't supplied, Django will use the default `America/Chicago`, not `UTC`, as documented [here](https://docs.djangoproject.com/en/dev/ref/settings/#std:setting-TIME_ZONE). – Kevin Christopher Henry Mar 10 '19 at 13:18
  • Ok, thanks for your help. I guess I'll have to manually set the postgresql database to not use a timestamp then. – Nathan Mar 11 '19 at 00:43