stiefels.net Just another WordPress weblog

22Nov/075

Building PostgreSQL on Leopard

As you might know the [PostgreSQL Global Development Group](http://www.postgresql.org) is currently working on the final version of the PostgreSQL Database Management System (DBMS) 8.3 which has some nice features. Unfortunately, there is no prebuilt package available for Mac OS X of the current beta versions. But fortunately building PostgreSQL is very easy on Leopard:

## Requirements
This short tutorial requires you to have Mac OS X Leopard (it also may work with Tiger) installed on your system along with the [Xcode tools](http://developer.apple.com/tools/xcode/).

## Getting the sources
The sources of PostgreSQL are free available from one of the mirrors listed [here](http://wwwmaster.postgresql.org/download/mirrors-ftp). Download the current beta from the *source* directory and unpack it.

## Configuring
Before you can start the actual build process, you have to generate the Makefiles. This is done by running the included **configure** script with optional parameters. Available parameters can be listed with the following command:

$ ./configure --help

I decided to build my installation with the following parameters:

$ ./configure --prefix=/opt/local/postgresql83b3
--enable-thread-safety
--without-docdir
--with-perl
--with-gssapi
--with-pam
--with-bonjour
--with-openssl

There is no need to install 3rd party libraries when you use these parameters, but feel free to change them. For a full description of all the parameters have a look at this [website](http://www.postgresql.org/docs/8.3/static/install-procedure.html). The **configure** script now does some checks and creates the Makefiles needed for the build process.

## Building PostgreSQL
The actual build process is started by running the **make** command. The created Makefiles are used to tell the **make** program which files have to be compiled and how the whole application has to build together.

$ make

Depending on your machine the build process takes a few minutes. If **make** succeeds you should see this line:

All of PostgreSQL successfully made. Ready to install.

Great!

## Installing PostgreSQL
The install process is as easy as the build process. The only thing we do is telling **make** to start the installation:

$ make install

A few seconds later you should see this:

PostgreSQL installation complete.

## Creating the database cluster
Before we can use PostgreSQL we have to create at least one database cluster. That is the location where all the data will be stored (here in the directory *data*).

$ cd /opt/local/postgresql83b3
$ bin/initdb ./data

## Starting the server process
Starting the PostgreSQL server process is done via the **pg_ctl** program located in the *bin* directory.

$ bin/pg_ctl -D ./data start

To stop it, use

$ bin/pg_ctl -D ./data stop

## Using launchd to start automatically
If you plan to use PostgreSQL frequently you might be interested in starting PostgreSQL automatically when you log into your system. To do this, download [this file](http://www.stiefels.net/wp-content/uploads/2007/11/net.stiefels.postgresql.starter.plist) and copy it into *~/Library/LaunchAgents* (you may create the *LaunchAgents* directory if it doesn’t already exist). Next time you log in, the PostgreSQL server process is started automatically. The file expects to find your PostgreSQL installation in */opt/local/postgresql83b3*. Feel free to change it, if you installed it somewhere else.

## Connecting to the database
To test if your installation is actually working you can use **psql** to make a local connection to your server. Please note, that PostgreSQL doesn’t allow any network connections using the default configuration. Have a look at the [manual](http://www.postgresql.org/docs/8.3/static/index.html) for further information.

$ cd /opt/local/postgresql83b3$ bin/psql -d postgres
Welcome to psql 8.3beta3, the PostgreSQL interactive terminal.

## Congratulations
The purpose of this little tutorial is to show you how source packages can be compiled on Mac OS X (and other Unices). This way of building applications (configure/make/make install) is a common way in the Unix world and you'll come across these tools very often if you build applications from source packages.

Comments (5) Trackbacks (0)
  1. Thanks, this worked for me on the current Leopard version (10.5.1) with one small glitch. I had to su as admin in order for the install to work.

  2. Thanks, as far as I can tell this worked on OS X 10.5.2 and PostgreSQL 8.3.0.

  3. This absolutely does not work with Mac OS X Leopard 10.5.3.

    creating template1 database in ./data/base/1 … FATAL: could not create shared memory segment: Cannot allocate memory
    DETAIL: Failed system call was shmget(key=1, size=1777664, 03600).
    HINT: This error usually means that PostgreSQL’s request for a shared memory segment exceeded available memory or swap space. To reduce the request size (currently 1777664 bytes), reduce PostgreSQL’s shared_buffers parameter (currently 50) and/or its max_connections parameter (currently 13).
    The PostgreSQL documentation contains more information about shared memory configuration.
    child process exited with exit code 1
    initdb: removing contents of data directory “./data”

  4. @joost:
    Try increasing the shared memory settings on your system.
    Create /etc/sysctl.conf with the follwing content and reboot your machine:

    kern.sysv.shmmax=167772160
    kern.sysv.shmmin=1
    kern.sysv.shmmni=32
    kern.sysv.shmseg=8
    kern.sysv.shmall=65536

  5. FWIW, I found that this error will occur if you already have an instance of posgresql running.


Leave a comment


No trackbacks yet.