I'd like to use the create_engine parameters to get my timezone aware data out (in python), not as a datetime.datetime, but as a string representation of the UTC time it stores.
Specifically, I have:
engine = create_engine(DB_URI,
convert_unicode=True,
json_serializer=json.dumps)
conn = engine.connect()
res = find_all(conn, myquery)
But my dates come back like this:
datetime.datetime(2020, 4, 14, 7, 30, tzinfo=psycopg2.tz.FixedOffsetTimezone(offset=0, name=None)
While I expect it to look more like:
2020-04-14 07:30Z
What can I change so that sqlalchemy will retrieve these values as text timestamps?
I expected the following to work as well:
PGTZ = psycopg2.extensions.new_type(
psycopg2.extensions.DATEARRAY.values,
'PGTZ',
lambda value, curs: value.isoformat() if value is not None else None)
psycopg2.extensions.register_type(PGTZ)
But this had no effect.
I've consulted other references:
- Timezone aware datetime to string? This doesn't help because it converts after query time, which is not what I'm looking for
- https://access.crunchydata.com/documentation/psycopg2/2.7.5/extras.html#psycopg2.extras.register_tstz_w_secs This is promising
- https://www.postgresql.org/message-id/20130801121119.GA10036%40llserver.lakeliving.com This is about storage, not about retrieval
- https://stackoverflow.com/a/43121970/1052117 This is close, but is about flask
- https://docs.sqlalchemy.org/en/13/dialects/postgresql.html#sqlalchemy.dialects.postgresql.JSON this is close but serializes json in particular.