connecting to pg in php


#1

I’m trying to connect to a postgres database using the code below but it doesn’t seem to work.

<?php echo 'point 0
'; $con = pg_Connect("dbname=testdb port=5432 user=postgres"); if ($con == 0) { die ("Could not connect"); } echo 'point 1
'; ?>

and all that shows up in my web browser is

point 0

Shoudn’t it at least be telling me that it ‘Could not connect’? What does this mean?

I’ve got my database in usr/local/pgsql/data, my username is ‘postgres’ and I don’t have a password, and I’m running my php file out of localhost (usr/local/apache2/htdocs). help!

Gibran


#2

Try adding the host in the connect string and make sure that the user is valid (test using an other client or something like that).

The documentation mentions that the connect string is a ‘new’ syntax, but no version is indicated as I can see. You might want to try the ‘old’ method to connect as your version might not support the new one. A lot of the documentation is being modified for PHP 5 at the moment.


#3

The version I’m using is 4.3.7RC1. I read the documentation too and it says I should be able to use the string format I’m using. After doing some more research, I tried re-starting my pgsql server with

[root@localhost /]# /usr/local/pgsql/bin/postmaster -i -p 5432 -h localhost -D /usr/local/pgsql/data

so the port, hostname, and TCP/IP connection should all work. Then I changed my code to

echo ‘point 0
’;
$con = pg_connect(“host=localhost port=5432 dbname=testdb user=postgres”);
if ($con == 0) {
die (“Could not connect”);
}
echo ‘point 1
’;

Still, all I get printing to the screen is

point 0

Does it matter where the testdb is located relative to the localhost directory (/usr/local/apache2/htdocs/)? Does it matter that I don’t have a password? Does it matter that I have 2 servers running (one for apache and one for the postgres database)?

I really think that it should be printing out “Could not connect” at the very least. Since it’s not, could there be a problem in how I installed php? If so, what do you suppose I could have done differently?

Gibran


#4

Is error reporting activated? You should at least see an error message if the script just dies.

Is the extension installed? (probably is, just double checking)

Both servers (pg and http) are totally independent. The paths shouldn’t cause any problem and since they don’t listen on the same port, there are no conflicts. Try setting a password it there is really nothing else you can think of…


#5

Look at the config file pg_hba.conf (in $PG_HOME/data/) to make sure your pg server is set up correctly for receiving tcp connections, etc.


#6

OK, I got display_errors working and it’s telling me

Fatal error: Call to undefined function: pg_connect() in /usr/local…

So it looks like support for pgsql was not installed, but I’m sure I configured php with

./configure --with-apxs2=/usr/local/apache2/bin/apxs --with-pgsql

According to ./configure --help, if the DIR path in ‘–with-pgsql[=DIR]’ is left blank (which it was), it will default to /usr/local/pgsql which shouldn’t be a problem, right?

Some things I noticed when I called phpinfo() were as follows:
-disable_functions = no value
-Hostname:port = localhost.localdomain:0
-Loaded Modules DO NOT include mod_pgsql or anything of the sort.
-Supported databases, however, DO support PostgreSQL.
-remote_port: 1024

Also, I checked out pg_hba.conf and everything seems to be set to ‘ALL’ (including databases).

What reason could there be for pg_connect() not being recognized?

Gibran


#7

At the top of phpinfo() there is a section with the configure command – post that for us?

Also, look for a postgresql section further down (it should be its own table of values like mysql). Mine looks like:
pgsql

PostgreSQL Support => enabled
PostgreSQL(libpq) Version => 7.4.2
Multibyte character support => enabled
SSL support => enabled
Active Persistent Links => 0
Active Links => 0

Directive => Local Value => Master Value
pgsql.allow_persistent => On => On
pgsql.auto_reset_persistent => Off => Off
pgsql.ignore_notice => Off => Off
pgsql.log_notice => Off => Off
pgsql.max_links => Unlimited => Unlimited
pgsql.max_persistent => Unlimited => Unlimited

(that is from the command-line php, not html output, but same difference)