How to make Rails use a database from another droplet?

February 10, 2013 1.3k views
Separating the database from the actual Rails app is probably the first thing everyone will do when they want to scale their app, but still, I'm unable to find a tutorial on how to do it. It will be great if you can include one in the "Articles and Tutorials" section or at least give me a link to a page that explains how to solve this problem. Thanks. PS: I'll probably use Nginx as an web server, PostgreSQL as RDBMS so it's better if the tutorial will use them.
2 Answers
It's actually quite easy to separate out the DB from your app.

Just install it on a separate server and make sure that it's bound to your public IP address or instead of just and then configure your user to be able to connect from your app server.

On your app server just specify the IP of your remote DB server instead of localhost and you're all set.
You can set the DATABASE_URL environment variable:
For MySQL (using the mysql2 gem):

For Postgres (using the pg gem):

Rails looks for this variable and will use it in place of the database.yml configuration.

If you'd rather set it in your database.yml file (that's all great and all, but don't check it into source control!!!), just set:

host: host:port

and that will point the database directly to that hostname. All other variables work as expected.
Have another answer? Share your knowledge.