PostgreSQL, Homebrew, and You

If you need to install PostgreSQL onto your Macbook, you have several options available to you nowadays. You could use the BigSQL package, or you could use, or several others. However, if you’re a geek running OSX, you’ve probably already installed Homebrew and it has a wonderful PostgreSQL package. So let’s use it, shall we?

I’m not going to walk you through installing Homebrew, so let’s just assume it’s already up and running and you’ve followed all the directions. At this point, you have /usr/local/bin/ in your $PATH and brew is up and running. So, let’s tell brew to install PostgreSQL:

$ brew install postgresql
==> Downloading
Already downloaded: /Users/doug/Library/Caches/Homebrew/postgresql-9.5.4_1.sierra.bottle.tar.gz
==> Pouring postgresql-9.5.4_1.sierra.bottle.tar.gz
==> Using the sandbox
==> /usr/local/Cellar/postgresql/9.5.4_1/bigsqln/initdb /usr/local/var/postgres
==> Caveats
If builds of PostgreSQL 9 are failing and you have version 8.x installed,
you may need to remove the previous version first. See:

To migrate existing data from a previous major version (pre-9.0) of PostgreSQL, see:

To migrate existing data from a previous minor versionn (9.0-9.4) of PostgreSQL, see:

  You will need your previous PostgreSQL installation from brew to perform `pg_upgrade`.
  Do not run `brew cleanup postgresql` until you have performed the migration.

To have launchd start postgresql now and restart at login:
  brew services start postgresql
Or, if you don't want/need a background service you can just run:
  pg_ctl -D /usr/local/var/postgres start
==> Summary
🍺  /usr/local/Cellar/postgresql/9.5.4_1: 3,147 files, 35M

As you can see, it downloaded the package, installed the binaries, and ran initdb for us! As the output tells us, we can set PostgreSQL to auto-start when we login by issuing:

$ brew services start postgresql
==> Successfully started `postgresql` (label: homebrew.mxcl.postgresql)

And if you check your process listing, you can see that the cluster is up and running under your id:

$ ps -efw|grep postgres
  501  6808     1   0 11:03AM ??         0:00.02 /usr/local/opt/postgresql/bin/postgres -D /usr/local/var/postgres
  501  6817  6808   0 11:03AM ??         0:00.00 postgres: logger process
  501  6819  6808   0 11:03AM ??         0:00.00 postgres: checkpointer process
  501  6820  6808   0 11:03AM ??         0:00.00 postgres: writer process
  501  6821  6808   0 11:03AM ??         0:00.00 postgres: wal writer process
  501  6822  6808   0 11:03AM ??         0:00.00 postgres: autovacuum launcher process
  501  6823  6808   0 11:03AM ??         0:00.00 postgres: stats collector process

And just like that, you have PostgreSQL installed and running! Set $PGDATA to /usr/local/var/postgres and you’re all set.