Creating a staging environment for your heroku app

Rob Faldo
2 min readAug 27, 2021

I have a side project using heroku and I want to make a staging environment to test a big change i’m making before deploying it to production. Here’s the steps:

1. Create a new heroku application with a remote name

heroku create app-name-here --remote remote-name-here

You can run git remote to see the new remote that’s added:

$ git remote
heroku
remote-name-here

2. Setup the database

I’m using postgres so I run these commands to add the database and migrate it:

$ heroku addons:create heroku-postgresql --remote remote-name-here$ heroku run rake db:migrate --remote remote-name-here

Without this I was hitting this error when deploying to heroku:

remote: -----> Preparing app for Rails asset pipeline
remote: Running: rake assets:precompile
remote: rake aborted!
remote: PG::ConnectionBad: could not connect to server: Connection refused
remote: Is the server running on host "127.0.0.1" and accepting
remote: TCP/IP connections on port 5432?
... (more logs)
...
....
remote: ! Precompiling assets failed.
remote: ! Attempted to access a nonexistent database:

3. Deploy a branch that’s not master to the new heroku app:

git push remote-name-here branch-name-here:master

Obviously if you use main instead of master branch then switch them out.

4. Copy the ENV variables from your application to staging (thanks to this blog)

1) Export existing app’s variables to config.txt

heroku config -s -a existing-heroku-app > config.txt

2) Go into config.txt and delete the DATABASE_URL line (we don’t want to overwrite that).

3) Review and push to your new app

cat config.txt | tr '\n' ' ' | xargs heroku config:set -a app-name-here

Quick note, if you don’t know your app name you can run this:

$ heroku apps:info -s --remote remote-name-here
...
web_url=https://app-name-here.herokuapp.com/
...

and it’s the bit in the url that’s bolded :)

--

--