# Multiple database support

**URL:** <https://www.ruby-forum.com/t/multiple-database-support/195432>\
**Category:** Rails\
**Created:** [September 23, 2010, 7:15am UTC](https://www.ruby-forum.com/t/multiple-database-support/195432 "2010-09-23T07:15:20Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![nitin\_k\_m](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/f475e1/32.png) [@nitin\_k\_m](https://www.ruby-forum.com/u/nitin_k_m)\
**Post date:** [September 23, 2010, 7:15am UTC](https://www.ruby-forum.com/t/multiple-database-support/195432/1 "2010-09-23T07:15:20Z")

</div>

Hi All,  
Could you please help me getting a way to support multiple  
databases with rails application. I found that with ruby application we  
can connect only one database at a time by either specifying database  
information in database.yml file or overrriding it in activerecord  
subclasses.

My requirement is:

Suppose I have three databases: db1, db2, db3. All these three databases  
have same database schema (structure). When a user logs in to the  
application he will be asked to which database he wants to connect. And  
the application will be connected to selected database accordingly.

I am able to connect to databases dynamically using activerecord  
establish\_connection method. But the problem is When user1 logs in and  
opt to connect to db1, he finds everything working properly. Now if  
user2 logs in and connects to database db2, user2 will find everything  
fine but now user1 also finds himself connected to db2 instead of db1.  
i.e. rails application connects to only one database at a time.

Is there a way to connect to multiple databases based on different user  
sessions.  
Please suggest me method to do so.

Thanks  
Nitin
