# Multi databases in rails 3

**URL:** <https://www.ruby-forum.com/t/multi-databases-in-rails-3/191360>\
**Category:** Rails\
**Created:** [July 10, 2010, 9:16am UTC](https://www.ruby-forum.com/t/multi-databases-in-rails-3/191360 "2010-07-10T09:16:41Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![trung](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/4bbf92/32.png) [@trung](https://www.ruby-forum.com/u/trung)\
**Post date:** [July 10, 2010, 9:16am UTC](https://www.ruby-forum.com/t/multi-databases-in-rails-3/191360/1 "2010-07-10T09:16:41Z")

</div>

Is there any support for multiple databases in Rails 3? Either  
natively through ActiveRecord or third party gem?

Thanks.

---

<div class="post-metadata">

**Author:** ![trung](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/4bbf92/32.png) [@trung](https://www.ruby-forum.com/u/trung)\
**Post date:** [July 10, 2010, 10:28pm UTC](https://www.ruby-forum.com/t/multi-databases-in-rails-3/191360/2 "2010-07-10T22:28:36Z")

</div>

trung wrote:

> Is there any support for multiple databases in Rails 3? Either  
> natively through ActiveRecord or third party gem?

I haven’t looked at Rails 3 yet but since being able to access  
multiple databases is a core feature of Rails 1 and Rails 2 I feel  
confident in saying that if Rails 3 didn’t have it then it would be a  
regression and therefore very unlikely. 🙂

Bob

---

<div class="post-metadata">

**Author:** ![trung](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/4bbf92/32.png) [@trung](https://www.ruby-forum.com/u/trung)\
**Post date:** [July 14, 2010, 3:24am UTC](https://www.ruby-forum.com/t/multi-databases-in-rails-3/191360/3 "2010-07-14T03:24:50Z")

</div>

You guessed right. I want to connect to multiple schemas.

Like you said, we had to jump through hoops to get Rails 2 to work  
with multiple databases, especially with the schema migrations between  
multiple databases.

I see that there’s a project called DBCharmer that solves all these  
problems for Rails 2. I wish we had known about it before.  
Unfortunately, DBCharmer doesn’t work with Rails 3.

It would be awesome of Rails 3 addresses all the problems associated  
with using multiple databases out of the box.

---

<div class="post-metadata">

**Author:** ![trung](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/4bbf92/32.png) [@trung](https://www.ruby-forum.com/u/trung)\
**Post date:** [July 11, 2010, 2:51pm UTC](https://www.ruby-forum.com/t/multi-databases-in-rails-3/191360/4 "2010-07-11T14:51:11Z")

</div>

If you mean can rails connect to different db’s like mysql, oracle,  
etc, then  
the answer is yes, but my guess is that you are  
asking if there is support to connect your app to different schema’s.

I’d like to know this too. My app has a master database, and then  
each  
customer has their own schema that contains their specific data.  
We had to jump through some hoops to make it work in rails so  
hopefully  
there is something in rails 3 to make life easier. I haven’t been  
able to  
find any info on this so I’m guessing there isn’t anything coming.

---

<div class="post-metadata">

**Author:** ![trung](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/4bbf92/32.png) [@trung](https://www.ruby-forum.com/u/trung)\
**Post date:** [July 14, 2010, 4:01pm UTC](https://www.ruby-forum.com/t/multi-databases-in-rails-3/191360/5 "2010-07-14T16:01:52Z")

</div>

jmamma wrote:

> If you mean can rails connect to different db’s like mysql, oracle,  
> etc, then  
> the answer is yes, but my guess is that you are  
> asking if there is support to connect your app to different schema’s.
> 
> I’d like to know this too. My app has a master database, and then  
> each  
> customer has their own schema that contains their specific data.

That’s a bad idea. Generally speaking, all data should be in the same  
schema.

> We had to jump through some hoops to make it work in rails

This is another example of Rails making bad things hard. Don’t do bad  
things!

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![trung](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/4bbf92/32.png) [@trung](https://www.ruby-forum.com/u/trung)\
**Post date:** [July 15, 2010, 1:27am UTC](https://www.ruby-forum.com/t/multi-databases-in-rails-3/191360/6 "2010-07-15T01:27:15Z")

</div>

I don’t think this is a bad idea at all. People just have different  
need.

For example, if you’re building an eCommerce site.

You might want to clone the Catalog database that holds all static  
products information and deploy it on each front end app server. But  
all the app servers share a centralized Order database.

This is just one example, there are many other use cases for multiple  
databases…

Just because it’s hard to do, doesn’t mean it’s a bad idea…

---

<div class="post-metadata">

**Author:** ![trung](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/4bbf92/32.png) [@trung](https://www.ruby-forum.com/u/trung)\
**Post date:** [July 15, 2010, 2:26am UTC](https://www.ruby-forum.com/t/multi-databases-in-rails-3/191360/7 "2010-07-15T02:26:29Z")

</div>

trung wrote:

> I don’t think this is a bad idea at all. People just have different  
> need.

A database per customer is bad.

> For example, if you’re building an eCommerce site.
> 
> You might want to clone the Catalog database that holds all static  
> products information and deploy it on each front end app server. But  
> all the app servers share a centralized Order database.

Then you need clustering and replication support in the DB, not in  
Rails.

> This is just one example, there are many other use cases for multiple  
> databases…
> 
> Just because it’s hard to do, doesn’t mean it’s a bad idea…

---

<div class="post-metadata">

**Author:** ![trung](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/4bbf92/32.png) [@trung](https://www.ruby-forum.com/u/trung)\
**Post date:** [July 15, 2010, 3:03am UTC](https://www.ruby-forum.com/t/multi-databases-in-rails-3/191360/8 "2010-07-15T03:03:14Z")

</div>

It obviously depends on your needs. We are connecting to multiple  
databases simply because we have to pull information from different  
systems.  
We have a main database where most Rails related data lives but we are  
also connecting to a second system which belongs to our PACS image  
storage system to pull information from it. Haven’t tried out Rails 3  
yet with multiple connections, initial tests showed some problems with  
it, but I will definitely come back to it and it is critical for us to  
get it working. Migrations were no issue for us since we were only  
reading from the PACS system.

Daniel
