Justin Marney

Installing Postgresql 9.3 Beta using Homebrew

Recently, I've been working on a project that relies heavily on Postgresql and I've come to appreciate its flexibility. New features like hstore, json and plv8js allow for rapid application development. Index-only scans and native streaming replication make Postgresql even more appealing in a traditional context. After reading about the forthcoming features of 9.3, I went in search of a way to install 9.3 beta 1 along side my existing 9.2 installation.

The magic begins with petere's homebrew-postgresql formulae. The first step is to follow the README and brew tap the repository.

brew tap petere/postgresql

Next, install postgresql-9.3.

brew install postgresql-9.3

This will install postgresql into /usr/local/opt/postgres-9.3. It will not symlink any of the executables in /usr/local/opt/postgres-9.3/bin to /usr/local/bin because the formula was added via brew tap. This means that the existing brew-based postgres executables won't be overwritten by 9.3. It also means that there's a little more legwork involved in getting everything setup. Currently, I'm just calling the executables directly via their full path, but you could add /usr/local/opt/postgresql-9.3/bin to your PATH and avoid lots of typing.

At this point, we need to initialize the database.

/usr/local/opt/postgresql-9.3/bin/initdb /usr/local/var/postgres-9.3 -E utf8

Once that finishes you can start postgres manually using the following command.

/usr/local/opt/postgresql-9.3/bin/pg_ctl -D /usr/local/var/postgres-9.3 -l /usr/local/var/postgres-9.3/server.log start

If you want postgres to automatically start on boot, use the following plist to create a LaunchAgent.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
  <key>KeepAlive</key>
  <true/>
  <key>Label</key>
  <string>homebrew.mxcl.postgresql</string>
  <key>ProgramArguments</key>
  <array>
    <string>/usr/local/opt/postgresql-9.3/bin/postgres</string>
    <string>-D</string>
    <string>/usr/local/var/postgres-9.3</string>
    <string>-r</string>
    <string>/usr/local/var/postgres-9.3/server.log</string>
  </array>
  <key>RunAtLoad</key>
  <true/>
  <key>WorkingDirectory</key>
  <string>/usr/local</string>
  <key>StandardErrorPath</key>
  <string>/usr/local/var/postgres-9.3/server.log</string>
</dict>
</plist>

Save this plist into /usr/local/opt/postgresql-9.3/homebrew.mxcl.postgresql.plist. Then symlink this file into ~/Library/LaunchAgents using the following command.

ln -sfv /usr/local/opt/postgresql-9.3/*.plist ~/Library/LaunchAgents

Lastly, start up postgres immediately using launchctl.

launchctl load ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist

Check out the 9.3 beta 1 release notes or Postgres 9.3 feature highlight: JSON data generation for more information on some interesting new features.