Skip to content

Instantly share code, notes, and snippets.

@doole
Last active September 21, 2023 19:12
  • Star 33 You must be signed in to star a gist
  • Fork 13 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save doole/8651341 to your computer and use it in GitHub Desktop.
Install PostgreSQL PHP extensions on Mac OS X
#!/bin/bash
# Install Xcode Command Line Tools first (required)
xcode-select --install
# Check PHP version `php --version`
PHP_VER=$(php -v | head -1 | awk '{ print $2 }')
# Extensions directory (default: empty string)
EXT_DIR=""
# Postgres.app < 9.3.5.0
#PG_APP="/Applications/Postgres.app/Contents/MacOS"
# Postgres.app >= 9.3.5.0 (check currently installed version first!)
PG_VER="9.5"
PG_APP="/Applications/Postgres.app/Contents/Versions/$PG_VER"
# El Capitan / Sierra workaround
if [ $(uname -r | head -c2) > 14 ]; then
EXT_DIR=/usr/local/lib/php/extensions/
mkdir -p $EXT_DIR
fi
# Check if extension exists first
php -m | grep pgsql
# Update brew and install requirements
brew update
brew install autoconf
# Download PHP source and extract
mkdir -p ~/src && cd ~/src
curl -O http://php.net/distributions/php-$PHP_VER.tar.bz2
tar -xjf php-$PHP_VER.tar.bz2
# Go to extension dir and phpize
cd ~/src/php-$PHP_VER/ext/pdo_pgsql/
phpize
# Configure for Postgress.app or just use `./configure` for the brew version
./configure --with-pdo-pgsql=$PG_APP
make
# El Capitan / Sierra workaround
if [ $(uname -r | head -c2) > 14 ]; then
cp ./modules/pdo_pgsql.so $EXT_DIR
else
sudo make install
fi
echo "extension=${EXT_DIR}pdo_pgsql.so" | sudo tee -a /private/etc/php.ini
# Go to extension dir and phpize
cd ~/src/php-$PHP_VER/ext/pgsql/
phpize
# Configure for Postgress.app or just use `./configure` for the brew version
./configure --with-pgsql=$PG_APP
make
# El Capitan / Sierra workaround
if [ $(uname -r | head -c2) > 14 ]; then
cp ./modules/pgsql.so $EXT_DIR
else
sudo make install
fi
echo "extension=${EXT_DIR}pgsql.so" | sudo tee -a /private/etc/php.ini
# Check if extension exists, again
php -m | grep pgsql
# Cleanup
rm -rf ~/src/php-$PHP_VER/
@mon-compte-github
Copy link

Thanks a lot 😃

@terilars
Copy link

terilars commented Jul 9, 2015

Sorry, but I can't make this shell script work in Yosemite, brew installed Postgre 9.4.4 running. Here's my bash after sh:

--2015-07-09 21:45:22--  http://dk2.php.net/get/php-5.5.24.tar.bz2/from/this/mirror
Resolving dk2.php.net... 185.21.233.25, 2a00:5a20::a00:27ff:fe52:f0a6
Connecting to dk2.php.net|185.21.233.25|:80... connected.
HTTP request sent, awaiting response... 302 Found
Location: http://dk2.php.net/distributions/php-5.5.24.tar.bz2 [following]
--2015-07-09 21:45:22--  http://dk2.php.net/distributions/php-5.5.24.tar.bz2
Reusing existing connection to dk2.php.net:80.
HTTP request sent, awaiting response... 200 OK
Length: 13337043 (13M) [application/octet-stream]
Saving to: 'mirror'

mirror                 100%[=============================>]  12.72M  1.36MB/s   in 12s    

2015-07-09 21:45:34 (1.09 MB/s) - 'mirror' saved [13337043/13337043]

tar: Unrecognized archive format
tar: Error exit delayed from previous errors.
install_psql_php.sh: line 16: cd: php-5.5.24/ext/pdo_pgsql/: No such file or directory
Cannot find config.m4. 
Make sure that you run '/usr/bin/phpize' in the top level source directory of the module

install_psql_php.sh: line 21: ./configure: No such file or directory
make: *** No targets specified and no makefile found.  Stop.
make: *** No rule to make target `install'.  Stop.
install_psql_php.sh: line 26: /private/etc/php.ini: Permission denied
install_psql_php.sh: line 29: cd: /Users/torlarse/src/php-5.5.24/ext/pgsql/: No such file or directory
Cannot find config.m4. 
Make sure that you run '/usr/bin/phpize' in the top level source directory of the module

install_psql_php.sh: line 34: ./configure: No such file or directory
make: *** No targets specified and no makefile found.  Stop.
make: *** No rule to make target `install'.  Stop.
install_psql_php.sh: line 39: /private/etc/php.ini: Permission denied

I also tried to install manually, but could never 'make' due to errors. Please help?

@terilars
Copy link

terilars commented Jul 9, 2015

should I perhaps do all this from the usr/ directory instead of my Users/<username>folder?

@doole
Copy link
Author

doole commented Jul 11, 2015

No, your problem is with php source file – it's saved as mirror not as it should be as php-5.5.24.tar.bz2. Therefore tar command breaks and everything else after that. I'll update the script for Yosemite as soon as possible.
For the quick fix: download manually php-5.5.24.tar.bz2 into~/src directory, comment lines 1-12, and run the script again.

@larryweya
Copy link

I had to use this to get mine to work with a recent version of Postgres.app

./configure --with-pdo-pgsql="/Applications/Postgres.app/Contents/Versions/9.4/"

@jibiel
Copy link

jibiel commented Sep 3, 2015

@pavelpigalev Nope, directory have been already changed at line 11:

mkdir -p ~/src; cd ~/src

@doole
Copy link
Author

doole commented Oct 2, 2015

I've updated the script, so it should work better for the latest versions...
Thanks @larryweya for the correct Postgres.app path.

@LiamKlyneker
Copy link

I fucking love you dude!! Save my life!!!

@dieze
Copy link

dieze commented Oct 21, 2015

It should be (don't forget the -a) :

echo "extension=${EXT_DIR}pdo_pgsql.so" | sudo tee -a /private/etc/php.ini
echo "extension=${EXT_DIR}pgsql.so" | sudo tee -a /private/etc/php.ini

instead of :

sudo echo "extension=$EXT_DIRpdo_pgsql.so" >> /private/etc/php.ini
sudo echo "extension=$EXT_DIRpgsql.so" >> /private/etc/php.ini

because sudo echo "something" >> privilegedFile throws a "Permission denied" error.
because echo "$EXT_DIRsomething" evaluate the variable $EXT_DIRsomething, not $EXT_DIR.

And it should be (note the "~/src") :

# Go to extension dir and phpize
cd ~/src/php-$PHP_VER/ext/pdo_pgsql/

# Go to extension dir and phpize
cd ~/src/php-$PHP_VER/ext/pgsql/

@annettedorothea
Copy link

Thanks and merci à Adrien :)
Very helpful!

@doole
Copy link
Author

doole commented Nov 16, 2015

I've updated the script. Thanks @adrien-desfourneaux 😄

@xtsolucoes
Copy link

Hi I have this problem

cc -I. -I/var/root/src/php-5.5.29/ext/pgsql -DPHP_ATOM_INC -I/var/root/src/php-5.5.29/ext/pgsql/include -I/var/root/src/php-5.5.29/ext/pgsql/main -I/var/root/src/php-5.5.29/ext/pgsql -I/usr/include/php -I/usr/include/php/main -I/usr/include/php/TSRM -I/usr/include/php/Zend -I/usr/include/php/ext -I/usr/include/php/ext/date/lib -I/Applications/Postgres.app/Contents/Versions/9.5/include -DHAVE_CONFIG_H -g -O2 -c /var/root/src/php-5.5.29/ext/pgsql/pgsql.c -fno-common -DPIC -o .libs/pgsql.o
/var/root/src/php-5.5.29/ext/pgsql/pgsql.c:35:10: fatal error: 'php.h' file not found

include "php.h"

     ^

1 error generated.
make: *** [pgsql.lo] Error 1
cp: ./modules/pgsql.so: No such file or directory
extension=/usr/local/lib/php/extensions/pgsql.so
PHP Warning: PHP Startup: Unable to load dynamic library '/usr/local/lib/php/extensions/pdo_pgsql.so' - dlopen(/usr/local/lib/php/extensions/pdo_pgsql.so, 9): image not found in Unknown on line 0
PHP Warning: PHP Startup: Unable to load dynamic library '/usr/local/lib/php/extensions/pgsql.so' - dlopen(/usr/local/lib/php/extensions/pgsql.so, 9): image not found in Unknown on line 0
PHP Warning: PHP Startup: Unable to load dynamic library '/usr/local/lib/php/extensions/pdo_pgsql.so' - dlopen(/usr/local/lib/php/extensions/pdo_pgsql.so, 9): image not found in Unknown on line 0
PHP Warning: PHP Startup: Unable to load dynamic library '/usr/local/lib/php/extensions/pgsql.so' - dlopen(/usr/local/lib/php/extensions/pgsql.so, 9): image not found in Unknown on line 0
PHP Warning: PHP Startup: Unable to load dynamic library '/usr/local/lib/php/extensions/pdo_pgsql.so' - dlopen(/usr/local/lib/php/extensions/pdo_pgsql.so, 9): image not found in Unknown on line 0
PHP Warning: PHP Startup: Unable to load dynamic library '/usr/local/lib/php/extensions/pgsql.so' - dlopen(/usr/local/lib/php/extensions/pgsql.so, 9): image not found in Unknown on line 0

@pyrog
Copy link

pyrog commented Feb 26, 2016

Hi, at that time postgresql is in version 9.5 😉
PG_APP="/Applications/Postgres.app/Contents/Versions/9.5/"

If you start this script many times like me, you should remove content of /private/etc/php.ini

@dole, Thank you very much for your script

@bortexz
Copy link

bortexz commented Mar 8, 2016

You saved my day, thanks for this amazing script!!

Just if anyone comes in the same situation, if you change the postgres version and you need to rebuild the extensions, you need to delete these two folders:

~/src/php-$PHP_VER/ext/pdo_pgsql/
~/src/php-$PHP_VER/ext/pgsql/

@ruckiand
Copy link

i experience this weird issue. can't find workaround. any ideas?

cc -I. -I/usr/local/src/php-5.5.30/ext/pgsql -DPHP_ATOM_INC -I/usr/local/src/php-5.5.30/ext/pgsql/include -I/usr/local/src/php-5.5.30/ext/pgsql/main -I/usr/local/src/php-5.5.30/ext/pgsql -I/usr/include/php -I/usr/include/php/main -I/usr/include/php/TSRM -I/usr/include/php/Zend -I/usr/include/php/ext -I/usr/include/php/ext/date/lib -I/Applications/Postgres.app/Contents/Versions/9.4/include -DHAVE_CONFIG_H -g -O2 -c /usr/local/src/php-5.5.30/ext/pgsql/pgsql.c -fno-common -DPIC -o .libs/pgsql.o
/usr/local/src/php-5.5.30/ext/pgsql/pgsql.c:35:10: fatal error: 'php.h' file not found

include "php.h"

@doole
Copy link
Author

doole commented Mar 26, 2016

@pyrog, you're welcome 😉

@bertofer, you're welcome 😄

@xtsolucoes @ruckiand, you're missing php.h – do you have xcode command-line tools installed? try with xcode-select --install

@bipin07
Copy link

bipin07 commented Apr 29, 2016

Thank you so much for the script. Its def saved me a ton of time. Also, I was running into same missing php.h file not found error and then I ran xocde-select --install and re-run the script, and it all resolved. Thanks again!

@povtasci
Copy link

povtasci commented May 7, 2016

Great work !!

Thanks

@azizultex
Copy link

Thanks man! Already spent a day fixing it.

@alvesoaj
Copy link

alvesoaj commented Jul 8, 2016

Amazing!!!

@DelaramR
Copy link

Saved my day! Thanks!

@doole
Copy link
Author

doole commented Oct 27, 2016

Updated for macOS Sierra 🍺

Copy link

ghost commented Nov 3, 2016

Dudeeeeeee you save my life :D

@theangelperalta
Copy link

You saved me! I owe you a beer haha

@brygom
Copy link

brygom commented Dec 14, 2016

Thank you!

@cameronwlewis
Copy link

Freaking amazing. This is the best thing since sliced bread. Thanks a billion

@kickstart530
Copy link

Very Helpful. Thanks you helpful to get the pgsql module installed.

@onlyjazz
Copy link

Note that on OS/X the location of the php.ini for the Web application and for the php command line is different -

On OS/X Yosemite:

/usr/local/php5/lib/php.ini - php.ini for the Web app
/usr/local/etc/php/7.1/php.ini - php.ini for the php command line (if you are using php 7.1 YMMV)
If you are using Postgres you will need to specify the location of the pdo_pgsql extension like:
extension="/usr/local/php/extensions/pdo_pgsql.so" (again YMMV depending on your system)

@mattjis
Copy link

mattjis commented May 20, 2020

xcode-select: error: command line tools are already installed, use "Software Update" to install updates
pdo_pgsql
pgsql
Already up-to-date.
Warning: autoconf 2.69 is already installed and up-to-date
To reinstall 2.69, run `brew reinstall autoconf`
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   177  100   177    0     0    264      0 --:--:-- --:--:-- --:--:--   264
tar: Unrecognized archive format
tar: Error exit delayed from previous errors.
/Users/mattijs/Downloads/8651341-0606545a4db1820cf127755505d0d35a186a6827/install_psql_php.sh: line 37: cd: /Users/mattijs/src/php-7.1.23/ext/pdo_pgsql/: No such file or directory
Cannot find config.m4. 
Make sure that you run '/usr/bin/phpize' in the top level source directory of the module

/Users/mattijs/Downloads/8651341-0606545a4db1820cf127755505d0d35a186a6827/install_psql_php.sh: line 41: ./configure: No such file or directory
make: *** No targets specified and no makefile found.  Stop.
cp: ./modules/pdo_pgsql.so: No such file or directory
Password:
extension=/usr/local/lib/php/extensions/pdo_pgsql.so
/Users/mattijs/Downloads/8651341-0606545a4db1820cf127755505d0d35a186a6827/install_psql_php.sh: line 53: cd: /Users/mattijs/src/php-7.1.23/ext/pgsql/: No such file or directory
Cannot find config.m4. 
Make sure that you run '/usr/bin/phpize' in the top level source directory of the module

/Users/mattijs/Downloads/8651341-0606545a4db1820cf127755505d0d35a186a6827/install_psql_php.sh: line 57: ./configure: No such file or directory
make: *** No targets specified and no makefile found.  Stop.
cp: ./modules/pgsql.so: No such file or directory
extension=/usr/local/lib/php/extensions/pgsql.so
PHP Warning:  PHP Startup: Unable to load dynamic library '/usr/local/lib/php/extensions/pdo_pgsql.so' - dlopen(/usr/local/lib/php/extensions/pdo_pgsql.so, 0x0009): dlopen(): file not found: /usr/local/lib/php/extensions/pdo_pgsql.so in Unknown on line 0
PHP Warning:  PHP Startup: Unable to load dynamic library '/usr/local/lib/php/extensions/pgsql.so' - dlopen(/usr/local/lib/php/extensions/pgsql.so, 0x0009): dlopen(): file not found: /usr/local/lib/php/extensions/pgsql.so in Unknown on line 0
pdo_pgsql
pgsql
logout
Saving session...
...copying shared history...
...saving history...truncating history files...
...completed.
Deleting expired sessions...9 completed.

[Process completed]

@doole
Copy link
Author

doole commented May 20, 2020

@mattjis since macOS High Sierra pgsql is included in the standard PHP instalation, so you don't need this workaround anymore.

If you take a look at your log, lines 2 and 3 show that you actually have pdo_pgsql and pgsql installed [from here]. Check yourself by typing php -m | grep pgsql or php --info | grep PostgreSQL in console.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment