I have always used MySQL but I wanted to give PostgreSQL a whirl. This is what I did.

Prerequisites

Now I don’t want to go too deep into this, so I’m going to assume that if you are reading this, that you have the following installed

So now that you have all of those dependencies out of the way, let’s get to installing us some PostgreSQL.

PostgreSQL

To install the database software, simply run the following command in Terminal

sudo port install \
  postgresql83 \
  postgresql83-server

This will install the client and server needed for you to run this on your machine. Now pay special attention to the installation script. At the end of installation processes, macports will display a message for you that contains some commands for you to get started with.

Configuration

First, execute these lines of code. This will setup a new database for you to play with

sudo mkdir -p /opt/local/var/db/postgresql83/defaultdb
sudo chown postgres:postgres /opt/local/var/db/postgresql83/defaultdb
sudo su postgres -c '/opt/local/lib/postgresql83/bin/initdb -D /opt/local/var/db/postgresql83/defaultdb'

Start PostgreSQL Automatically

By running the next command, you can have PostgreSQL lunch automatically so that it’s always available to you

sudo launchctl load -w /Library/LaunchDaemons/org.macports.postgresql83-server.plist

pgAdmin III

pgAdmin III is a PostgreSQL management and administrations piece of software. It’s a nice GUI to some things that you may find yourself wanting to do, like created databases and whatnot. Installation of this is pretty cake.

sudo port install pgAdmin3

Upon completion of installation, you can find the application in /Applications/MacPorts/pgAdmin3

PostgreSQL Libraries for Ruby via Gems

I have to admit, the previous installation steps were nothing to cry about; however, it was this step that made me want to put together this little walkthrough.

sudo env ARCHFLAGS="-arch i386" \
  gem install postgres -- \
  --with-pgsql-lib=/opt/local/lib/postgresql83 \
  --with-pgsql-include=/opt/local/include/postgresql83

That’s not exactly your everyday gem install command. For one, the environment variable ARCHFLAGS has to be set. Secondly, the gem command needs to know where the PostgreSQL libraries and include headers are so that it can compile. Looking back, it’s actually quite easy to copy and paste that command, it just took me a while to find out exactly what I needed to make it work.

Fin

Well, I hope that helps anyone looking to install and setup PostgreSQL in Leopard. Until next time…

42 Responses to “Installing PostgreSQL on Leopard using MacPorts”

  1. March 22nd, 2008 at 3:56 pm #Jim

    Hi Adam – terrific post. And exactly what I am doing today. The install line for the ruby adapter had me pulling my hair out yesterday. Thanks!!

    There’s a hiccup in the post layout that prevents your suggestion for auto-starting postgres and marking up pgAdmin. I can read it from the page source: the closing > on the pre tag is missing.

    Many thanks!!! Jim.

  2. March 23rd, 2008 at 7:20 am #K. Adam Christensen

    Thanks for the heads up on that missing >.

  3. April 1st, 2008 at 7:10 pm #Fred Nerk

    I’m getting an error trying to initdb the database with the postgres account. The error is “su: /dev/null: Permission denied”. That seems to me that there is no user called postgres to su to, or that the postgres user is set to no-login by setting its shell to /dev/null.

    So I think the instructions need another step about how to set up a “postgres” user if you don’t already have one. Thanks.

  4. April 2nd, 2008 at 10:46 am #K. Adam Christensen

    I actually didn’t set up a postgres user so that has perked my curiosity. I will have to look into this once I get home. Thanks.

  5. April 4th, 2008 at 8:05 pm #Robert

    Awsome!
    Thanks for the taking the time to put this together.

  6. April 20th, 2008 at 12:43 pm #Benjamin Keating

    nice! thanks for the simple walk through, I use MySQL but thought I’d give this a try today.

  7. May 9th, 2008 at 8:38 am #Fabien Jakimowicz

  8. May 14th, 2008 at 12:49 pm #Peter Jones

    The solution to the /dev/null error:

    > sudo dscl . -create /Users/postgres UserShell /bin/sh
    > sudo dscl . -create /Users/postgres NFSHomeDirectory /opt/local/var/db/postgresql83

  9. May 15th, 2008 at 8:17 am #K. Adam Christensen

    Thanks Peter!

  10. June 19th, 2008 at 8:46 pm #Mark Catley

    FYI. If you put pg_config in your path (if not you should put it in there by adding /opt/local/lib/postgresql83/bin to your path)
    –with-pgsql-lib=/opt/local/lib/postgresql83 \
    –with-pgsql-include=/opt/local/include/postgresql83

    is not required.

  11. June 27th, 2008 at 4:33 am #Steve Purcell

    With rubygems 1.2.0, installing the postgres gem using the above command does not work.

    I have a macports Ruby 1.8.6 with that rubygems version, and the postgres gem apparently won’t build, while the same command works fine with Apple’s preinstalled Ruby + rubygems.

    Odd.

  12. July 3rd, 2008 at 1:36 pm #Alex

    I didn’t have any problems installing the postgres gem under gem 1.2.0 and 1.8.6 p114

    $ sudo env ARCHFLAGS=”-arch i386″ \
    > gem install postgres — \
    > –with-pgsql-lib=/opt/local/lib/postgresql83 \
    > –with-pgsql-include=/opt/local/include/postgresql83
    Building native extensions. This could take a while…
    Successfully installed postgres-0.7.9.2008.01.28
    1 gem installed
    Installing ri documentation for postgres-0.7.9.2008.01.28…
    Installing RDoc documentation for postgres-0.7.9.2008.01.28…

  13. July 7th, 2008 at 2:30 pm #K. Adam Christensen

    Thanks for the heads up Alex

  14. July 13th, 2008 at 10:09 am #Phoenix

    Any idea of how to make sure postgresql and PHP work together? Thanks!

  15. July 14th, 2008 at 8:48 pm #Phoenix

    How can I manually start postgres after I have installed it this way? I do not wish to have it start automatically every time as I do not use it all the time. Thanks! (Please CC my email address if you reply. Thanks)

  16. July 17th, 2008 at 6:08 pm #djwonk › Setup PostgreSQL on Mac OS X

    [...] post was partly inspired by the March 2008 article by ShiftEleven. This was written by David. Posted on Thursday, July 17, 2008, at 5:45 pm. Filed under Software. [...]

  17. October 15th, 2008 at 3:14 pm #slothbear

    Many thanks — exactly what I needed to get up and running.

  18. October 17th, 2008 at 11:01 am #Darren Oh

    The “su: /dev/null: Permission denied” can be avoided by setting the postgres UserShell to /usr/bin/false.

  19. November 6th, 2008 at 3:34 pm #jpemberthy

    Thanks!

  20. November 22nd, 2008 at 2:57 pm #John Baker

    I had postgresql81 installed via macports on my G5 leopard already so I just adapted your command like this..

    sudo env ARCHFLAGS=”-arch Power Macintosh” \
    gem install postgres — \
    –with-pgsql-lib=/opt/local/lib/postgresql81 \
    –with-pgsql-include=/opt/local/include/postgresql81

    However, I this happens..

    osx:redmine john$ sudo env ARCHFLAGS=”-arch Power Macintosh” \
    > gem install postgres — \
    > –with-pgsql-lib=/opt/local/lib/postgresql81 \
    > –with-pgsql-include=/opt/local/include/postgresql81
    Bulk updating Gem source index for: http://gems.rubyforge.org
    Building native extensions. This could take a while…
    ERROR: Error installing postgres:
    ERROR: Failed to build gem native extension.

    /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin/ruby extconf.rb install postgres — –with-pgsql-lib=/opt/local/lib/postgresql81 –with-pgsql-include=/opt/local/include/postgresql81
    extconf.rb:73: command not found: pg_config –bindir
    extconf.rb:46: command not found: pg_config –includedir
    extconf.rb:53: command not found: pg_config –libdir
    checking for main() in -lpq… no
    *** extconf.rb failed ***
    Could not create Makefile due to some reason, probably lack of
    necessary libraries and/or headers. Check the mkmf.log file for more
    details. You may need configuration options.

    Provided configuration options:
    –with-opt-dir
    –without-opt-dir
    –with-opt-include
    –without-opt-include=${opt-dir}/include
    –with-opt-lib
    –without-opt-lib=${opt-dir}/lib
    –with-make-prog
    –without-make-prog
    –srcdir=.
    –curdir
    –ruby=/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin/ruby
    –with-pgsql-dir
    –without-pgsql-dir
    –with-pgsql-include=${pgsql-dir}/include
    –with-pgsql-lib=${pgsql-dir}/lib
    –with-pqlib
    –without-pqlib
    Could not find PostgreSQL build environment (libraries & headers): Makefile not created

    Gem files will remain installed in /Library/Ruby/Gems/1.8/gems/postgres-0.7.9.2008.01.28 for inspection.
    Results logged to /Library/Ruby/Gems/1.8/gems/postgres-0.7.9.2008.01.28/ext/gem_make.out

    —-

    I don’t understand as the include and lib are definitely there on my machine..

    Any clues?

    In case it helps.. I have the following gems installed (I upgraded rails)..

    osx:redmine john$ gem list

    *** LOCAL GEMS ***

    actionmailer (2.1.0, 1.3.6, 1.3.3)
    actionpack (2.1.0, 1.13.6, 1.13.3)
    actionwebservice (1.2.6, 1.2.3)
    activerecord (2.1.0, 1.15.6, 1.15.3)
    activeresource (2.1.0)
    activesupport (2.1.0, 1.4.4, 1.4.2)
    acts_as_ferret (0.4.1)
    capistrano (2.0.0)
    cgi_multipart_eof_fix (2.5.0, 2.2)
    daemons (1.0.9, 1.0.7)
    dnssd (0.6.0)
    fastthread (1.0.1, 1.0)
    fcgi (0.8.7)
    ferret (0.11.4)
    gem_plugin (0.2.3, 0.2.2)
    highline (1.2.9)
    hpricot (0.6)
    libxml-ruby (0.3.8.4)
    mongrel (1.1.4, 1.0.1)
    needle (1.3.0)
    net-sftp (1.1.0)
    net-ssh (1.1.2)
    rails (2.1.2, 2.1.0, 1.2.6, 1.2.3)
    rake (0.8.3, 0.7.3)
    RedCloth (3.0.4)
    ruby-openid (1.1.4)
    ruby-yadis (0.3.4)
    rubynode (0.1.3)
    sources (0.0.1)
    sqlite3-ruby (1.2.1)
    termios (0.9.4)

  21. November 22nd, 2008 at 3:57 pm #John Baker

    I got it!!!… For those who want to know I had to change it to this..

    sudo env ARCHFLAGS=”-arch ppc” \
    env PATH=$PATH:/opt/local/lib/postgresql81/bin \
    gem install postgres — \
    –with-pgsql-lib=/opt/local/lib/postgresql81 \
    –with-pgsql-include=/opt/local/include/postgresql81

    Phew…

  22. February 15th, 2009 at 3:21 pm #PostgreSQL, Ruby, and Rails: WIN at Josh Kim dot Org

    [...] yet another great walkthrough, you’re going to want to install it in this manner on an Intel Mac (if you installed [...]

  23. February 21st, 2009 at 3:52 pm #Niall

    Nice post. Would recommend a final bullet about putting the PostgreSQL install bin directory into the system PATH using a file in /etc/paths.d:

    [niallsmart@Nialls-Air-3 ~]$ cat /etc/paths.d/PostgreSQL
    /opt/local/lib/postgresql83/bin

  24. February 24th, 2009 at 8:13 am #hill180

    Thanks for the post!

    Just FYI,
    I usually only want the server to start when I am developing. I added the following to my .profile file and I can stop, start and get a status easily by just typing pgstart, pgstop and pgstatus

    alias pgstart=”sudo su postgres -c ‘/opt/local/lib/postgresql83/bin/pg_ctl -D /opt/local/var/db/postgresql83/defaultdb -l /opt/local/var/log/postgresql83/postgres.log start’”
    alias pgstop=”sudo su postgres -c ‘/opt/local/lib/postgresql83/bin/pg_ctl -D /opt/local/var/db/postgresql83/defaultdb stop -m fast’”
    alias pgstatus=”sudo su postgres -c ‘/opt/local/lib/postgresql83/bin/pg_ctl status -D /opt/local/var/db/postgresql83/defaultdb’”

    Just a hint for those who might want it.

  25. May 10th, 2009 at 1:23 pm #cult hero

    Thanks for this guide. I’ve been experimenting with getting the Postgres gem to work with Ruby 1.9.1 and while I got it working fine on Ubuntu, getting it to work on OS X was a little bit more complicated. Thanks to your guide, a slightly modified step did the trick:

    sudo env ARCHFLAGS=’-arch i386′ POSTGRES_INCLUDE=’/opt/local/include/postgresql83′ POSTGRES_LIB=’/opt/local/lib/postgresql83′ ruby extconf.rb

    (Since I had to modify extconf.rb, libpq-compat.c and postgres.c somewhat I have to do the build steps manually, running “gem” overwrites all my changes.)

  26. July 8th, 2009 at 3:05 pm #Ceres

    Thanks for this. It helped a lot.

  27. July 13th, 2009 at 10:13 pm #gabe

    Thanks for this tutorial! it helped a lot!

    Now to figure out pgAdmin…

  28. October 18th, 2009 at 7:11 pm #links for 2009-10-18 « Mike Does Tech

    [...] Installing PostgreSQL on Leopard using MacPorts – Blog – ShiftEleven sudo port install pgAdmin3 [...]

  29. November 15th, 2009 at 5:57 am #Blake

    I was running into issues getting Postgres working nicely with GeoDjango and postgis, and found this tutorial very helpful. Postgres has GIS and spatial mapping capabilities that I am now beginning to explore. The tutorial here is the perfect complement for those interested in hooking Postgres to Django and GeoDjango on Mac OS X.
    If interested, check out:
    http://geodjango.org/docs/install.html

  30. January 17th, 2010 at 10:14 pm #josh

    i was having problems with this and then i tried installing the ruby-pg gem instead. also using macports on snow leopard your arch will be x86_64 and not i386.

    sudo env ARCHFLAGS=”-arch x86_64″ gem install –remote ruby-pg

  31. January 20th, 2010 at 7:11 am #Warren Buffett

    If you have older MacPorts you have to do a Macports update

    sudo port selfupdate

    HTH

    Warren

  32. February 3rd, 2010 at 10:04 pm #Sean Schofield

    Thanks for the tip re:gem install/compile with mac ports.

  33. February 20th, 2010 at 11:33 pm #Insulinwof

    these instructions worked great for me. many thanks.

    question: could you do something similar for upgrade to 8.4.2 via ports? i have no idea how to do this right.

    also: any idea why i’m getting this when i try to do selfupdate:

    dlopen(/Library/Tcl/macports1.0/MacPorts.dylib, 10): no suitable image found. Did find:
    /Library/Tcl/macports1.0/MacPorts.dylib: no matching architecture in universal wrapper
    while executing
    “load /Library/Tcl/macports1.0/MacPorts.dylib”
    (“package ifneeded macports 1.0″ script)
    invoked from within
    “package require macports”
    (file “/opt/local/bin/port” line 39)

  34. February 21st, 2010 at 10:05 am #Adam Christensen

    @Insulinwof – I don’t think I remember seeing the macports error before. Just as a shot in the dark, did you have macports installed on OSX 10.5 and then upgrade to 10.6?

  35. April 5th, 2010 at 3:40 pm #Darren Weber

    How about

    {
    sudo port install rb-postgres
    }

  36. April 5th, 2010 at 3:42 pm #Darren Weber

    One more thing, check the details in that port with

    {
    port cat rb-postgres
    }

  37. April 12th, 2010 at 11:51 am #Carlos

    Hi, I am Carlos from Colombia. I would like to know what is the advantage of using MacPorts to install PostgreSQL over downloading the .DMG package for Leopard from the PostgreSQL website.

  38. April 17th, 2010 at 6:43 am #mac leopard

    I am very pleasedto see that you are putting so much of effort for encouraging the readers with valueable posts like this, I have sent this post to my twitter accounts.

  39. May 4th, 2010 at 11:16 am #Jenlsing

    NiksBilt say: It is a pity, that now I can not express – it is very occupied. But I will be released – I will necessarily write that I think.

    _____________
    {vigra
    order online cheap
    5

  40. July 23rd, 2010 at 5:22 am #raaz

    how can i avoid the error su: do directory

  41. July 23rd, 2010 at 5:23 am #raaz

    sorry how can i avoid the error su: no directory

  42. July 23rd, 2010 at 12:40 pm #Sketchy

    Thank you for the instructions! Points of note though: pgAdmin3 errors out when installing dependencies; also it installs a different postgresql84 even if it is installed.

Leave a Reply