Skip to content
Advertisement

python psycopg2 select current_timestamp problem with timezone

I’m calling a simple select to obtain current timestamp with timezone with psycopg2 and it’s retrieving UTC time instead of my local time (-3).

datetime.datetime(2021, 1, 13, 20, 49, 47, 931834, tzinfo=psycopg2.tz.FixedOffsetTimezone(offset=0, name=None))

In postgresql I’m doing:

JavaScript

This retrieves (Argentina time -3):

JavaScript

So this is correct, but in Python:

JavaScript

Select method retrieves:

JavaScript

So datetime object is comming with offset 0, i.e UTC. Is it possible to retrieve current timestamp in psycopg2 with the proper timezone? If not, how can I convert the datetime object timezone?

Advertisement

Answer

I solved this issue in this way: I added a method in the class init to set the timezone. In this way, the SELECT statements are giving the proper time.

JavaScript

Result logged in python is:

JavaScript

This is correct (Argentina time now).

Extra info: I based on the psycopg documentation, in the example is telling the timezone first, before the query. I think the select current_time in this library is working in UTC by default.

Source: https://www.psycopg.org/docs/usage.html#time-zones-handling

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement