PHP and PostgreSQL on Windows

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.

What Happens?

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.

Open system properties

The system properties window is shown. Click on the Advanced tab.

System properties window

The Advanced tab is shown. Click on Environment Variables.

Advanced tab

The Environment Variables window is shown. Select on Path in the System variables group and click Edit.

Environment variables

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.

Add postgres path

Save your work and restart the computer. Check phpinfo() if the PostgreSQL extension is loaded. Is should!

4 thoughts on “PHP and PostgreSQL on Windows”

  1. Thanks sooo much…it helped me a great deal. Finally I got my phppgAdmin to work

  2. hi there i am using win 7 but its not working. kinldy help me.i am using 7 ultimate sp1,trying to install postgresql,9.3 and navicat as well. when i am trying to create a new table i am getting issues as error:column”spcp\location” does not exist.
    Line 1;oid,spcname,pg_get_userbyid(spcowner) as owner,spcloatio…

Leave a reply

Your email address will not be published. Required fields are marked *