Quick tip: Developing a Rails app with a remote database

David Pedersen
Tonsser
Published in
2 min readJul 20, 2018

Our Rails app is mostly a JSON API which is great because I can do the majority of my work through tests and don’t need a Rails server running locally. But our app also has an internal admin part that we use for all kinds of miscellaneous tasks. When I’m working on that it helps to have some real data in my local database for writing the HTML templates.

However the size of our production database is currently 140 GB. I don’t know if that is large or not, but I do know that it takes forever to download and restore locally. I believe it took 2 hours last time I tried.

But it turns out you don’t need the data locally at all! I recently realized that you can set the DATABASE_URL environment variable to point to any remote database. If you then start a Rails server all the queries will go to the remote database, while all the code is running locally. So you’re able to get feedback quickly while using real data.

Here is a way to do that in one shell command:

DATABASE_URL=`heroku pg:credentials:url --app your-staging-app-name | grep "postgres://"` rails server

The only caveat is that this setup doesn’t work if you need to change your schema with a migration. I suppose you could run DATABASE_URL=... rails db:migrate but then you might have code on your staging server that isn’t compatible with the schema changes you’ve made. So in that case I would write the new migration and write as much of the code as possible through tests. When I can’t get any further I would push to staging, run the migrations and then spin up a local Rails server pointed at the staging database. At Tonsser our staging server is mostly just a test server so pushing features that aren’t finished is no big deal.

This little tip has saved me lots of hours over the last few weeks.

--

--

David Pedersen
Tonsser
Writer for

Developer at @tonsser, computer science student, martial artist, addicted to Cardistry