3 min read

Monitoring Heroku PostgreSQL

Use Heroku CLI to monitor Postgres

ByAmir Ardalan
Share on X
This guide is for Mac OS

So you've got a PostgreSQL database up and running on Heroku.

When developing and testing, it is critical to keep an eye on the health and usage of your database in real time. Monitoring your Postgres database via Terminal is fairly easy, but there are a lot of steps.

Before getting started, there are a couple of things to note:

  • In this guide I am using/referencing ZSH %. But everything should work just fine using Bash $.
  • This guide assumes you've already created an app and set up your PostgreSQL database. If not, follow Heroku's guide.
  • Use the command line at your own risk.

Install Homebrew

In the Terminal, run:
% /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

On MacOS you may encounter this error:

1fatal: invalid branch name: init.defaultBranch = Failed during: git init -q 2

It's essentially an issue with Homebrew attempting to initialize a git repository to clone itself, but failing. Here are the steps I took to resolve it:

  1. % cd /usr/local/Homebrew
  2. % git config --global init.defaultBranch master
  3. % /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

Homebrew should now be installed for use via Terminal on your Mac.

Install Heroku CLI and Login

To install Heroku CLI run the following commands:

  1. % brew tap heroku/brew && brew install heroku
  2. % heroku login

When prompted, press any key to be redirected to your browser to authenticate. Boom, you're in.

Connect to the Heroku App Linked to Your DB

Back in the Terminal run:
% heroku pg:info -a your-app-name

You should see something like:

1=== DATABASE_URL 2Plan: Hobby-dev 3Status: Available 4Connections: 0/20 5PG Version: 13.2 6Created: 2021-05-15 11:06 UTC 7Data Size: 9.0 MB 8Tables: 4 9Rows: 13/10000 (In compliance) 10Fork/Follow: Unsupported 11Rollback: Unsupported 12Continuous Protection: Off 13Add-on: postgresql-XXXXX-XXXXX 14

Great! You can now access your PostgreSQL data via command line.

Hobby-tier databases often have limited connections. If your connections are maxing out, investigate why, and look to modify your code to reduce requests and open connections. If you quickly need to terminate all connections and restore functionality, simply run: heroku pg:killall -a your-app-name

Using watch to Monitor Your Database

Now let's take it one step further and install and utilize watch to continuously monitor our database. watch is a simple CLI tool for monitoring data at a set interval. Let's ensure you have watch installed (if you are running ZSH you may not).

  1. % type watch will inform you if watch is installed
  2. If not installed, run: % brew install watch

Now run:
% watch heroku pg:info -a your-app-name

You now have a view of your database that refreshes every 2 seconds. ⏲

to refresh every 10 seconds, you would run: % watch -n 10 heroku pg:info -a your-app-name

Here are some additional arguments:

1-d – Highlights differences between iterations 2-h – Displays a help message, then exits 3-n secs – Specifies the interval between executions of the command in seconds 4-t – Tells watch not to display the header 5-v – Prints version information, then exits 6

That's it! Happy monitoring. 🎉

Additional Documentation

For full documentation, follow the Using the CLI guide on Heroku.

Did you enjoy this post?

© 2024 Amir Ardalan0 views