Jump to content


Installing PostgreSQL server on Mac OS X Snow Leopard


Recommended Posts

I did this last night and since there is no package like there is for MySQL you either have to install something like Macports or compile it by hand.

I went the latter root and figured it might be useful for other people so here are the instructions

Make a directory to store the source code

You may also want to remove the entry from $PATH in ~/.profile or ~/.bash_profile but it won't matter if you don't

$ sudo mkdir /usr/local/src

$ cd /usr/local/src

Since we are now in /usr/local it is easier just to run as root

$ sudo su

Now download and extract the code

$ curl http://wwwmaster.postgresql.org/download/mirrors-ftp/source/v8.4.1/postgresql-8.4.1.tar.gz -O

$ tar -zvxf postgresql-8.4.1.tar.gz

$ rm postgresql-8.4.1.tar.gz

$ cd postgresql-8.4.1

Now you need to configure it

$ ./configure --prefix=/usr/local/postgresql-8.4.1

Finally compile and install it

$ make

$ make install

if you want to compile it as a 64 bit application the following should work

$ ARCH=x86_64 CFLAGS="-arch x86_64" LDFLAGS="-arch x86_64" make

$ make install

Now some house keeping so that man and the commands work

$ ln -s /usr/local/postgresql-8.4.1 /usr/local/pgsql

$ echo "PATH=/usr/local/pgsql/bin:$PATH" >> ~/.profile

$ source ~/.profile

$ mkdir /usr/local/pgsql/data/

if you have a file called ~/.bash_profile you'll want to use that instead of ~/.profile as OS X ignore ~/.profile if ~/.bash_profile exists

This is were things get messy

Run the following command, it will give you a list of all the user IDs that are in use

$ dscl . list /Users UniqueID

Choose a free one below 500 (so that it isn't shown on the login screen), on mine mysql was using 74 so I choose 75 for postgresql

Now we need create the user and group (remember to replace 75 in both these blocks with whatever ID you choose). Since the convention on OS X is for system users to start with _ we'll do the same

$ dscl localhost create /Local/Default/Users/_postgres

$ dscl localhost create /Local/Default/Users/_postgres PrimaryGroupID 0

$ dscl localhost create /Local/Default/Users/_postgres UniqueID 75

$ dscl localhost create /Local/Default/Users/_postgres UserShell /bin/bash

$ dscl localhost passwd /Local/Default/Users/_postgres

$ dscl localhost create /Local/Default/Users/_postgres NFSHomeDirectory /var/home/_postgres

$ mkdir -p /var/home/_postgres

$ chown -Rf _postgres:_postgres /var/home/_postgres

$ dscl localhost create /Local/Default/Groups/_postgres

$ dscl localhost create /Local/Default/Groups/_postgres UniqueID 75

$ dscl localhost append /Local/Default/Groups/_postgres GroupMembership _postgres

When prompted to create the user password do enter something as we need to be able to login as the user from the shell for a brief period.

To stop the user appearing on the login screen run

$ defaults write /Library/Preferences/com.apple.loginwindow.plist Hide500Users -bool TRUE

Finally change the folder so that it is owned by the new user

$ chown -R _postgres /usr/local/postgresql-8.4.1/

$ exit

Next we need to login as the postgres user and setup the database

$ su - _postgres

$ /usr/local/pgsql/bin/initdb -E UTF8 -D /usr/local/pgsql/data/

$ /usr/local/pgsql/bin/pg_ctl -D /usr/local/pgsql/data/ -l /usr/local/pgsql/data/postgresql.log start

If the server started correctly you should now be able to create a database and access it

$ /usr/local/pgsql/bin/createdb test

$ /usr/local/pgsql/bin/psql test

type \q to quit the prompt

If you want to shutdown the server now

$ /usr/local/pgsql/bin/pg_ctl -D /usr/local/pgsql/data/ stop

Finally we can remove things that were only needed so we could login directly as the postgres user

$ exit

$ sudo su

$ rm -rf /var/home

$ dscl localhost delete /Local/Default/Users/_postgres NFSHomeDirectory

$ dscl localhost passwd /Local/Default/Users/_postgres

$ exit

There you have it, an installed PostgreSQL server.

If you want it to launch at startup you'll need to look at creating a Startup Item (/Library/StartupItems) or a Launch Daemon (/Library/LaunchDaemons) but since I don't need it too I didn't spend too long trying to get it working after my initial attempt failed

I use the following script to start/stop/restart it




	echo -n "Starting PostgreSQL server"

	sudo su _postgres -c '/usr/local/pgsql/bin/pg_ctl -D /usr/local/pgsql/data/ -l /usr/local/pgsql/data/postgresql.log start'





	echo -n "Stopping PostgreSQL server"

	sudo su _postgres -c '/usr/local/pgsql/bin/pg_ctl -D /usr/local/pgsql/data/ stop'



case "$1" in












        echo "Usage: {start|stop|restart}"

        exit 1



exit $?

Just make it executable (chmod +x) and then run it as /path/to/script start|stop|restart

Hope someone finds that useful

To uninstall, stop the server and do the following

$ sudo su

$ rm  /usr/local/pgsql

$ rm -rf /usr/local/postgresql-8.4.1

$ dscl localhost delete /Local/Default/Users/_postgres

$ dscl localhost delete /Local/Default/Groups/_postgres

$ defaults delete /Library/Preferences/com.apple.loginwindow.plist Hide500Users

$ exit
Link to post
Share on other sites
  • 1 month later...

[quote name='Stephen' date='12 September 2009 - 04:55 PM' timestamp='1252774537' post='1855461']
I did this last night and since there is no package like there is for MySQL you either have to install something like Macports or compile it by hand.


You can find a PostgreSQL installer for OS X, including Snow Leopard, at http://www.postgresql.org/download/macosx which will install the server and the pgAdmin GUI.

Link to post
Share on other sites


This topic is now archived and is closed to further replies.

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Create New...

Important Information

We use technologies, such as cookies, to customise content and advertising, to provide social media features and to analyse traffic to the site. We also share information about your use of our site with our trusted social media, advertising and analytics partners. See more about cookies and our Privacy Policy