postgres install on OS X

by Jon Hancock on October 1, 2008

I use postgres 8.3.x for ShellShadow.  My production and staging servers are linux but my dev and testing is done on OS X 10.5.x.  There exist “one-click installers” for postgres on OS X.  They are good and well worth using if that’s what you need.  I need finer control and like to use an install as close to the same as what’s on my production machine.  So I install from source.  PostgreSQL installs quite easily from source but under OS X there are some little tricks that make your install cleaner and more like the production system.

Here is my recipe, tested under OS X 10.5.5 with postgres 8.3.4:

Find an unused Group and User IDs. My system has two “login users”.  I want to add another user and need to find a number not in use.  The trick is to create a new user that does not show up on the OS X login dialog.   I found tips from here: http://macosx.com/forums/mac-os-x-system-mac-software/297377-add-user-can-not-seen.html

> sudo dscl . -list /Groups PrimaryGroupID
> sudo dscl . -list /Users UniqueID

On my system, ID 503 is not in use, I’ll use this for my postgres Group and User IDs. Note: OS X should give you an error on the following commands if you screw up and pick an ID already in use.

Create OS X Group and User for postgres using ID 503 (substitute a different ID if this is taken):

> sudo dscl . create /Groups/postgres
> sudo dscl . create /Groups/postgres PrimaryGroupID 503
> sudo dscl . create /Groups/postgres RealName 'Postgres Admin Group'
> sudo dscl . create /Users/postgres
> sudo dscl . create /Users/postgres UniqueID 503
> sudo dscl . create /Users/postgres PrimaryGroupID 503
# not sure if this next command is necessary.
> sudo dscl . create /Users/postgres NFSHomeDirectory /usr/local/pgsql
> sudo dscl . create /Users/postgres Password '*'
> sudo dscl . create /Users/postgres UserShell /bin/bash
> sudo dscl . create /Users/postgres RealName 'Postgres Admin User'

Install postgres.  Download latest source from http://www.postgresql.org/ftp/source/ We’ll assume postgresql-8.3.4.tar.gz for this example.

> tar zxf postgresql-8.3.4.tar.gz
# check your umask. 0002 or 0022 is good.
> umask
> cd postgresql-8.3.4
> ./configure
> make
> sudo make install
> sudo mkdir /usr/local/pgsql/data
> sudo chown postgres:postgres /usr/local/pgsql/data
# switch to postgres user to initialize the database
> sudo su postgres
> /usr/local/pgsql/bin/initdb -D /usr/local/pgsql/data
> exit
# setup the logs directory
> sudo mkdir /usr/local/pgsql/data/logs
> sudo chown postgres:postgres /usr/local/pgsql/data/logs
> sudo chmod 700 /usr/local/pgsql/data/logs
# setup OS X launch script
# paths assume you are still in the postgresql-8.3.4 source root
> cd contrib/start-scripts/osx
> sudo /bin/sh ./install.sh
# to keep postgres from starting at OS X system start, edit /etc/hostconfig file:
# change POSTGRESQL=-YES- => POSTGRESQL=-NO-
# to manually stop postgres
> sudo /Library/StartupItems/PostgreSQL/PostgreSQL stop
# to manually start postgres
> sudo /Library/StartupItems/PostgreSQL/PostgreSQL start
# add /usr/local/pgsql/bin to your PATH
# in my ~/.bash_profile I have a line near the top which I changed to
# PATH=$HOME/bin:/usr/local/pgsql/bin:$PATH:
# after PATH change, exit shell and reopen or re-source .bash_profile
# test if you can connect
> psql -Upostgres
# type \q to quit

Install pgcrypto. Basic digest and crypto functions are not in the default compile. You must install them separately. Its best to do this now as the install requires the config options created from the core install above.

# from postgresql-8.3.4/contrib/pgcrypto
> make
> sudo make install
# The above install creates
/usr/local/pgsql/share/contrib/pgcrypto.sql which must be imported to each schema (YOUR_DB_NAME) where the functions are used.
> psql -Upostgres -d YOUR_DB_NAME -f /usr/local/pgsql/share/contrib/pgcrypto.sql

{ 3 comments… read them below or add one }

m February 21, 2009 at 8:29 am

Thanks for this post! I find it rare to move through instructions like this without stumbling on at least one step (from some hidden gotcha or exception in my particular configuration) but the installation went almost perfectly. Usually I muddle through by combining several tutorials but in this case all I needed was your post and it saved me a bunch o’ time.

BTW these instructions worked fine on 10.4 (Tiger).

John Rice April 5, 2009 at 3:26 pm

Very nice and thank you. The dscl examples are exactly what I needed to get postgres set up on Leopard.

Tim Smith October 28, 2011 at 4:10 pm

Still works for adding a user from the command line on OS X 10.6.8. Thanks!

Leave a Comment