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.
Paul Russell May 12 '11 at 17:05 2011-05-12 17:05
source share