Installing Postgres on a Mac with Homebrew

I needed a postgres. I did not appear to have postgresql (server) on my mac (still Snow Leopard). My vms looked old and worn. I had some kind of psql90 client which appears to be for 9.0.1, so … not the latest. I figured I should just see what homebrew could do for me.
One of the things I like about homebrew as a package installer is that it has yet to fail me. It seems to just work; it’s super easy to use. If I have questions, answers appear easy to find. My experience installing postgres with it was no exception.
Most package installers need some kind of update command run periodically. I find I usually start with it, even though it’s not always necessary. In homebrew’s case, that command would be this:
$ brew update
which happened to give me some odd errors.
They looked like:
error: The following untracked working tree files would be overwritten by merge:
Library/Formula/haxe.rb
Library/Formula/log4shib.rb
Library/Formula/opensaml.rb
Library/Formula/spim.rb
Library/Formula/xml-security-c.rb
Library/Formula/xml-tooling-c.rb
Please move or remove them before you can merge.
Aborting
Error: Failure while executing: git pull -q origin refs/heads/master:refs/remotes/origin/master
I decided to ignore them while I was doing the install as those things didn’t seem super relevant. I later reviewed it and discovered it just means my brew’s git got out of date somehow. Here’s instructions on how to fix it. Those instructions worked like a charm. Although as a side note: I have to admit, I found the knowledge that brew adds a git remote to my /usr/local directory a little unsettling. But, I can’t argue with the results, which appear quite effective.
Next, let’s install postgres:
$ brew install postgresql
Console feedback said a bunch of things, but the one I was chiefly interested in was:
If this is your first install, create a database with:
initdb /usr/local/var/postgres -E utf8
so I did:
$ initdb /usr/local/var/postgres -E utf8
then the console finished up with:
Success. You can now start the database server using:
postgres -D /usr/local/var/postgres
or
pg_ctl -D /usr/local/var/postgres -l logfile start
I wanted to confirm installed version so, I ran:
$ postgres -V
postgres (PostgreSQL) 9.2.1
and then also:
$ psql -V
psql (PostgreSQL) 9.2.1
I wanted some reminders about how to start and stop the server, so I made a shell script for starting:
#!/bin/bash
LOGFILE="/path/to/where/I/want/my/pg.log"
pg_ctl -D /usr/local/var/postgres -l $LOGFILE start
and stopping:
#!/bin/bash
pg_ctl -D /usr/local/var/postgres stop -s -m fast
After starting it I wanted to confirm running status. First, I added an environment variable to my .bash_profile so I won’t have to keep typing the -D parameter:
export PGDATA=/usr/local/var/postgres
Then:
$ pg_ctl status
pg_ctl: server is running
Also, I can always take a peek at the logfile I specified. I like to have a tail running in a console if I’m actively developing against the database, so I can see what’s actually happening. That looks like this:
$ tail -f pg.log
A couple of follow up details: homebrew isn’t going to create the standard ‘postgres’ user. Instead it creates a superuser with the user account you installed postgres with. So you can access your postgres very easily, but if you need the standard postgres account for some reason, you’ll have to add that yourself.
Some resources:
theme by teslathemes