Configurations environment timezones Django postgres linux

Asked

Viewed 2,812 times

1

Good morning to all.

I have a website (Django) in a Linux hosting service with SQL Postgre bank that is in New Jersey (USA). Users are in Brazil, therefore I have a difference of Timezone.

I’m sure that this is something very silly, with an adjustment I solve everything, but I’m confused because the changes and tests I made gave incorrect results.

First I will post the settings of the environments.

Linux Server (USA):

$cat /etc/timezone
Etc/UTC

Postgres Console (USA):

postgres=# show timezone;
UTC

Django Settings.py (USA):

TIME_ZONE = 'America/Sao_Paulo'
USE_TZ = True

But now for the results.

Here in Brazil are:

10:07 am

There on the Linux server (USA):

12:07:52 UTC

Here it is already wrong, there are 3 hours and difference, 2 hours with daylight time?

In the POSTGRES:

select now();
2018-01-16 12:07:36.967415+00

I have a Model called COLLECTION that has a field that is called DATA_LEITURA.

data_leitura = models.DateTimeField(null=True)

So I just entered a record in this table at 10:07:16. I create the datetime that I attribute to the COLETA.data_leitura model is this one:

br_tz = timezone('America/Sao_Paulo')
coleta.data_leitura = datetime(2018, 01, 16, 10, 07, 16, tzinfo=br_tz)

I accessed the linux console (NJ) in postgres and gave a select:

SELECT data_leitura FROM coleta;
2018-01-16 15:13:16+00

The value displayed on the web page for the client here in Brazil is:

15:13:16

Before there was no line br_tz = Timezone('America/Sao_paulo') Then a message appeared from NAIVE DATETIME and the time was also wrong!

I’m trying to change each configuration and it always goes wrong even more, that means there are 2 wrong factors there to correct. It’s my first experience with these environments and frameworks.

What should I change? suggestions? Thanks in advance for the help of the community!

1 answer

1

You can set up an environment variable called TIMEZONE, which informs the database the region in which the client connected to the server, this makes the server know how to convert the hours properly:

SET TIMEZONE = 'America/Sao_Paulo';
SELECT NOW();
  • 1

    I changed in /etc/postgresql/9.5/main/postgresql.conf the value to Timezone="America/Sao_paulo", and the time appeared more wrong yet, now that there are 12:23 there in the bank is being recorded and displayed as 17:23h (5 hours difference), that means that this setting may be correct, but someone else is hurting the value of this one. I tried to switch to New_york also to see how it works and leave UTC. All return wrong values!

  • Vc is changing the server Timezone configuration. You need to change your client configuration.

  • The client opens a Django(html) page that shows the data brought directly from the.filter() Collection, I do not do any commands about this change. I simply put there in the/html template {{collection.data_reading}}, how would I make this configuration? I thought that the TIME_ZONE of Django would already do it alone, whenever it displays, will be the BR time. Besides because I will not have users there in the USA, only the server is there.

  • From what I understand the problem is in the INSERTION of the data, because the SELECT I give in the postgres already shows that the Insert has written the date already wrong (converted alone).

  • If by chance the die has already been entered in the database using the incorrect Timezone, changing the client and/or server settings will make the problem worse. First of all, you must fix the data that was recorded with the incorrect Timezone.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.