Saying Goodbye to Heroku Postgres for Now
Tags: devops, database, postgres, heroku
Published: 2022-04-28 14:39:11

A little less than a year ago, I built [Burplist](https://burplist.me/), a free search engine for craft beer in Singapore. With the goal to [keep my infrastructure cost as low as possible](https://jerrynsh.com/how-i-built-burplist-for-free/), I started off with [Heroku Postgres free tier](https://elements.heroku.com/addons/heroku-postgresql).

With a row limit of 10,000 and a storage capacity of 1 GB, I thought that it would last me for at least a year — it didn’t.

Despite having a garbage collector service that runs on a weekly basis to remove staled rows, Heroku Postgres’s free tier simply wasn’t enough. I started looking for alternatives.

_TL;DR: I’ve migrated my side projects’ PostgreSQL to [Railway](https://railway.app?referralCode=jerrynsh) because of its pricing and ease of migration._

---

## Heroku Postgres Alternatives

After some Googling, I came across a couple of PaaS similar to Heroku, each with its own Postgres offerings:

-   [render.com/pricing](https://render.com/pricing)
-   [fly.io/docs/about/pricing/#postgresql-clusters](https://fly.io/docs/about/pricing/#postgresql-clusters)
-   [railway.app/pricing](https://railway.app/pricing)

### One stood out

I chose Railway because it makes the most economical sense for my use case. Below are some of the things that I like about Railway:

-   [Incredibly generous pricing](https://railway.app/pricing), where you would only start paying for resources usage after $10. In my case, this is a lot cheaper than using Heroku.
-   They have a [Discord community](https://railway.app/help) where you can easily get help from.
-   Rather slick and intuitive UI. On top of that, you can view and make SQL queries directly via the dashboard. Though, I would argue that using a database tool like [TablePlus](https://tableplus.com/) or [pgAdmin](https://www.pgadmin.org/) would be much more convenient.
    ![You can only view and make SQL queries on the default database name `railway`](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z3ykmoxknbhuegqn7pbq.png)

    > You can only view and make SQL queries on the default database name `railway`

-   The dashboard also provides CPU, memory, and network metrics on your database usage; which is something that is unavailable on Heroku’s free tier.
    ![Railway PostgreSQL dashboard metrics](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dejahzne6ztzw5ji0aca.png)
    > Railway PostgreSQL dashboard metrics

## How to Migrate

The migration was a piece of cake. This is easily one of the push factors that made the decision of migrating to Railway Postgres.

### Pre-requisite

1. Installed `postgresql` locally on your machine. For e.g. if you’re on Mac — `brew install postgresql`
2. Make sure you can run the `[pg_restore](http://www.postgresql.org/docs/current/static/app-pgrestore.html)` command

### Setup Railway

1. Setup a Railway account ([referral link](https://railway.app?referralCode=jerrynsh))
2. Go to your Railway dashboard and create a new project and provision PostgreSQL
   ![Take note of your database credentials under the "Connect" or "Variables" tab. You'll need them later](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r8mw1wgkumwhe9a5bmst.png)
    > Take note of your database credentials under the "Connect" or "Variables" tab. You'll need them later

### 5 Migration steps

1. Export Heroku Postgres ([reference](https://devcenter.heroku.com/articles/heroku-postgres-import-export)).

```bash
heroku pg:backups:capture -a <heroku_app_name>
heroku pg:backups:download -a <heroku_app_name>
```

1. At your current working directory, you should see `latest.dump` file

```bash
$ heroku pg:backups:capture -a <heroku_app_name>

Starting backup of postgresql-encircled-90125... done
Use Ctrl-C at any time to stop monitoring progress; the backup will continue running.
Use heroku pg:backups:info to check progress.
Stop a running backup with heroku pg:backups:cancel.
Backing up DATABASE to b001... done

$ heroku pg:backups:download -a <heroku_app_name>
Getting backup from ⬢ <heroku_app_name>... done, #1
Downloading latest.dump... ████████████████████████▏  100% 00:00 309.99KB

$ ls
latest.dump
```

1. In the same working directory with `latest.dump`, run the following command to import your downloaded database dump to your Railway Postgres. Update accordingly with your own Postgres credentials:

```bash
# NOTE:
# Keep PGDATABASE as `railway` if you want to view your tables via the Railway dashboard.
# If you insist on another database name, you will have to create your own database via psql command.
#
# E.g.:
# PGPASSWORD=$PGPASSWORD psql -h $PGHOST -U $PGUSER -p $PGPORT -d $PGDATABASE
# CREATE DATABSE your_database_name

PGPASSWORD=$PGPASSWORD pg_restore -h $PGHOST -U $PGUSER -p $PGPORT -d $PGDATABASE < latest.dump
```

1. Verify that your data is imported correctly. To do so, you can use the `psql` command as shown in the example in Step 4; or connect to your own database via a tool like pgAdmin.
2. Go to your existing apps, e.g. your Heroku app, and update the database connection URL/credentials accordingly.

That’s it!

## I Miss Heroku Dataclip

One thing that I really miss about Heroku Postgres is its ability to share query results with [Dataclips](https://devcenter.heroku.com/articles/dataclips). Heroku Dataclips is incredibly useful as you can easily:

1. Make a SQL query via the UI
2. Share the output in JSON or CSV format via a link

Once you have a sharable link to CSV, you easily import it to other SaaS such as Google Sheets. On top of that, most data processing SaaS supports CSV out of the box; this makes Dataclip incredibly useful.

Now, I’ll either have to generate my own CSV on another server or pray that the SaaS that I’m using has PostgreSQL integration (hint: most don’t).

Having this said, I am still more than happy to make this tradeoff.

### Other Uses

Besides using Railway as my PostgreSQL database server for my projects, I am also using it to host my own [umami](https://umami.is/) analytics site in combination with [Vercel](https://vercel.com/).

I’d also recommend you to check out their other offerings such as Redis, MongoDB, and also their starter project templates.

## Closing Thoughts

No, I’m not leaving Heroku entirely. Heroku has stood the test of time. I would expect a company owned by Salesforce to provide better reliability and stability.

On top of that Heroku’s [add-ons](https://elements.heroku.com/addons) often come in handy. Add-ons provide useful integration out of the box e.g. logging, search, and many more!

On the contrary, I can’t speak for the reliability and uptime of the Railway. Having that said, the projects that I am running allow me to take on this level of risk, and my experience so far has been great.

I’m rooting for [Railway](https://railway.app?referralCode=jerrynsh). I simply enjoy seeing competitions in the market.

Thanks for reading!

---

_This article was originally published on [jerrynsh.com](https://jerrynsh.com/saying-goodbye-to-heroku-postgres/)_