Skip to main content
Deploying Apps with

How to Migrate from Heroku to

Episode 248 · June 7, 2018

See how to migrate your Ruby apps from Heroku to

Production Deployment



Earn a free month

Migrating an application from Heroku to HatchBox is pretty straightforward. When you have your application already running on Heroku you're going to have data in that application you need to migrate over. So the first thing that you're going to need to do is go into HatchBox and create a server.

We're going to create one in digitalocean on my digitalocean account, we'll set it up in New York and then you get to choose how many servers you want to deploy to. We're just going to have a single server here but you can go in and tag it for the services that you would like it to run, so if you were using MySQL or Elasticsearch or MongoDB you could tag those and this server will have those installed.

We need a PostgreSQL and Redis and the rest of these defaults are perfect for us but feel free to change those if you need and then we're just going to choose the cheapest server size by default.

We'll go ahead and create this cluster. I've already got one set up here and we're going to then deploy our application to it. So we're going to set up the application on the server and then after we've done that we're going to migrate our database over so that way we have all of the code and everything on our cluster and it's ready to go and then once we migrate our database over, we will have everything ready. This will also create a database for us as we set this up so we have a database to import into.

I'm going to grab my code from github we're gonna use this repository and the name of my app is going to be to do list and it's going to be running PostgreSQL.

We can deploy this app and then that's going to clone a repository to the server set up, our nginx configs, create our database and do a bunch of little things to get us ready to run.

We'll all that is running let's go to our Postgres database in Heroku and create a backup so this durability tab is where you're going to find those. Your backups you want to create one as most recent as possible so you don't lose any data when you do this. Then you're going to download that. We're going to download it to our computer and then, we're going to upload it to our server on HatchBox. Now you can upload it to any of your servers on hash box definitely do one of the web servers, that would be the best option because then we'll use that server or the database server to talk to the database.

With that we can then go into our terminal to take this file that we just downloaded from Heroku and upload it to our server.

Now before we get into this we're going to need to make sure that we have SSH keys added to our server, so we can upload this file. If you go to your cluster you can click on the server here and go to the SSH keys tab and you'll be able to add an SSH key right here. I've already got mine added but you, if you haven't already added one will need to add one so that we can use the SCP command to upload to it.

So grab your IP address for the server that you want to upload this file to either definitely do one of the web servers or the database server. In my case, that's all the same server.

Then we can go to our command line and run SCP and give it the file that we just downloaded which is this one and we're going to say deploy at the IP address colon and a tilde(~) and so that's going to upload this file to the root to the home directory of our deploy user on our server.

scp dump_filename deploy@X.X.X.X:~

Are you sure you want to continue connecting (yes/no)? yes

If we run this and I'll ask us if this is a valid key and then it will upload that file and you'll see that uploaded then we can run SSH deploy at our IP address. So let me grab that again, paste that and on our server here, we will see that we have that database backup saved right here. So now we have that file on our server and we can import it into our Postgres database. So first things first, let's check our Postgres connection and make sure that everything is working.

ssh deploy@X.X.X.X

Now we've deployed our application to the server already so we should have migrated the database and I already have a database set up. So we can go into the database tab in our app on HatchBox and find all the details in the credentials in order to login to our database.

Now this is going to be a little bit different depending if your app is on the same server or multiple servers but the command that you're gonna run is exactly the same and it's printed out, here at the bottom, we're going to type this out and show you how to connect this.

$ psql -U database_username -W -h -d database_name

First thing is capital "U" is going to be the username need to capital "W" to prompt for a password the "h" option is for the host and we're gonna give it "" because our servers on these, are our databases on the same server this would be the IP address of your database server if you needed that. You can also optionally pass in the port but because we're using the default port we don't need to do that and then we need to pass in the database name as to-do-list and if we run this we can paste in our password and then we should be connected.

These are the same things that you would have to set up if you're going to use a tool, I postico, sequel Pro or some other GUI on your computer. You just set those up to go through the SSH connection as a tunnel and then connect to your database and so that's what all of these other configs up here mention.

database_name=# \dt

Then from here we can run "\dt". You'll be able to see all the database tables and this owner is your user for that application and because our application automatically migrated the database, we have some data and tables are ready. So the to these tables already ready to go and all of that has been created which means our application runs but we don't have any data in the database.

database_name=# \q

Our application's database can be restored using pretty much the same command as what we had before, except we're going to use PG restore instead, we're gonna have to pass it the same option so it knows how to connect to the database but we're going to give it a couple extra ones in order to make that connection and then import our database.

$ pg_restore -U database_username -W -h -d database_name --clean --no-acl --no-owner dump_filename
Password: *******

The other options we want here are --clean --no-acl --no-owner and then last but not least we want that file that was our Postgres dump. So we're gonna run this we're going to grab our password again and then paste that in and the command should then import your data and all of that should be good.

Then you can go into view site and you'll see that your records are imported. So all of that is worked and we have our database records just like we did on our other application. Now if you're importing other things like a MongoDB database you'll have to look up the instructions for that and if you're removing an Elasticsearch application over you'll just simply go and import your Postgres or MySQL database and then you will reindex on the HatchBox cluster so then Elasticsearch on HatchBox will have its own index which will be separate and you don't have to import that from Heroku.

Then for Redis, Redis is generally used for background jobs, maybe caching and storing some temporary data, so you can either if you want manually do the same sort of process with Redis or you can just set it up to create a new cache or new jobs or whatever unhatched box and not do any of that importing.

So Redis and Elasticsearch don't necessarily need to be migrated like a database does because that's where all of your data that lives in your database. MySQL is a very similar process if you're doing that just take a look at the Heroku export and then look up the import command for MySQL and you'll be able to do almost the exact same process for MySQL. So that's it! That's all you have to do to import your application and transfer it from Heroku to HatchBox.


Subscribe to the newsletter

Join 31,152+ developers who get early access to new screencasts, articles, guides, updates, and more.

    By clicking this button, you agree to the GoRails Terms of Service and Privacy Policy.

    More of a social being? We're also on Twitter and YouTube.