Was hunting around for how to do this, and couldn't find any examples, but 
eventually figured it out.

Goal is to have all dates coming out of Postgres to be in UTC, regardless 
of the DBA's timezone configuration.

Key piece of code is as follows:

import sqlalchemy
def set_utc_on_connect(dbapi_con, con_record):
    c = dbapi_con.cursor()
    c.execute("SET TIME ZONE UTC")
    c.close()
sqlalchemy.event.listen(sqlalchemy.pool.Pool, 'connect', set_utc_on_connect)

Now all connections in the pool will have their timezone set to UTC, so all 
queries will return UTC for datetime objects with timezone specified.

Just posting it here incase someone else needed the info.

Cheers,
~Owen

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to