I have the following situation: - Postgres backend with a field
timestamp without time zone
What I suspect is happening is that you are storing aware datetime
s correctly, but are not reading it back with a time zone because the column is WITHOUT TIME ZONE
. Each PostgreSQL connection has an associated time zone that defaults to the system's time zone, so when you retrieve a particular TIMESTAMP
it gets returned as a naïve datetime
in the system's time zone. For this reason, I always recommend storing TIMESTAMP WITH TIME ZONE
instead.
If you want to change the time zone of the connection in SQLAlchemy to UTC, do the following when you create the engine:
engine = create_engine("...", connect_args={"options": "-c timezone=utc"})
This should make you read the value back as a naïve datetime
in UTC.
EDIT: @Peter The documentation does not make it obvious how to do this; I had to read several different docs and connect the dots:
connect()