# How to configure every (postgresql) db connection?

**URL:** <https://discuss.rubyonrails.org/t/how-to-configure-every-postgresql-db-connection/46825>\
**Category:** rubyonrails-talk\
**Created:** [October 14, 2009, 10:16am UTC](https://discuss.rubyonrails.org/t/how-to-configure-every-postgresql-db-connection/46825 "2009-10-14T10:16:19Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tilmann\_Singer](https://avatars.discourse-cdn.com/v4/letter/t/c6cbf5/32.png) [@Tilmann\_Singer](https://discuss.rubyonrails.org/u/Tilmann_Singer)\
**Post date:** [October 14, 2009, 10:16am UTC](https://discuss.rubyonrails.org/t/how-to-configure-every-postgresql-db-connection/46825/1 "2009-10-14T10:16:19Z")

</div>

I would like to set some db session variables specific to my application on the database connection that rails uses. They can be set by issuing an SQL command like "SET statement\_timeout = 1000" once - it will be in effect for every subsequent SQL statement that uses this connection.

So I tried to put the following into an initializer:

ActiveRecord::Base.connection.execute("SET statement\_timeout = 1000")

but this does not have the desired effect - it is not set for all connections. I assume this has to do with the connection pool rails uses - the code above just sets it for a single one.

The PostgreSQLAdapter issues some SET statements itself for each new connection - is there a good way to add your own configuration statements, either specific to the PostgreSQLAdapter or in a general way?

Til

---

<div class="post-metadata">

**Author:** ![jeff\_emminger](https://avatars.discourse-cdn.com/v4/letter/j/aca169/32.png) [@jeff\_emminger](https://discuss.rubyonrails.org/u/jeff_emminger)\
**Post date:** [October 15, 2009, 8:57pm UTC](https://discuss.rubyonrails.org/t/how-to-configure-every-postgresql-db-connection/46825/2 "2009-10-15T20:57:34Z")

</div>

In SqlServer, in the context of a Model, you can do this:

&nbsp;&nbsp;&nbsp;&nbsp;self.connection.instance\_variable\_get (:@connection).handle.instance\_variable\_get(:@handle).setproperty ('CommandTimeout', 300)

Perhaps there is something similar for Postgres.

---

<div class="post-metadata">

**Author:** ![Tilmann\_Singer](https://avatars.discourse-cdn.com/v4/letter/t/c6cbf5/32.png) [@Tilmann\_Singer](https://discuss.rubyonrails.org/u/Tilmann_Singer)\
**Post date:** [October 16, 2009, 11:20am UTC](https://discuss.rubyonrails.org/t/how-to-configure-every-postgresql-db-connection/46825/3 "2009-10-16T11:20:12Z")

</div>

I used to do this in the context of a model which I assume has a similar effect:

self.connection.execute("SET statement\_timeout 300")

but I doubt that this is reliably configuring \_all\_ connections of the AR connection pool, and I'd rather do it in an app-wide configuration file and not in a model.

Til
