Enabling PostgreSQL support in PHP on Mac OS X
The PHP version that comes bundled with OS X doesn't include PostgreSQL. You'll have to compile the extension yourself. Here are some instructions:
- Find your version of PHP:
php -v
. - Download the version of PHP that matches yours:
curl -O http://us.php.net/distributions/php-5.3.3.tar.gz
. (This example downloads PHP 5.3.3 but this must match your version) - Extract the archive you downloaded:
tar -xzvf php-5.3.3.tar.gz
- Change to the PostgreSQL's extension directory:
cd php-5.3.3/ext/pgsql/
- Type
phpize
. - Type
./configure
. - Type
make
. - Type
sudo make install
. - Add the extension to you
php.ini
file by addingextension=pgsql.so
. (You may already have done this) - Restart Apache.
Update for OS X Mountain LionApple has removed autoconf
from the newer versions of XCode so the procedure above will fail at #5. To solve that problem:
- Type
/usr/bin/ruby -e "$(/usr/bin/curl -fksSL https://raw.github.com/mxcl/homebrew/master/Library/Contributions/install_homebrew.rb)"
. - Type
sudo chown -R $USER /usr/local/Cellar
. - Type
brew update
. - Type
brew install autoconf
.
That should install autoconf
and allow you to install the module using the instructions above.
If you use home brew
, you can solve this with a command as simple as:
brew install php55-pdo-pgsql
for other php version, search with:
brew search pgsql
This worked for me with OSX 10.9.4 «Mavericks»
Install sources
Download the PHP source code. Unlike on Mountain Lion, you don’t get any headers preinstalled to link against so need to put it in /usr/include/php. Mavericks ships with PHP 5.4.17, but the latest 5.4.x source from php.net should do:
tar -jxvf php-5.4.20.tar.bz2sudo mkdir -p /usr/includesudo mv php-5.4.20 /usr/include/php
Configure PHP
cd /usr/include/php./configure --without-iconvsudo cp /etc/php.ini.default /etc/php.ini
Building a module
I needed the pdo_pgsql module - the same pattern should apply to just about any module assuming you have the necessary dependencies installed:
cd ext/pdo_pgsql
In my case I had the following error:
Cannot find autoconf. Please check your autoconf installation and the$PHP_AUTOCONF environment variable. Then, rerun this script. ERROR:`phpize' failed
So I had to use this command:
brew install autoconf
Then:
phpize
After that I tried to do:./configure
but I had the next problem:
checking for pg_config... not found configure: error: Cannot findlibpq-fe.h. Please specify correct PostgreSQL installation path
So the solution was to specify correct PostgreSQL installation path:
./configure --with-pdo-pgsql=/Library/PostgreSQL/9.3/makesudo make install
That copies pdo_pgsql.so to /usr/lib/php/extensions/no-debug-non-zts-20100525.
Then simply add
extension=pdo_pgsql.so to /etc/php.ini
Run php -m
to confirm everything went to plan.