Installing PostgreSQL on OSX for Rails development

14,973

Solution 1

I think I've managed to find a way that works. I'm borrowing heavily from this great post1, but since they are installing a bunch of other stuff at the same time I'm going to write out what I did here for people who are just looking for the PostgreSQL install answer.

1 Editor's note: Link seemed dead when I tried. Is this the one? http://blog.blackwhale.at/?p=175#PostgreSQL Please fix if it is.

  1. Download PostgreSQL for Mac and download the ‘Postgres.app’ installer.

  2. Create a user for your rails development (keep in mind that if you're sharing an application during development you'll probably want the same user between all members your dev team in order to avoid headaches)

    sudo -u postgres /Library/PostgreSQL8/bin/createuser
    
  3. Enter your Mac OS X system user name as role name, and make it a superuser.

  4. Install the pg gem so Rails can talk to PostgreSQL

    sudo env PATH=/Library/PostgreSQL8/bin:$PATH gem install pg
    
  5. Configure your rails app to talk to PostgreSQL. You can either create a new application with:

    rails *appname* -d postgresql (for Rails 3 -> rails new *appname* -d postgresql)
    

    Or for an existing app, modify your database.yml file.

This worked for me without any hiccups. If anyone else tries using this method I'd be interested to hear some feedback on how it went for you.

Solution 2

On a 64-bit Mac (Snow Leopard with Core 2 Duo or newer) I had to compile PostgreSQL from source, as rails kept complaining that:

*** Your PostgreSQL installation doesn't seem to have an architecture in common
with the running ruby interpreter (["ppc", "i386", "x86_64"] vs. [])

The architecture mismatch was probably bc I'd compiled rails from source, which defaulted to 64-bit. The binary installer on postgresql.org seemed only a 32-bit version. Setting ARCHFLAGS didn't fix this for me.

Anyhow, if you download the [source][1] from postgresql.org and follow the instructions in the INSTALL file, it's fairly straightforward. You don't have to create a new user if you use your own account. I did have to create the sysctl.conf file to expand shared memory - just google 'postgresql os x sysctl.conf'

Solution 3

Old question, but still maybe i can help someone with this (rather simple) solution:

gem install pg -- --with-opt-include=/opt/local/include/postgresql84/ --with-opt-lib=/opt/local/lib/postgresql84/

Solution 4

  1. Download and Install MacPorts
  2. fire up terminal
  3. sudo port install ruby postgresql83-server rb-postgres rb-gems rb-rails
Share:
14,973
Ganesh Shankar
Author by

Ganesh Shankar

Engineer, Entrepreneur, Product Manager at Google. I love distance running, traveling and training my attack cat, Comrade.

Updated on June 06, 2022

Comments

  • Ganesh Shankar
    Ganesh Shankar about 2 years

    I've spent several hours over the past few days trying to get PostgreSQL to play nice with RoR on my Mac.

    I've followed several tutorials using several different methods such as installing PostgreSQL manually and installing from various 1-click installers

    However the all the different methods I tried failed on the last step of installing the pg gem. Very frustrating!

    Does anyone here have a tried and tested tutorial for getting this done? (Or would you like to write some instructions here...?)

    My environment is this: Macbook running OSX 10.6, PostgreSQL 8.4.1 server

  • Ganesh Shankar
    Ganesh Shankar over 14 years
    I already have RoR installed, not through Macports. Wont this stuff things up?
  • prodigitalson
    prodigitalson over 14 years
    nope this will use its own installation of everything completely by passing the system installation. Note though if you want to use through apache youll need to also install the port of Apach2 and then fastcgi or what have you. Then youll need to make sure you modify your path in your ~/.bash_profile to have the macports paths before the rest of your path or else use the complete path to the installations like /opt/local/bin/ruby and such.
  • Ganesh Shankar
    Ganesh Shankar over 14 years
    Thanks for the answer. +1 as it's a good solution but I'm not sure it's exactly what I'm looking for. I want to see if I can add postgres to my existing dev environment and I feel that while macports is great, it doesn't always give you great control over versions...
  • prodigitalson
    prodigitalson over 14 years
    @gshankar: To some extent this is true. Im primarly a php Dev and there was definitely a period where i had to manage my own port repo with php 5.2.x when they switch to 5.3. That hosting a local repo was alot less fuss than compiling/upgrading and hooking everything up manually. Definitely valid criticism though and im glad you found a solution that meets your need. :-)
  • Ganesh Shankar
    Ganesh Shankar over 14 years
    Will do! (apparently I have to wait till tomorrow) Thanks for the help :)
  • AnApprentice
    AnApprentice over 13 years
    After installing i ran... sudo: /Library/PostgreSQL8/bin/createuser: command not found
  • Ganesh Shankar
    Ganesh Shankar over 13 years
    Hmm, have you looked in /Library/PostgreSQL8/ to see if you're actually installed in there? I just tried the command then and it works for me. Maybe you installed PostgreSQL9 ?
  • djones
    djones over 13 years
    The line should have been sudo -u postgres /Library/PostgreSQL/bin/createuser
  • Alex
    Alex about 13 years
    If using 9.0 , the default path for me was /Library/PostgreSQl/9.0/bin
  • Ken Mazaika
    Ken Mazaika over 11 years
    The line about "Install the pg gem so Rails can talk to PostgreSQL" is exactly what I needed. Thanks.