Report this

What is the reason for this report?

How to make Rails use a database from another droplet?

Posted on February 10, 2013

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.



This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

It’s actually quite easy to separate out the DB from your app. <br> <br>Just install it on a separate server and make sure that it’s bound to your public IP address or 0.0.0.0 instead of just 127.0.0.1 and then configure your user to be able to connect from your app server. <br> <br>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: <br>For MySQL (using the mysql2 gem): <br>DATABASE_URL=“mysql2://username:password@host:port/database” <br> <br>For Postgres (using the pg gem): <br>DATABASE_URL=“postgres://username:password@host:port/database” <br> <br>Rails looks for this variable and will use it in place of the database.yml configuration. <br> <br>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: <br> <br>host: host:port <br> <br>and that will point the database directly to that hostname. All other variables work as expected.

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.