views:

81

answers:

3

I need to set a run-time parameter in postgresql. It is "SET DATESTYLE TO PostgreSQL,European;". Where can I pass that in a Rails Application?

A: 

Have you tried ActiveRecord#execute ? It lets you send arbitrary sql to the database:

connection.execute("SET DATESTYLE TO PostgreSQL,European;")

I'm not sure if you need or want the semicolon there.

Jeff Paquette
It doesn't work
Hector Villalobos
+2  A: 

An initializer would be an appropriate place to do that:

# config/initializers/set_datestyle.rb

ActiveRecord::Base.connection.execute 'SET DATESTYLE TO PostgreSQL,European;'
mtyaka
+1  A: 

Don't you want the "SET DATASTYLE" statement for each new connection? Then an initializer isn't the right place.

I think you should take a look at

ActiveRecord::Base.postgresql_connection

or

ConnectionAdapters::PostgreSQLAdapter#configure_connection (private)

This ticket has a patch for a similar problem in the mysql world:

Might help you look in the right files.

On the other hand, it may be easier to set the environment variable PGDATESTYLE, see

www.postgresql.org/docs/8.2/static/datatype-datetime.html

At the end of section "8.5.2. Date/Time Output" it says "The date/time styles can be selected by the user using the SET datestyle command, the DateStyle parameter in the postgresql.conf configuration file, or the PGDATESTYLE environment variable on the server or client. The formatting function to_char (see Section 9.8) is also available as a more flexible way to format the date/time output."

Either on the server, or the client.

Stephan

Stephan Wehner