How to migrate a Rails app from Heroku to Railway
Tags: rails, ruby, tutorial
Published: 2022-09-28 18:39:15

First, you need to go to [Railway](https://railway.app) and create an account if you don't have one already. After that, install the Railway CLI. If you're on Mac, this can be done with Brew. `brew install railwayapp/railway/railway`. Otherwise, you can use NPM and install with `npm i -g @railway/cli`. Next log in to the CLI with `railway login`. Then, in your projects directory, start a new project with `railway init`.  Choose Empty Project and pick a name for your project. Hit enter. It will take a second, then open your web browser and you should see a box that says Click to add service. Click that and then chose the Github repo you want to use to deploy. Next copy all your `.env` variables from Heroku except the `DATABASE_URL` we will be using the URL from the new Railway db we will create. I found it easiest to use the RAW editor to add the `.env` variables. After that is done, go back to the Dashboard(in the navbar) and click New Project, and click Provision PostgreSQL. After it is created, click on the box that says PostgreSQL, go to the connect tab and copy the connection URL. Then go back to dashboard, go back into your app, click on the box in center of the screen, and add the variable `DATABASE_URL` with the url just copied. Now the spicy part. If you have data you would like to migrate from Heroku, which you probably do, you can, rather easily! You need some information however. First, go to your app in Heroku and go to resources. Then click on Heroku Postgres. Once there, click on settings, and you should see a column that says Database Credentials. Click View Credentials. We need these credentials to get copy the data out of the db and download it to your machine. Use the following command 
```
pg_dump -h <host> -d <database> -U <user> -p <port> -W -F t > latest.dump
```
Use the db credentials from Heroku and put them in their corresponding places, and hit enter. When it prompts for the password, copy the password from the Heroku db credentials. After you get your dump file, you can then load that data into your new psql database we created earlier on Railway, with the following command:
```
pg_restore -U <username> -h <host> -p <port> -W -F t -d <db_name> <dump_file_name>
```
Go to your Railway database and grab the credentials from the env variables. The username is your `PGUSER` the host is `PGHOST` the port is `PGPORT` the db_name is `PGDATABASE` and the dump_file_name should be `latest.dump` unless you made your filename different. The password when prompted is `PGPASSWORD`. After you are done, you should be able to go to the Data tab and see all of your data that was copied over. Now you should have your app migrated over with all of it's data from Heroku, congratulations!! Now, to see it in action, go back to dashboard and click on your app. Click the app's box in the middle of the screen and go to the settings tab. You should see a little section called Domains, click Generate Domain. It should give you a link, go to that link and view your app! Hope you all enjoyed this tutorial. Resources: https://blog.railway.app/p/railway-heroku-rails