99

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

Arslan Ali
  • 17,418
  • 8
  • 58
  • 76
donald
  • 23,587
  • 42
  • 142
  • 223

6 Answers6

106

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:5FSLVUSLLT123@ec2-123-456-78-90.compute1.amazonaws.com/ldfoiusfsf

Then, set the DATABASE_URL for new apps 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. 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.

Paul Russell
  • 4,727
  • 1
  • 30
  • 28
  • 1
    just setup apps on Heroku to prove it's possible - details in my response. – John Beynon May 12 '11 at 19:33
  • @John Coo - interesting :) Not sure strictly speaking that deserves a downvote though! Heroku specifically prohibit it in their developer docs, which means it could break at some point in the future unexpectedly... – Paul Russell May 12 '11 at 20:00
  • 2
    Fair enough, down vote retracted...Heroku support give a different answer! Question: If I develop two heroku apps, can they access a single database? The reason I ask is that we have a rails app and a sinatra app, and they need to share a postgres database. Would rather not do that by http! Answer from heroku support: Yes, if you need to, you can. Every Heroku app gets a database provisioned by default, but it's possible for one app to use another app's database simply by copying the DATABASE_URL config var from one app to another. – John Beynon May 13 '11 at 08:10
  • 1
    I retracted the down vote but the accepted answer is still incorrect just as an FYI. – John Beynon May 13 '11 at 08:12
  • Yeah - understand the use cases. Interesting that Heroku support gives advice that appears to contradict the site. I'm much happier now I know they've actually said that, so am upvoting your answer. Just worries me they might drop support for that in the future, e.g. when they enable multi-region support. – Paul Russell May 13 '11 at 08:23
  • 1
    they do say they reserve the right to change your applications database URL so if that occured you'd need to change the database url you were using manually. – John Beynon May 13 '11 at 08:35
  • 1
    @Paul: I would really recommend updating your answer since it is incorrect as it stands. If I hadn't scrolled down and seen John's answer, I would have thought this was impossible. I will +1 if you update your answer, but for now I unfortunately had to -1 :( – iwasrobbed Aug 12 '11 at 22:55
  • 1
    @iWasRobbed - thanks, good call. Hadn't spotted this in the latest news letter. Have updated to remove the comments about firewalls etc (which clearly are no longer true). I note that Heroku have now removed the advice the original answer was based on from their website too, so I'm assuming that they're comfortable with this sharing of databases now. – Paul Russell Aug 15 '11 at 05:56
  • To be clear, if you share databases, you couldn't have tables with the same name, correct? – RealCasually Apr 10 '13 at 23:49
  • 1
    @Derek: unless you're specifically looking to share data, then that's true. Of course, if you're not sharing data, then sharing a database might not be buying you much benefit though! – Paul Russell Apr 11 '13 at 10:11
  • There's a blog post describing this configuration: http://www.neilmiddleton.com/sharing-databases-between-heroku-applications/ – Guilherme Garnier Jul 30 '13 at 17:37
  • 1
    Please notice what @JohnBeynon says. They might and WILL change your database_url. So if you do replicate the url manually, your application will break. Check ".profile.d scripts" to make it automatically when your app reloads. – Ron Jan 06 '14 at 10:58
  • 1
    This blog post describes syncing the DATABASE_URL between apps http://engineering.qmee.com/2014/05/06/accessing-a-database-from-multiple-apps-on-heroku/ – andorov Sep 17 '14 at 19:47
  • 8
    there is now an official heroku way to share the DATABASE_URL between apps. see the answer by @c360ian [below](http://stackoverflow.com/a/28249767/1363024). – mobeets Apr 19 '15 at 17:56
101

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

c360ian
  • 1,253
  • 1
  • 10
  • 18
  • Much thanks!, this should be the prioritized answer! – HLL Apr 28 '15 at 21:16
  • 2
    This is the right answer. All other answers are deprecated, and most of them are duplicates anyway. – jelder Aug 07 '15 at 14:20
  • 6
    The format of the command I used was `heroku addons:attach originating_app_name::HEROKU_POSTGRESQL_COLOR_URL -a no_db_app_name` – Marklar May 25 '16 at 00:34
15

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?

John Beynon
  • 37,398
  • 8
  • 88
  • 97
  • +1: I see this as being very useful for doing short or vanity URLs.. like http://myapp.me/iwasrobbed (Heroku App #1) redirects to http://myapp.com/users/1 (Heroku App #2) since now I can store the vanity name in the standard user DB. Thanks John! – iwasrobbed Aug 12 '11 at 22:34
  • 1
    heroku pg:promote DATABASE_URL --app your_app_name is the supported way of setting this variable. – Neil Middleton Dec 18 '11 at 15:52
  • my only comment is to the race conditions.. Workers and web instance would be the same, as would remote workers like iron worker or resqueue. – baash05 Feb 05 '13 at 04:33
12

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:5FSLVUSLLT123@ec2-123-456-78-90.compute-1.amazonaws.com/ldfoiusfsf

Then, set the DATABASE_URL for new apps 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.

That's it — now both apps will share one database.

Community
  • 1
  • 1
JoshRivers
  • 9,920
  • 8
  • 39
  • 39
  • 4
    I asked Heroku about using this method to get both a free Web dyno and a free Worker dyno. Here is their response. >>"Josh - we discourage doing this, but in addition there would be issues with the worker being idled. Once it was idled, it would stop doing any work and with nothing to 'wake' it like a web request, it would be useless as a worker."<< So it looks like you can use multiple apps on the same DB, but it's not very useful for free. – JoshRivers Jul 18 '11 at 20:35
  • Im using it for developing purposes, I already have thousands of rows in my live app, and the new appeareance is being developed in another smaller heroku app,so being able to share data is cool, I can develop and see thew new looks with the same data – Dvid Silva May 06 '13 at 01:06
7

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.

Steve Wilhelm
  • 6,200
  • 2
  • 32
  • 36
1

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.

Community
  • 1
  • 1
jmk
  • 27
  • 2
  • Small point that this URL doesn't work anymore (most probably since the answer dates back to 2012). Updated documentation can be found here -- https://devcenter.heroku.com/articles/heroku-postgresql#sharing-heroku-postgres-between-applications – Greg Sadetsky Apr 21 '22 at 22:30