Share database between 2 apps in Heroku

Ruby on-Rails-3Heroku

Ruby on-Rails-3 Problem Overview


I want to access the database of an app from another Heroku app. Is that possible in the shared database?

Ruby on-Rails-3 Solutions


Solution 1 - Ruby on-Rails-3

UPDATED

Originally, this answer stated that although this was possible with a few tricks, it was strongly discouraged. This was based on advice on the Heroku developer support website. However, recently Heroku issued a communication specifically describing how to achieve this, and have watered down their advice on the developer site. The complete text of this section of their e-mail is included below:

> Did you know that Heroku apps can > share a common database? For example, you can put analytics functions > in a separate application from your user-facing code. > > Simply set the DATABASE_URL config var for several apps to the same > value. First, get the DATABASE_URL for your existing app: > > $ heroku config | grep DATABASE_URL --app sushi DATABASE_URL => postgres://lswlmfdsfos:[email protected]/ldfoiusfsf > > Then, set the DATABASE_URL for new apps to this value: > > $ heroku config:add DATABASE_URL=postgres://lswlmfdsfos:[email protected]/ldfoiusfsf --app sushi-analytics > Adding config vars: DATABASE_URL => postgres://lswlm...m/ldfoiusfsf Restarting app... done, v74. That's it > > — now both apps will share one database.

Just as a point of reference, Heroku's original advice was to create and use an API to access data remotely. My personal view is that overall, for many situations this is good advice, (i.e. better than just connecting multiple application to the same DB) although I can see situations where that'd be more trouble than it's worth.

UPDATE

As per comments on this answer, it's worth noting that Heroku do reserve the right to change database URLs as required. If this occurs, it will cause your secondary connections to fail, and you'll need to update the URLs accordingly.

Solution 2 - Ruby on-Rails-3

Late-breaking answer to the pertinent question!

Heroku officially supports a better/graceful solution by the way of their addon framework now. It is described in their latest newsletter on how to share addons between apps. Below are the snippets mentioned in the writeup:

$ heroku addons:attach my-sushi-db -a my-sushi-reporting --as MAIN_SUSHI_DB    
  Adding MAIN_SUSHI_DB to my-sushi-reporting... done
  Setting MAIN_SUSHI_DB vars and restarting my-sushi-app-reporting... done, v3

Link: expanding_the_power_of_add_ons

Solution 3 - Ruby on-Rails-3

as far as I know it is possible - you'll have to look at the heroku config variables for your app with the DB and then set the database_url on the app that wants to share the database to the same value. Kinda off track though and as to how supported it is I don't know.

EDIT Just to set my mind at rest I've spun up two apps on Heroku - a simple scaffold 'post' with a title.

http://evening-spring-734.heroku.com/posts is the master

http://electric-galaxy-230.heroku.com/posts - is the slave

So posts created on either will be written to the database URL of evening-spring-734.

All I've done is use heroku config to get the DATABASE_URL of evening-spring-734 and then set the same value into the DATABASE_URL of electric-galaxy-230.

You could end up with some fruity DB race conditions but it is definitely possible to do.

Magic huh?

Solution 4 - Ruby on-Rails-3

I received this in the Heroku newsletter recently. I'm emailing them to find out if it really is an approved usage.

> DID YOU KNOW? > > Sharing one database with many apps > > Did you know that Heroku apps can share a common database? For example, you can put analytics functions in a separate application from your user-facing code. > > Simply set the DATABASE_URL config var for several apps to the same value. First, get the DATABASE_URL for your existing app: > > $ heroku config | grep DATABASE_URL --app sushi > DATABASE_URL => postgres://lswlmfdsfos:[email protected]/ldfoiusfsf > > Then, set the DATABASE_URL for new apps to this value: > > $ heroku config:add DATABASE_URL=postgres://lswlmfdsfos:[email protected]/ldfoiusfsf --app sushi-analytics > Adding config vars: DATABASE_URL => postgres://lswlm...m/ldfoiusfsf > Restarting app... done, v74. > That's it — now both apps will share one database.

Solution 5 - Ruby on-Rails-3

The Amazon RDS plugin works well for this. Multiple apps can share the same RDS instance.

You have the flexibility to have them share tables or avoid table name collision using the active_record.table_name_prefix.

Solution 6 - Ruby on-Rails-3

See Heroku reference: https://devcenter.heroku.com/categories/heroku-postgres

> Q: Can multiple Heroku Applications Connect to a single database? > > Yes. You can configure multiple applications running on Heroku to connect > to a single database, provided that you have the databases credentials. > Simply override the DATABASE_URL of the apps that you wish to connect using > the heroku config:add DATABASE_URL=... command.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiondonaldView Question on Stackoverflow
Solution 1 - Ruby on-Rails-3Paul RussellView Answer on Stackoverflow
Solution 2 - Ruby on-Rails-3c360ianView Answer on Stackoverflow
Solution 3 - Ruby on-Rails-3John BeynonView Answer on Stackoverflow
Solution 4 - Ruby on-Rails-3JoshRiversView Answer on Stackoverflow
Solution 5 - Ruby on-Rails-3Steve WilhelmView Answer on Stackoverflow
Solution 6 - Ruby on-Rails-3jmkView Answer on Stackoverflow