As far as I can tell that is just for setting the time. By the linked 
documentation, using server_default.

Postgres stores all datetimes as UTC and then does the conversion on query, 
depending on the timezone set in the connection. This defaults to the 
computer's timezone.

In my submission it sets the timezone on all the connections in the pool. 
>From then on all datetimes returned in queries are UTC regardless of the 
database configuration. This saves a configuration dependency while still 
allowing other connections/applications to query in other timezones if 
needed. The other added benefit is that it only happens once during 
connection setup, not at every query call.

On Wednesday, 5 June 2013 14:44:23 UTC-7, Michael Bayer wrote:
>
> we do this (for PG and SQL Server):
>
> class utcnow(expression.FunctionElement):
>     key = 'utcnow'
>     type = DateTime()
>
> @compiles(utcnow, 'postgresql')
> def _pg_utcnow(element, compiler, **kw):
>     return "TIMEZONE('utc', CURRENT_TIMESTAMP)"
>
> @compiles(utcnow, 'mssql')
> def _ms_utcnow(element, compiler, **kw):
>     return "GETUTCDATE()"
>
> you then use utcnow() as your SQL expression.
>
> just remembered i put it in the docs too: 
> http://docs.sqlalchemy.org/en/rel_0_8/core/compiler.html#utc-timestamp-function
>
>
>
> On Jun 5, 2013, at 4:51 PM, Owen M <[email protected] <javascript:>> 
> wrote:
>
> 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 [email protected] <javascript:>.
> To post to this group, send email to [email protected]<javascript:>
> .
> Visit this group at http://groups.google.com/group/sqlalchemy?hl=en.
> For more options, visit https://groups.google.com/groups/opt_out.
>  
>  
>
>
>

-- 
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 [email protected].
To post to this group, send email to [email protected].
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