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)
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…
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.
Thanks for the heads up on that missing
>.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.
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.
Awsome!
Thanks for the taking the time to put this together.
nice! thanks for the simple walk through, I use MySQL but thought I’d give this a try today.
I use your guide to made my own one in french : http://fabien.jakimowicz.com/articles/2008/05/09/rails-sur-postgresql-pour-leopard
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
Thanks Peter!
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.
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.
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…
Thanks for the heads up Alex
Any idea of how to make sure postgresql and PHP work together? Thanks!
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)
Pingback: djwonk › Setup PostgreSQL on Mac OS X
Many thanks — exactly what I needed to get up and running.
The “su: /dev/null: Permission denied” can be avoided by setting the postgres UserShell to /usr/bin/false.
Thanks!
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)
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…
Pingback: PostgreSQL, Ruby, and Rails: WIN at Josh Kim dot Org
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
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.
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.)
Thanks for this. It helped a lot.
Thanks for this tutorial! it helped a lot!
Now to figure out pgAdmin…
Pingback: links for 2009-10-18 « Mike Does Tech
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
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
If you have older MacPorts you have to do a Macports update
sudo port selfupdate
HTH
Warren
Thanks for the tip re:gem install/compile with mac ports.
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)
@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?
How about
{
sudo port install rb-postgres
}
One more thing, check the details in that port with
{
port cat rb-postgres
}
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.
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.
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
how can i avoid the error su: do directory
sorry how can i avoid the error su: no directory
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.
Thats one of that article which it really is worth to bookmark. Genuinely nice article.
I was very pleased to find this site.I wanted to thank you for this great read!! I definitely enjoying every little bit of it and I have you bookmarked to check out new stuff you post.
thanks well done
Hey take a look here:
http://www.gregbenedict.com/2009/08/31/installing-postgresql-on-snow-leopard-10-6/
It is exactly the same code!
great post.. exactly what i needed.