Database share between 2 applications in Heroku

I want to access the application database from another Heroku application. Is this possible in a shared database?

+84
ruby-on-rails-3 heroku
May 12 '11 at 16:46
source share
6 answers

UPDATED

Initially, this answer claimed that, although this was possible with a few tricks, it was very discouraged. This was based on tips on the Heroku Developer Support website. However, Heroku recently released a message that specifically describes how to achieve this, and downloaded his advice on the developer's site. The full text of this section of their email is below:

Did you know that Heroku apps can share a common database? For example, you can use the analytics functions in a separate application from your code that is addressed to the user.

Just set the DATABASE_URL configuration parameter for multiple applications at the same cost. First, get DATABASE_URL for your existing application:

$ heroku config | grep DATABASE_URL --app sushi DATABASE_URL => postgres://lswlmfdsfos:5FSLVUSLLT123@ec2-123-456-78-90.compute1.amazonaws.com/ldfoiusfsf 

Then set DATABASE_URL for new applications to:

 $ heroku config:add DATABASE_URL=postgres://lswlmfdsfos:5FSLVUSLLT123@ec2-123-456-78-90.compute-1.amazonaws.com/ldfoiusfsf --app sushi-analytics Adding config vars: DATABASE_URL => postgres://lswlm...m/ldfoiusfsf Restarting app... done, v74. That it 

- Now both applications will share the same database.

As a guideline, Heroku's initial advice was to create and use an API for remote data access. My personal opinion is that in general for many situations this is good advice (better than just connecting several applications to the same database), although I can see situations in which it will be more problems than it costs.

UPDATE

According to the comments on this answer, it is worth noting that Heroku reserves the right to change the database URLs as necessary. If this happens, your additional connections will fail and you will need to update the URLs accordingly.

+95
May 12 '11 at 17:05
source share

Late answer to the corresponding question!

Heroku officially supports the best / most elegant solution along the way of its add-on infrastructure. His latest newsletter describes how to share add-ons between apps. The following are snippets mentioned in the entry:

 $ 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

+72
Jan 31 '15 at 8:39
source share

as far as I know, this is possible - you will have to look at the heroku configuration variables for your application using the database, and then set database_url in the application that wants to share the database with the same value. Discard the track, although I don’t know how much this is supported.

EDIT Just to calm down, I created two apps on Heroku - a simple relay post with a title.

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

http://electric-galaxy-230.heroku.com/posts is a subordinate

Thus, messages created on both will be written to the database URL in the evening of spring -734.

All I did was use the heroku configuration to get the evening DATABASE_URL - spring -734, and then set the same value to the DATABASE_URL electrogalaxy-230.

As a result, you may encounter certain fruit race conditions, but it is definitely possible.

Magical huh?

+15
May 12 '11 at 18:35
source share

Recently, I got this in the Heroku newsletter. I am emailing them to find out if this is an approved use.

DID YOU KNOW?

Sharing the same database with many applications

Did you know that Heroku apps can share a common database? For example, you can put analytics functions in a separate application from your code that is addressed to the user.

Just set DATABASE_URL config var for multiple applications with the same value. First, get DATABASE_URL for your existing application:

 $ heroku config | grep DATABASE_URL --app sushi DATABASE_URL => postgres://lswlmfdsfos:5FSLVUSLLT123@ec2-123-456-78-90.compute-1.amazonaws.com/ldfoiusfsf 

Then set DATABASE_URL for new applications to this value:

 $ heroku config:add DATABASE_URL=postgres://lswlmfdsfos:5FSLVUSLLT123@ec2-123-456-78-90.compute-1.amazonaws.com/ldfoiusfsf --app sushi-analytics Adding config vars: DATABASE_URL => postgres://lswlm...m/ldfoiusfsf Restarting app... done, v74. 

What is it - now both applications will share the same database.

+12
Jul 16 '11 at 5:45 a.m.
source share

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

You have the flexibility to allow them to exchange tables or avoid table name conflicts with active_record.table_name_prefix.

+7
May 13 '11 at 3:32 a.m.
source share

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

Q: Can multiple Heroku applications connect to the same database?

Yes. You can configure multiple applications running on Heroku to connect to the same database, provided that you have database credentials. Just override the DATABASE_URL of the applications you want to connect using config heroku: add the command DATABASE_URL = ....

+2
Sep 15 '12 at 16:14
source share



All Articles