# How to configure every (postgresql) db connection?

**URL:** <https://www.ruby-forum.com/t/how-to-configure-every-postgresql-db-connection/177271>\
**Category:** Rails\
**Created:** [October 14, 2009, 12:42pm UTC](https://www.ruby-forum.com/t/how-to-configure-every-postgresql-db-connection/177271 "2009-10-14T12:42:09Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tilmann\_Singer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/c6cbf5/32.png) [@Tilmann\_Singer](https://www.ruby-forum.com/u/Tilmann_Singer)\
**Post date:** [October 14, 2009, 12:42pm UTC](https://www.ruby-forum.com/t/how-to-configure-every-postgresql-db-connection/177271/1 "2009-10-14T12:42:09Z")

</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:** ![Tilmann\_Singer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/c6cbf5/32.png) [@Tilmann\_Singer](https://www.ruby-forum.com/u/Tilmann_Singer)\
**Post date:** [October 15, 2009, 10:59pm UTC](https://www.ruby-forum.com/t/how-to-configure-every-postgresql-db-connection/177271/2 "2009-10-15T22:59:19Z")

</div>

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

```
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://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/c6cbf5/32.png) [@Tilmann\_Singer](https://www.ruby-forum.com/u/Tilmann_Singer)\
**Post date:** [October 16, 2009, 1:22pm UTC](https://www.ruby-forum.com/t/how-to-configure-every-postgresql-db-connection/177271/3 "2009-10-16T13:22:10Z")

</div>

At Thu, 15 Oct 2009 13:57:34 -0700 (PDT),  
jemminger [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> In SqlServer, in the context of a Model, you can do this:
> 
> ```
> self.connection.instance_variable_get
> 
> ```
> 
> (:@connection).handle.instance\_variable\_get(:@handle).setproperty  
> (‘CommandTimeout’, 300)
> 
> Perhaps there is something similar for Postgres.

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
