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
- Mac OS Developer’s Tools
- MacPorts
- Ruby (comes with Leopard)
- Ruby Gems (also comes with Leopard)
Installation
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…
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.
March 23rd, 2008 at 7:20 am #K. Adam Christensen
Thanks for the heads up on that missing
>.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.
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.
April 4th, 2008 at 8:05 pm #Robert
Awsome!
Thanks for the taking the time to put this together.
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.
May 9th, 2008 at 8:38 am #Fabien Jakimowicz
I use your guide to made my own one in french : http://fabien.jakimowicz.com/articles/2008/05/09/rails-sur-postgresql-pour-leopard
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
May 15th, 2008 at 8:17 am #K. Adam Christensen
Thanks Peter!
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.
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.
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…