A while back I wrote about getting PostgreSQL support into PHP as it was packaged with Mac OS X 10.6. Since then, a lot has happened with Mac OS X, and PostgreSQL, and I find that I'm once again in need of developing PHP and PostgreSQL on my MacBook Pro. So I wanted to refresh this list and make it a little simpler at the same time.
Getting PostgreSQL 9.3
The first big difference in this post is that I'm switching to Homebrew and that's made the entire operation a lot smoother. There's no 32-bit/64-bit issue as Homebrew does both, and it builds in on your box, so again, a lovely customized solution with a simple command:
$ brew install postgresql
It even has a nice, simple informational blurb about how to start/stop and upgrade. Very nice. But now that it's on the box, and ready to roll, let's add in the PostgreSQL support to the PHP 5.4.24 that's installed with Mac OS X 10.9.
Activating PHP in Apache
The first thing to do is to activate PHP in the supplied Apache 2 with OS X 10.9. This is a single line in a single file - /etc/apache2/httpd.conf. There's a line and you need to uncomment it:
LoadModule php5_module libexec/apache2/libphp5.so
and then add a new file called /etc/apache2/other/php5.conf and have it contain:
<IfModule php5_module>
AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps
<IfModule dir_module>
DirectoryIndex index.html index.php
</IfModule>
</IfModule>
which does all the other PHP configuration in a separate file to make upgrades easy.
Building in PostgreSQL extra for PHP
At this point we need to get the source code for the exact version of PHP that Apple ships. This is pretty easy by getting the PHP 5.4.24 source from a mirror and unpacking it into a directory. We then just run the following commands:
$ cd php-5.4.24/ext/pgsql/
$ phpize
$ ./configure
$ make
at the end of this process, you'll have a file: php-5.4.24/ext/pgsql/.libs/pgsql.so and that needs to be placed in the right directory and referenced in the php.ini file.
For Mac OS X 10.9.2, this is accomplished with:
$ sudo cp .libs/pgsql.so /usr/lib/php/extensions/no-debug-non-zts-20100525/
and then edit the php.ini file to set the extension directory:
extension_dir=/usr/lib/php/extensions/no-debug-non-zts-20100525/
and then add the line:
extension=pgsql.so
Finishing Up
At this point, a simple restart of apache:
$ sudo apachectl restart
and everything should be in order. Hit a URL that's a static file with the contents:
<?php
phpinfo();
?>
and you should see all the details about the PHP install - including the PostgreSQL section with the version of Postgres indicated. It's all up and running now.