Article
Use PostgreSQL and PHP on Windows
I guess every SitePoint visitor has seen the words "Database Driven Website Using PHP & MySQL" a million times. Something you don't see every day is the combination "PHP & PostgreSQL" -- especially not with the post-script, "...on Windows".
Some people think it is incredibly difficult to run PostgreSQL on Windows, because there's no easy-to-use Win32 installer (at the time of writing, 7.3.4. is the latest version). Users have to install some sort of unix emulator first, and run the database system on this "platform".
The PostgreSQL Technical Documentation Site says, "The main PostgreSQL project plan is to add support for a native Windows version with our 7.4 release. This will probably be released in the middle of 2003..." I haven't seen this release yet… maybe late 2003? However, what this means is that, in the future PostgreSQL will be an even more appealing choice than MySQL!
Build a database driven Website with PostgreSQL? Why would you do such a thing? Isn't the rule that if you're using PHP, your database is MySQL -- period!?
Well, in my case, it was until I was assigned the task to convert an ASP/MSSQL solution to PHP/PostgreSQL. The reasons for the change?
- we (the assigner) didn't want to use Microsoft products any longer (due to the associated licensing cost)
- we wanted database features that weren't present in MySQL (foreign keys etc)
To me, it also seemed to be a good idea to have both the old ASP stuff and the new PHP install on the same (Windows) development machine when we performed this "translation". Another good reason to try the Windows, PostgreSQL, and PHP combination is that it's always fun to find the answer to that age-old question, "How difficult can it be?"
I must admit up-front that I'm a Microsoft-indoctrinated sort of developer. Primarily I use MSSQL, less often MySQL, and almost never PostgreSQL. It was the same with operating systems; 90% of the time I used Windows, 10% Linux. Despite those facts, my move to PostgreSQL ended happily. And if it was doable for me, then it is for you, too.
For more reading on alternatives to MySQL, see Wayne Luke's article Moving Beyond MySQL - High End Database Solutions.
Note that this tutorial assumes you have read chapter one in Kevin Yank's excellent Build Your Own Database Driven Website Using PHP and MySQL, or some other tutorial that explains how to install Apache and PHP on Windows. More importantly, I assume that after learning this process, you have successfully installed a Web server with PHP "plug-in".
Let's begin!
Cygwin Installation
The first task is to download and install Cygwin, which is a linux-like environment for Windows. At the time of writing, the latest version of Cygwin is 1.5.5. It's not a bad idea to rename setup.exe to cygwin_setup.exe -- this way, the file will be easier to locate when you want to add/remove/update Cygwin components later.
Naturally, you should be logged in as a user belonging to the group Administrators when you launch the setup program.
The Cygwin Net Release Setup Program will ask you the following eight questions:
- Disable Virus Scanner?
As some antivirus programs may cause problems during the Cygwin installation, you have the option to disable the virus scanner temporarily. In most cases, the default option Leave Virus Scanner Alone works. (If no antivirus program is detected, then -- sooner or later – you'll encounter another type of problem...) - Choose Installation Type
Select Install from Internet. - Choose Installation Directory
You can use the default options here too, unless you have some really good reason not to designate C:\cygwin as Root Directory, Install For All Users and select Unix as Default Text File Type. - Select Local Package Directory
The folder you specify here is the location where the downloaded installation files will be saved. Enter the path of your choice, e.g. E:\cygwinfiles. - Select Connection Type
Here you select the appropriate connection type; the default option is Direct Connection. Most likely, your antivirus/firewall program will pop up after you click Next, to check if you want to allow cygwin_setup.exe to access the Internet. Yes, that's exactly what we want! - Choose Download Site
Select any site from the list, preferably one that's close to you. Depending on your connection speed, it will take a moment or two before the next screen appears. - Select Packages
You only need to make two additions to the by-default selected packages; cygrunsrv (category Admin) and postgresql (category Database). Cygrunsrv is an NT/W2K service initiator, and PostgreSQL is... well, you know what it is. At the time of writing, the version of the Database Management System is 7.3.4.If you're a control freak, verify that cygipc (category Devel) is among the pre-selected packages. Cygipc provides IPC (InterProcess Communication) support for Cygwin, but will eventually be replaced by the "cygwin-daemon". At the time of writing, PostgreSQL depends on cygipc.
Now you can take a break and drink some coffee while the files are downloaded and installed.
- Create Icons
Well, this last screen is quite self-explanatory.
Click Finish, and smile happily when you see...
Installation complete!
Johan is an IT consultant and teacher with interests in logic, mathematics, and database design. He's constantly forced to learn more about computers and programming by his inquisitive nephew.