PostgreSQL is now easy to install on Windows using its installer. However, integrating it with PHP in Windows usually has problems. No matter how you configure PHP and the DLL for postgres extension, it will not work out of the box, not even after a restart. It is just tricky and probably the postgres team can fix it in later version of the software.
Note: I will not discuss the details of setting up PostgreSQL and PHP for Windows. I’m just telling how to make it work after successfully installing it but still
phpinfo() still says it is not loaded.
Installing PostgreSQL in Windows is as easy as installing MySQL. You download the installer and run it. Then you just follow the installer instructions and you’re done. Next, you modify your PHP installation to make PostgreSQL extension work.
For PHP installations that uses the MSI installer, it is easy to enable the PostgreSQL module and the PDO for PostgreSQL. Just a few click away from Add/Remove program in Control Panel (if you’re on Windows XP). Then you restart Apache or even restart Windows.
However, when you try to use the PostgreSQL extension functions (ex:
pg_connect) or the PDO equivalent, it does not work and it says that the PostgreSQL extension is not loaded. Yet you have a fully working PostgreSQL installation. You usually first encounter the problem when you install phpPgAdmin – a web based database tool for PostgreSQL powered by PHP.
The Problem and the Fix
The actual problem is that PHP cannot load the DLL for the PostgreSQL because it cannot find its dependent library – not that the extension is not found! php_pgsql.dll or php_pdo_pgsql.dll cannot load the module for your PostgreSQL installation.
The PHP extension will look for libpq.dll which is found of your PostgreSQL installation. The simple fix is to add the path than contains that file to your environment PATH. Here is the steps I’ve taken.
Based on Windows XP Professional SP2.
Open system properties. You can right-click My Computer -> select Properties.
The system properties window is shown. Click on the Advanced tab.
The Advanced tab is shown. Click on Environment Variables.
The Environment Variables window is shown. Select on Path in the System variables group and click Edit.
Lastly, add the full path to the bin directory of your PostgreSQL installation. In my machine, it is located at
D:\Program Files\PostgreSQL\8.4\bin. Be sure to prepend
; (semi-colon) before adding the path.
Save your work and restart the computer. Check
phpinfo() if the PostgreSQL extension is loaded. Is should!