Want to get Lullabot article, videocast, and podcast announcements delivered right to your in-box?
Let us know your email address (we won't share it) and we'll let you know when anything exciting happens.
Beginning with Drupal 6 and PostgreSQL on OS X 10.5 Leopard
PostgreSQL (often called Postgres) is the Other Major Database for Drupal. It has one of the same strange characteristics that Drupal does: its adherents swear that it is the best thing since sliced bread. In this article, we'll examine how to get PostgreSQL installed, then get a Drupal 6 installation running on top of it.
Assumptions: XCode Tools from your Mac OS X DVD (also available from Apple Developer Connection) are installed. Your copy of PHP has support for Postgres (I used MAMP and it worked out of the box).
Getting PostgreSQL
There are several places to get Postgres. You could compile it yourself, or use Dru Satori's PostgreSQL for Mac, or Mark Liyanage's PostreSQL Installer. I used MacPorts to install it because (1) I wanted to use the latest version of Postgres (8.3.3) and Dru's installer had 8.3.1 and Mark's installer had 8.2.3 and (2) I already had MacPorts installed, and this gave me an excuse to try out Porticus.
After ensuring that MacPorts and Porticus were installed, I selected and installed the postgresql83 package (that is, the PostgreSQL client) and the postgresql83-server package (the PostgreSQL server).
Creating a Place for Data
The next step is to create the directory to hold Postgres data. The MacPorts port recommends:
$ sudo mkdir -p /opt/local/var/db/postgresql83/defaultdbYou might be thinking, "but why do I want my database data hidden away under /opt? I want it right here in my home directory where I can look at it!" Ah, but because of the following command, you won't be able to look at the files directly anyway. (You can dump out the database contents as a backup and store that in your home directory if you want.)
$ sudo chown postgres:postgres /opt/local/var/db/postgresql83/defaultdbThis transfers ownership of the directory we just created to the postgres user (which was created during the pre-destroot phase of the install -- thanks, MacPorts!).

Creating the Database Cluster
Postgres keeps groups of databases together in a cluster. The following command means, "By the powers bestowed in me (the OS X root user) become the postgres user. As that user, create a new PostgreSQL database cluster in the directory /opt/local/var/db/postgresql83/defaultdb":
$ sudo su postgres -c '/opt/local/lib/postgresql83/bin/initdb -D /opt/local/var/db/postgresql83/defaultdb'To which you should get the response:
The files belonging to this database system will be owned by user "postgres".
This user must also own the server process.
The database cluster will be initialized with locale en_US.UTF-8.
The default database encoding has accordingly been set to UTF8.
The default text search configuration will be set to "english".Yay! PostgreSQL is now installed.
Configuration Files
Postgres has squirreled away its configuration files deep inside /opt/local/var/db/postgresql8/defaultdb, which is a pain to remember. These configuration files have the following permissions:
$ sudo ls -l /opt/local/var/db/postgresql83/defaultdb | grep conf
-rw------- 1 postgres postgres 3429 Jul 1 17:33 pg_hba.conf
-rw------- 1 postgres postgres 1460 Jul 1 17:33 pg_ident.conf
-rw------- 1 postgres postgres 16592 Jul 1 17:33 postgresql.confSo, if you're a Unix junkie, that means you can su to the postgres user and edit the configuration files in vi or emacs. Me, I'd rather use a nice lickable editor like BBedit (please pause while I stroke my long, grey beard) or Textmate. So one approach is to place a copy of the postgresql.conf file in my home directory, where it will be backed up regularly. Then, the original postgresql.conf file will be replaced with a symlink to the real file in my home directory.
First, make a directory to store the configuration file in.
$ mkdir ~/postgresNow, copy the file from the Postgres data directory, preserving permissions (so postgres will still be able to read it):
$ sudo cp -p /opt/local/var/db/postgresql83/defaultdb/postgresql.conf ~/postgres/Then add your group and give read and write access to it. My username is jvandyk, so:
$ sudo chgrp jvandyk ~/postgres/postgresql.conf
$ sudo chmod g+rw ~/postgres/postgresql.conf
$ ls -l ~/postgres
-rw-rw---- 1 postgres jvandyk 16618 Jul 8 14:55 postgresql.confNow the file can be easily edited to change various settings by double-clicking it in the Finder and using your favorite GUI editor.
Changing the Logging Detail
Hopefully one of the reasons you want to install Postgres is to help with Drupal development. It would be nice to be able to see when Drupal is asking Postgres to do something odd, and to see the angry mutterings from Postgres that result. Let's turn up to log level on Postgres to find out what's going on deep inside. Find the section of postgresql.conf that looks like this:
#log_min_messages = notice # values in order of decreasing detail:
# debug5
# debug4
# debug3
# debug2
# debug1
# info
# notice
# warning
# error
# log
# fatal
# panicand set the log_min_messages option to info:
log_min_messages = info
Or, if you want to see every time the autovacuum process runs, you can use debug1 instead of info.
Starting and Stopping Postgres
The easiest approach is to just set Postgres to launch at startup, so it's "always on". The MacPorts install includes a launchd item for this, which can be installed with the following command:
$ sudo launchctl load -w /Library/LaunchDaemons/org.macports.postgresql83-server.plistThis will also start the server immediately.
If you change your mind and want to have Postgres NOT launch at startup:
sudo launchctl unload -w /Library/LaunchDaemons/org.macports.postgresql83-server.plistIn that case, you will need to start PostgreSQL whenever you want to use it. You can do that with the pg_ctl command. But there's a catch. Remember when we created the initial database cluster, and we had to use sudo su postgres to execute the command as the postgres user? We have to do that here, too, which makes the commands for starting and stopping rather verbose. So we'll use the magic of shell functions to make ourselves two easy commands, one to start and one to stop Postgres. Here's how to do it in the Terminal, assuming you're using the bash shell, which is the default shell in Leopard:
$ startpg() { 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'; }$ stoppg() { sudo su postgres -c '/opt/local/lib/postgresql83/bin/pg_ctl -D /opt/local/var/db/postgresql83/defaultdb stop -m fast'; }And here's one more convenience function. Typing reloadpg will reload the configuration files.
$ reloadpg() { sudo su postgres -c '/opt/local/lib/postgresql83/bin/pg_ctl -D /opt/local/var/db/postgresql83/defaultdb reload'; }Now when you type startpg in the Terminal, you will be prompted for your computer's administrative password (just like when you install software) and PostgreSQL will start. To stop PostgreSQL, just type stoppg. Easy!
To have these handy shortcuts available whenever you log in, add the lines of code that we used to create the shell functions to ~/.bash_profile.
Seeing What's Going On
You will want to be able to see what is going on with PostgreSQL. The log is at /opt/local/var/log/postgresql83/postgres.log but you might want to monitor it in real time for troubleshooting. I find it handy to create a shell function called Tail Postgres, abbreviated as tpg.
$ tpg() { tail -f /opt/local/var/log/postgresql83/postgres.log; }Now all you have to do is type tpg at the command line and output from the log will be shown in real time:
$ tpg
LOG: database system is ready to accept connections
LOG: autovacuum launcher startedAgain, add the line of code that we used to create the alias to ~/.bash_profile in order to have the alias "tpg" available permanently.
Creating a user for Drupal
Let's create a database user for Drupal to use. In the following command, we say "As the postgres user (that is, the postgres superuser account) create a new role named drupalesque. Prompt now for the password to use. Encrypt the password inside Postgres instead of storing it in clear text. This user is not a superuser and may not create databases or roles."
$ /opt/local/lib/postgresql83/bin/createuser -U postgres --pwprompt --encrypted --no-superuser --no-createdb --no-createrole drupalesque
Enter password for new role: boingechev
Enter it again: boingechevCreating a Database for Drupal from the Command Line
Now it's time to create the database that our Drupal install will use. We'll create a database named drupaldb that is owned by the database user drupalesque with the following:
/opt/local/lib/postgresql83/bin/createdb -U postgres --owner drupalesque drupaldbPostgres will use the default encoding that was set when we ran initdb, which is UTF-8.
Installing Drupal
Install Drupal just like normal. On the database setup screen, choose pgsql as the database type, enter drupaldb as the database name, drupalesque as the database username, and boingechev as the database password. Using MAMP, this worked for me without a hitch.

Communicating with Postgres
Now that PostgreSQL is installed and started, how do you talk to it? Easy. Use the psql command. This command starts pgsql as the user we created and connects it to the database we created:
$ /opt/local/lib/postgresql83/bin/psql -U drupalesque -d drupaldb
Welcome to psql 8.3.3, the PostgreSQL interactive terminal.
Type: \copyright for distribution terms
\h for help with SQL commands
\? for help with psql commands
\g or terminate with semicolon to execute query
\q to quit
drupaldb=>SELECT uid FROM users;
uid
-----
0
1
(2 rows)
drupaldb=> and this one starts it as the postgres superuser:
/opt/local/lib/postgresql83/bin/psql -U postgresNote: if you don't want to type the entire path to the Postgres commands (e.g., you want to type psql instead of /opt/local/lib/postgresql83/bin/psql) you can add the path to the Postgres binaries to the paths that Leopard looks in for commands. You can do that with this command:
sudo bash -c 'echo "opt/local/lib/postreql83/bin" > /etc/paths.d/postgresql'You'll need to restart for that to take effect.
Summary
We have only scratched the surface here. There are so many more questions. Is there something similar to phpMyAdmin for Postgres? Is there a quick tweak or two to improve performance, similar to turning on MySQL's query cache? And why didn't Postgres prompt us for a password when we started psql as the superuser? Regrettably, those intriguing questions will have to wait. No, this article is only meant to get you up and running with Drupal on Postgres on OS X 10.5 Leopard. I hope it does that.
References
- ShiftEleven: Installing PostgreSQL on Leopard using MacPorts
- PostgreSQL 8.3 Manual
- #postgresql irc channel on FreeNode. Special thanks to RhodiumToad and dru_.



RSS Feed



Comments
web-based postgres admin tool
phpPgAdmin is a web-based administration tool for PostgreSQL. find it at http://phppgadmin.sourceforge.net/
functionality is nearly equivalent to phpmyadmin.
Why PostgreSQL?
Thanks for the quick start up guide to using PostreSQL on a Mac.
I've only used MySQL before and I'm wondering what the major differences are to using PostreSQL with Drupal instead. Are there advantages for a Drupal site that I should be considering?
Thanks for another great Lullabot resource!
postgres already installed in Leopard?
Isn't postgres already there in Leopard?
I moved Drupal to an Ubuntu box because of the pain of trying to get GD installed. If you have a way to roll your own php with gd I'm all ears.
postfix
I think you are thinking of postfix. Postfix is mail transfer agent that is included in Leopard.
For PHP with GD, my current solution is MAMP.
no, I'm even more of an id10t than that ...
Turns out I was thinking of sqlite. Oh boy. A little knowledge being a dangerous thing and all... Emphasis on 'little'.
Sorry!
PHP with GD on Leopard
You should be able to build PHP with GD support using the MacPorts package manager mentioned at the beginning of this post. Be sure to read about the variants feature in the MacPorts documentation.
Help The PDO effort
I guess this means I can install Postgres and see if I can help with the move to PDO. Postgres seems to be causing some issues. Now, if only Lullabot did a post to tell me how to find the time to do that.
Its very intresting, and
Its very intresting, and i've learned a lot.
phpmyadmin for postgres
Well, I see someone already beat me to phppgadmin :) However, as for postgres performance tuning, there was a recent post on the dev list from Michael Haggerty that goes a wee bit beyond query caching:
http://lists.drupal.org/pipermail/development/2008-July/030405.html
Postgres and Mac XAMPP
If you happen to be using XAMPP on the Mac (as I currently am,) you should also be aware that pgsql support is included at least as of version 0.71, but not enabled by default.
All that you have to do to enable it is to add the following line to your php.ini file:
extension=pgsql.so(Mac XAMPP installs that file at /Applications/xampp/etc/php.ini .)
Thanks for the writeup!
Nevermind
Well, it turns out that the version of pgsql.so included with XAMPP does not include support for pg_escape_string(), which Drupal depends on quite heavily. Even transplanting pgsql.so from the MAMP distribution to XAMPP did not work.
I'm afraid that unless you want to recompile XAMPP's PHP, you should look at MAMP.
Easy postgres install
If you can settle for less than the most recent version installing PostgreSQL 8.3.1 is really easy for those who are not familiar with the command line.
I downloaded a Unified Installer package from Postgresql for mac and installed the server package (which includes client tools). The client tools includes applications to create a user and create a database. After creating one of each I fired up my MAMP, created a new Drupal 6 site and ... that's it!
PostgreSQL/Python
Thank you for the info about PostgreSQL installation. It works and will be used for development in Python on my Mac ;-)
Hello, I'm looking for a
Hello, I'm looking for a video that Addison Berry did which walks you through Drupal 6 installation on Leopard...is it still available?
Here is the direct link to
Here is the direct link to the Installing Drupal 6 video. You can find all of our videos under podcast > videocasts.
Building on this to build PHP + pgSQL from source
John, I used your instructions as a starting point for my guide Getting PHP + GD + PostgreSQL working on OSX 10.5 (aka recompiling everything).
Post new comment