Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

PHP and PostgreSQL: pg_connect problem 1

Status
Not open for further replies.

gwinn7

Programmer
Feb 10, 2001
1,004
US
Has anyone here developed or is developing a PHP project against a PostgreSQL database? If so...

Can you explain or help me resolve this problem:

When I try to use the pg_connect() function in a PHP page, an error occurs:

"Call to Undefined Function"

This error is triggered when I step through the PHP code and land on this line...

$connection = pg_connect("test");

What does this mean? How can I resolve it?

Thanks in advance,
Gary
gwinn7
 
It means PHP was not build with PostgreSQL support. PostgreSQL is a module that must either be included in the compile configuration, or dynamically loaded by pointing to it in php.ini or using the dl() function.

MySQL and is the only database that PHP automatically supports in a default build. (Oh, and also generic ODBC on Windows builds of PHP).

So, if you have compiled PHP, yourself, the best method is to add the --with-pgsql option in the ./configure statement. If you have not done this before, you might need a little coaching here. What server platform are you running on? Do you have control over the PHP configuration on this machine? -------------------------------------------

"Calculus is just the meaningless manipulation of higher symbols"
                          -unknown F student
 
Thank you for your response.

I am currently running Red Hat 7.2. I have control over the PHP install. However, I used RPMs from RedHat to update the PHP and PostgreSQL. If it can be avoided, I would rather not have to recompile.

So, how should we proceed? What about the php.ini? Is that the option we should take here? If so, what should be the entry?

Gary
gwinn7




 
I strongly recommend you become familiar with how to compile PHP from source, because it will open up a wide range of possibilities for you.

Anyway, to dynamically load the PostgreSQL module in PHP, you need to edit php.ini, and look for the section called:
Code:
;;;;;;;;;;;;;;;;;;;;;;
; Dynamic Extensions ;
;;;;;;;;;;;;;;;;;;;;;;

Under here, you will see examples of how the extension-loading syntax looks. In your particular case, it should be

extension=pgsql.so

This is assuming that pgsql.so is located in (RedHat's standard location) /usr/lib/php4/pgsql.so.

You will probably also have to fool around with the 'extension_dir' directive also, which specifies where PHP should look for extensions. By default, the setting is:

extension_dir = ./

But you might need to change it to

extension_dir = /usr/lib/php4

Or even

extension_dir = /usr/lib

On this note, I am not sure, because I rarely load extensions dynamically, preferring them to be compiled into PHP.

Anyway, whenever you want to test your install, just run a small PHP script with the contents

<?php
phpinfo();
?>

And it will tell you all kinds of things about your PHP installation, including wherther &quot;pgsql&quot; is installed. -------------------------------------------

&quot;Calculus is just the meaningless manipulation of higher symbols&quot;
                          -unknown F student
 
I will give it a try.

Yes, I agree with the learning to compile the PHP from source. Its on my list of things to learn how to do.

I will let you know tomorrow how things went.

Thank you so much for your help!

Gary
gwinn7
 
Ok, here is what happened...

RE: extension=pgsql.so
I had to change the path to the pgsql.so. I ended up enabling this line and changing it to:
&quot;extension=/usr/lib/pgsql/pgsql.so&quot;

RE: extension_dir = /usr/lib/php4
This was already there. I changed nothing.

RE: PHP Info Script
I ran the script below and it displayed the PHP information just fine.

<?php
phpinfo();
?>

Conclusion:
The same problem still occurs when I try to run my pg_connect command.

Anything else to check?

Thanks,
Gary
gwinn7
 
OK, I just downloaded the source files for PHP. If you want, we could compile the PHP source.

Do I need to uninstall my old PHP installation first?

Gary
gwinn7
 
Update...

I ran the command from the folder the tar file created:

./configure --with-pgsql=/usr/bin

also ran...

./configure --with-pgsql

The &quot;/usr/bin&quot; folder was where the postgres installation package indicated that it was installed to. Both occurences of the above, I received a lot of &quot;missing&quot; results and the last line indicated:

&quot;configure: error: no acceptable cc found in $PATH&quot;

I am likely not doing this correctly (duh). I read the install notes that came with the PHP source, but I didn't recognize anything that would be helpful.

It would be helpful if you could list the exact steps to do this and describe where I am failing in this. My PostgreSQL installation does not seem to be in the default location as you specified.

Thanks,
Gary
gwinn7
 
FYI: The next time I will be able to respond is this evening (EST). I look forward to any other tips.

Gary
gwinn7
 
OK, a few mis-steps along the way here... ;-).

Specifically, you are not supposed to put the path in the extension statement. Extensions are supposed to be all under one directory, which in your case is /usr/lib/php4. If there is not a pgsql.so file in that directory, then you need to place it there. I am not absolutely sure that moving /usr/lib/pgsql/pgsql.so to /usr/lib/php4 is the answer though. There should be a &quot;pgsql.so&quot; file included with your PHP build. As I said, I have not really spent any time with this method, preferring direct compilation.

In regards to compiling the source, you need to know the concept of base directories. Traditionally, Unix systems had two places to put software: /usr, or /usr/local. Under these, they would then have lib/, share/, bin/, include/, etc... The idea was that each piece of software would put it's executables in bin/, its library files in lib/, etc... Thus the base directory would be /usr or /usr/local, and the individual files for that application would be distributed as expected inside the subdirectories. So, when compiling a program which would use shared objects from another program, you would often just say --with-application=/usr/local, or --with-application=/usr, depending on which was the base directory. (see * below)

In your case, it looks like you should have --with-pgsql=/usr as your configuration directive. Definitely not =/usr/bin, though, because PHP doesn't need the PostgreSQL executable; it needs the shared libraries.

>>My PostgreSQL installation does not seem to be in the default location as you specified.

I was only specifying where PHP usually keeps its local copy of the PostgreSQL module (/usr/lib/php4/pgsql.so), not the main PostgreSQL installation itself.

* (Note) Unfortunately, many Linux systems decided to break the standard Unix pattern a little, and SOME software would be installed using the base directories, while other software would have its own subdirectory, such as /usr/local/pgsql (with /usr/local/pgsql/bin, /usr/local/pgsql/lib as subdirectories). In this case, you would of course then have to specify --with-pgsql=/usr/local/pgsql. -------------------------------------------

&quot;Calculus is just the meaningless manipulation of higher symbols&quot;
                          -unknown F student
 
If I use the correct path in the ./configure statement, then those errors I experienced will vanish?

Since, my biggest problem at this point is to get the source to compile properly, this is even with/without running a --with switch in the command line. Perhaps I am missing a procedure somewhere? Maybe I am running the ./configure from the wrong folder?

Still learning this stuff!
Gary
gwinn7
 
Dohh... I dodn't look close enough at your error messages above: &quot;configure: error: no acceptable cc found in $PATH&quot;. This means you don't have a compiler available to build anything from source code. This could be construed as a no-op ;-).

Check out your system and make sure you have installed the &quot;Developer&quot; libary RPM's, such as gcc (Gnu C Compiler), Automake, bison (sometimes useful), maybe even lex and yacc. If you have the developer libraries installed, and you still get this error, it is because they are somehow not in a standard location, so they can't be found on the standard path. If that is the case, you need to revisit exactly what was done to the system when, because this is &quot;highly irregular&quot;. -------------------------------------------

&quot;Calculus is just the meaningless manipulation of higher symbols&quot;
                          -unknown F student
 
Ok! DOH!!! You just jarred my memory about my installing this copy of Linux. I did not install the development tools and compilers! OK, I will look for those. If you have an exact list of RPMs at the top of your head, feel free to mention them. Otherwise, I will hunt for them, it shouldn't be too hard to find them. Thanks again.

I will keep you posted on my progress over the next few of days.

Gary
gwinn7

 
After re-installing Red Hat 7.2, Apache, and Postgresql, I compiled PHP source with the option &quot;--with-pgsql&quot;. Unfortunately, I still get the same problem with the undefined function thing.

I will try again and include the path, but its getting frustrating. Can you help?

Gary
gwinn7
 
Gwinn7,

This is one of the reasons I avoid Linux, and especially RedHat, whenever I want true customization of my server software. The standard Linux modules work fine as long as you are satisfied with their standard configurations. Once you try to step out of that, you start running into all kinds of little problems.

Read my third post again very carefully. On Unix systems, the only way that different software packages can integrate with each other is through directory structure. There is no such thing as a &quot;registry&quot; like Windows has. This is a good thing, since it provides flexibility, but unless you understand how these relationships work, customizing of compile options can be confusing.

You re-installed Redhat, including PostgreSQL. This means that PostgreSQL is in the standard Redhat location, which might not be the standard Unix location expected by PHP. Thus simply saying --with-pgsql might not be enough. That's when you find out where your pgsql base directory is. If it is in /usr/local/pgsql, then that is what you would specify: --with-pgsql=/usr/local/pgsql

Try running &quot;./configure --help | less&quot; from your PHP source directory. This will output all the ./configure options, along with the default directories where PHP looks for things. (Piping it to the less utility makes it so you can scroll up or down through the message with your PageUp and PageDown keys; hit &quot;q&quot; to quit.)

Here's a compile debug tip: When you run your ./configure statement, add the --no-create option on the end, and then pipe the output to a text file. Something like this:

./configure --with-pgsql=/usr/local/pgsql --with-gd {etc... other options} --no-create > config_output.txt

This will capture all of that scrolling output that goes by too fast to read. Now, when the ./configure statement is done, you can simply open that text file and browse it for error messages. If you search for the word &quot;pgsql&quot; in there, it will actually tell you if it can't find PostgreSQL. -------------------------------------------

&quot;Calculus is just the meaningless manipulation of higher symbols&quot;
                          -unknown F student
 
Thank you for responding! When I compiled a second time, I did add what I thought to be the base location (path) of the pgsql installation. However, that did not help either, same result.

Your feedback is definitely helpful. I will try that too. Is is possible I am not recognizing the correct base installation folder?

You are being a life saver! Thanks again so much for following up with me.

Gary
gwinn7
 
OK, I am re-reading your other post. I will let you know.

Gary
gwinn7
 
Ok it didn't seem to make a difference. I re-ran the ./Configure command with the &quot;--with-pgsql=/usr&quot; option. I selected the &quot;/usr&quot; folder based on what I discovered when I ran &quot;Package Manager&quot; in the KDE. It said the following:

Short RPM LIST (with common directory references)
==========================
postgresql /usr/bin
/usr/share

postgresql-contrib /usr/lib/pgsql
postgresql-libs /usr/lib
==========================

This led me to believe that my folder specification, based on your comments, should be &quot;/usr&quot;

However, I am still stuck at the same problem. When I checked the logfile I created when I piped out the ./configure output, it said had one reference to the pgsql and there did not seem to be any errors.

Yes, after I ran the ./configure command, I did also run the &quot;make&quot; and &quot;make install&quot; commands to finish the installation.

Ok, so am I being ignorant of anything else?

Gary
gwinn7

 
'Nother thing I have seen happen... Sometimes the user installs a fresh compile of PHP, but the previous (default) install of PHP is located elsewhere, and that is the version that Apache is loading.

Check in your Apache httpd.conf file, and search for *every* line with the word &quot;php&quot; in it. Look for the addmodule and loadmodule lines, and in the section that looks like:
Code:
    #AddType application/x-httpd-php3 .php3
    #AddType application/x-httpd-php3-source .phps
    #
    # And for PHP 4.x, use:
    #
    AddType application/x-httpd-php .php .php3 .phtml
    AddType application/x-httpd-php-source .phps

Notice that the section for PHP3 should be commented, and the only the PHP4 lines should be active.

Feel free to post all relevant lines from httpd.conf here, and I will be glad to have a look. -------------------------------------------

&quot;Calculus is just the meaningless manipulation of higher symbols&quot;
                          -unknown F student
 
OK, it looks like the PHP3 lines are not commented. Here are the lines...

First we see...

<IfDefine HAVE_PHP>
LoadModule php_module modules/mod_php.so
</IfDefine>
<IfDefine HAVE_PHP3>
LoadModule php3_module modules/libphp3.so
</IfDefine>
<IfDefine HAVE_PHP4>
LoadModule php4_module modules/libphp4.so
</IfDefine>

Next we see...

<IfDefine HAVE_PHP>
AddModule mod_php.c
</IfDefine>
<IfDefine HAVE_PHP3>
AddModule mod_php3.c
</IfDefine>
<IfDefine HAVE_PHP4>
AddModule mod_php4.c
</IfDefine>

After that comes...

# The following is for PHP4 (conficts with PHP/FI, below):
<IfModule mod_php4.c>
AddType application/x-httpd-php .php4 .php3 .phtml .php
AddType application/x-httpd-php-source .phps
</IfModule>

# The following is for PHP3:
<IfModule mod_php3.c>
AddType application/x-httpd-php3 .php3
AddType application/x-httpd-php3-source .phps
</IfModule>

# The following is for PHP/FI (PHP2):
<IfModule mod_php.c>
AddType application/x-httpd-php .phtml
</IfModule>


I will try commenting out the PHP3 and the PHP/FI lines and see what happens. FYI: I will be following up this message in less than 30 minutes. (current time 9:06am EST)

Gary
gwinn7
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top