Homebrew or Docker? Getting a psql client

Almost all of the Rails apps that we develop at Codeface have a database behind them. More often than not, that database will be PostgreSQL

Over many years, and across several different Macs, I’ve relied on Homebrew to get a PostgreSQL client installed onto my computer. Once it’s installed, I can run things like:

psql -h myhostname -U myuser -d mydatabase

Commands like that will sit in my shell history, and I’ll pull them up with Ctrl-R whenever I need them.

But recently, I realised it’s possible to achieve almost exactly the same effect without actually installing the psql command.

All you need is Docker, and we use that for everything these days anyway — so any machine I’m working on will be sure to have it available.

Instead of installing psql from Homebrew and running it with a command like the one above, I can just run:

docker run --rm -it postgres:latest psql \
  -h myhostname -U myuser -d mydatabase

The first time I run it, it takes a minute to grab the postgres:latest image from Docker Hub.

But then any time I run it again, it’s instant. There’s no noticeable difference in timing between running a native local psql command and running psql within a Docker container.

In a way, this is overkill—I’m using a full PostgreSQL Docker image just to run a little psql client command-line tool. But if I were worried about that (and I’m not), it wouldn’t be too hard to create a more lightweight image to use.

There’s something nice about not installing a bunch of stuff on my Mac just to run the psql command.

A Docker container is completely self-contained and can be thrown away after I’ve finished with it, with nothing on my system affected.

How safe is it to run a command using Docker in this way? The chain of trust feels pretty similar for both Homebrew and Docker.

I’m trusting the Homebrew community and the transparency of Homebrew being open-source to not put anything dodgy on my Mac.

And with the Docker approach, I’m trusting the PostgreSQL maintainers and Docker Hub themselves not to let anything suspect through.

I’m comfortable with both approaches, but on balance, I think running an isolated Docker container has advantages.

The fact that it’s just a temporary container sitting on Docker (which I already have and need on my Mac) and it doesn’t install anything else on my system feels like a nice approach.

Valid HTML This page was handcrafted in Brighton by Codeface