Getting PostgreSQL Support into PHP 5.3.0 on Mac OS X 10.6

PostgreSQL.jpg

One of my favorite web scripting languages is PHP. To me, it seems that it's right in the sweet spot of powerful yet fundamentally a scripting language. That means that it's much more lightweight for a developer than, say Java/JSP, and at the same time, more C-like than, say Perl. While there is a place for almost all of the web technologies in today's development environment, I have to say, PHP holds a really special place in my heart.

Because of that, I was thrilled over the years to see Marc Liyanage continue to produce a great PHP distribution with all the bells and whistles - including my favorite database - PostgreSQL. For the last several OS releases, Apple has delivered a PHP component to Apache, they leave out the PostgreSQL support that I need to do the few scripting-based web projects that I have.

Not Playing Well with Snow Leopard

But with the introduction of Snow Leopard - Mac OS X 10.6, Marc hasn't been able to build a PHP distribution that is compatible with the OS. So when I ran across this article about building the PostgreSQL support for PHP into the Snow Leopard Apache install, I had to jump at it for several reasons:

  • It's low-impact. I don't have to get rid of all the suport that Apple has in it's PHP install.
  • It's independent of Marc, so I don't have to wait on him to release compelte updates to PHP.

So it is with excitement that I venture off on my own to build the PostgreSQL support into Snow Leopard's Apache server and forever empower myself to keep PostgreSQL support in PHP as it ships from Apple.

Let's get to it!

According to the write-up, we need to set the bash variables:

  MACOSX_DEPLOYMENT_TARGET=10.6
  CFLAGS="-arch i386 -arch x86_64 -g -Os -pipe -no-cpp-precomp"
  CCFLAGS="-arch i386 -arch x86_64 -g -Os -pipe"
  CXXFLAGS="-arch i386 -arch x86_64 -g -Os -pipe"
  LDFLAGS="-arch i386 -arch x86_64 -bind_at_load"
  export CFLAGS CXXFLAGS LDFLAGS CCFLAGS MACOSX_DEPLOYMENT_TARGET

but we can also use the tcsh variable scheme:

  setenv MACOSX_DEPLOYMENT_TARGET 10.6
  setenv CFLAGS "-arch i386 -arch x86_64 -g -Os -pipe -no-cpp-precomp"
  setenv CCFLAGS "-arch i386 -arch x86_64 -g -Os -pipe"
  setenv CXXFLAGS "-arch i386 -arch x86_64 -g -Os -pipe"
  setenv LDFLAGS "-arch i386 -arch x86_64 -bind_at_load"

Then I get into the PHP 5.3.0 source code at: php-5.3.0/ext/pgsql/ and run the commands to build the module:

  phpize
  ./configure
  make

The end result being that in php-5.3.0/ext/pgsql/.libs/ there is now pgsql.so that's ready to be used with PHP. All we need to do is to copy into the right place: (assuming we're still in the php-5.3.0/ext/pgsql directory)

  sudo cp .libs/pgsql.so /usr/lib/php/extensions/no-debug-non-zts-20090626

and then edit the /etc/php.ini file to tell it about the addition. This is a two-step process, as we need to update the commented-out line:

  ;extension_dir=./

to:

  extension_dir=/usr/lib/php/extensions/no-debug-non-zts-20090626

and then add in the line:

  extension=pgsql.so

after all the Windows examples of loadable modules.

A quick restart with:

  sudo apachectl restart

and then the system is all ready to go!

Results

It's amazing to see the show up with the familiar 'pgsql' section:

phpinfo()

And with this, I can dump Marc's PHP distribution and not have to worry about his updates any longer. The feeling of freedom and independence this gives me is really hard to express. It's fantastic!

UPDATE: this seems to be working, but when I look at /var/log/apache2/error_log I see a bunch of:

dyld: lazy symbol binding failed: Symbol not found: _PQconnectdb
  Referenced from: /usr/lib/php/extensions/no-debug-non-zts-20090626/pgsql.so
  Expected in: flat namespace

dyld: Symbol not found: _PQconnectdb
  Referenced from: /usr/lib/php/extensions/no-debug-non-zts-20090626/pgsql.so
  Expected in: flat namespace

when I try to run a simple PHP script to hit a PostgreSQL database and pull back a little data. I have a bad feeling that it's a 32-bit/64-bit issue as the libraries are most likely 32-bit and I'm guessing Apache2 is 64-bit from Apple.

Indeed... the file type of the libpq.5.dylib library in use is ppc and i386, but the pgsql.so is i386 and x86_64. But I'm betting that httpd is x86_64. This means I need to update to a 64-bit version of PostgreSQL for the Mac. Marc L doesn't have one. I wonder if that means I'm on my own, or if I can get something built for me?

[9/18] UPDATE: when I updated from PostgreSQL 8.3.0 by Marc L. to the 8.3.7-3 from William Kyngesburye, and repeated the build, the 64-bit (actually 32-bit and 64-bit) libraries were all I needed. The test page was perfect without any errors:

PHP PostgreSQL Test