Thread: Accessing Postgres db from apache using PHP

Accessing Postgres db from apache using PHP

From
"bruce"
Date:
Hi...

I'm new to Postgres... I've installed Postgres on a Linux RH 8.0 server. I'm
trying to access a pgsql db from an Apache website, using PHP(v-4.2.2). I've
taken a look at google and through the archives with no real luck...

I created a Linux user and given the user rights to the Postgres group.
I created a Linux user (gforge) and given it rights to the Postgres group.
Using the gforge user, I created a postgres user, using
    -> createuser gforge - W
I gave the user the password of ->gforge

I gave the user rights to add/create databases, but not add new users
I then installed a database.
    pgsql -U gforge -d gforge < gforge3.sql

At this point, I can see that I have a db in postgres....
I did a "\l" and it displayed:
    name    owner    encoding
-->>    gforge    gforge    sql_ascii


In my php code, I have....

$sys_dbhost = "localhost";
$sys_dbname = "gforge";
$sys_user = "gforge";
$sys_dbpasswd = "gforge";

i then do a:
    $tmp = "host=localhost dbname=gforge user=gforge password=gforge";
    $conn = @pg_connect($tmp);

at this point $conn is not set... it should be a resouce if I've connect to
the DB..!!!!!!!

So.. the question... Anybody have any idea as to what I've done wrong/left
out/etc...

Any pointers/comments/criticisms would be helpful.....
Any sample docs, or code that illustrates how this should be accomplished
would be helpful...


Thanks...

Bruce Douglas
(925) 866-2790
bedouglas@earthlink.net


Re: Accessing Postgres db from apache using PHP

From
"A.Bhuvaneswaran"
Date:
>     $tmp = "host=localhost dbname=gforge user=gforge password=gforge";
>     $conn = @pg_connect($tmp);
>
> at this point $conn is not set... it should be a resouce if I've connect to
> the DB..!!!!!!!

Did you uncomment the below line in /etc/php.ini?
;extension=pgsql.so
If not do it and restart your httpd. Hope it helps.

regards,
bhuvaneswaran


Re: Accessing Postgres db from apache using PHP

From
christoph.dellavalle@goetheanum.ch
Date:
I'm not sure, but I think you have to start postgres with the option -i to allow it to use TCP/IP.

if you get a socket error look at the php.ini, sockets.unix_socket_directory = /tmp, go to the directory indicated
thereand have a look, if there is a socket.  PHP and postgres both have a setting for the socket directory and they
mustbe the same. 

Hope it helps,
Christoph Della Valle

>-----Ursprüngliche Nachricht-----
>Von: bruce [mailto:bedouglas@earthlink.net]
>Gesendet: Montag, 28. Juli 2003 03:00
>An: pgsql-novice@postgresql.org; pgsql-php@postgresql.org
>Betreff: [NOVICE] Accessing Postgres db from apache using PHP
>
>
>Hi...
>
>I'm new to Postgres... I've installed Postgres on a Linux RH
>8.0 server. I'm
>trying to access a pgsql db from an Apache website, using
>PHP(v-4.2.2). I've
>taken a look at google and through the archives with no real luck...
>
>I created a Linux user and given the user rights to the Postgres group.
>I created a Linux user (gforge) and given it rights to the
>Postgres group.
>Using the gforge user, I created a postgres user, using
>    -> createuser gforge - W
>I gave the user the password of ->gforge
>
>I gave the user rights to add/create databases, but not add new users
>I then installed a database.
>    pgsql -U gforge -d gforge < gforge3.sql
>
>At this point, I can see that I have a db in postgres....
>I did a "\l" and it displayed:
>    name    owner    encoding
>-->>    gforge    gforge    sql_ascii
>
>
>In my php code, I have....
>
>$sys_dbhost = "localhost";
>$sys_dbname = "gforge";
>$sys_user = "gforge";
>$sys_dbpasswd = "gforge";
>
>i then do a:
>    $tmp = "host=localhost dbname=gforge user=gforge
>password=gforge";
>    $conn = @pg_connect($tmp);
>
>at this point $conn is not set... it should be a resouce if
>I've connect to
>the DB..!!!!!!!
>
>So.. the question... Anybody have any idea as to what I've
>done wrong/left
>out/etc...
>
>Any pointers/comments/criticisms would be helpful.....
>Any sample docs, or code that illustrates how this should be
>accomplished
>would be helpful...
>
>
>Thanks...
>
>Bruce Douglas
>(925) 866-2790
>bedouglas@earthlink.net
>
>
>---------------------------(end of
>broadcast)---------------------------
>TIP 4: Don't 'kill -9' the postmaster
>

Re: Accessing Postgres db from apache using PHP

From
Jean-Christian Imbeault
Date:
>i then do a:
>    $tmp = "host=localhost dbname=gforge user=gforge
>password=gforge";
>    $conn = @pg_connect($tmp);


Just jumping in here but don't you need to specify the port?

$conn = pg_connect("host=localhost port=5432 dbname=gforge user=gforge");

Also, don't use the @, that way you can see any useful error messages
that are displayed :)

Oh, and you did change the conf file to let postgres use TCP/IP?

tcpip_socket = true
port=5432

Jc



Re: Accessing Postgres db from apache using PHP

From
"bruce"
Date:
hi....

I just searched my drive... I find the following files:
    postgresql
    postgres.conf.sample

I do not find a file --> postgres.conf

Where should it be/reside.... The information in the postgres.conf.sample
has the tcpip_socket parameter !!!

This might be the issue!!!!!!!!!!!!

Thanks

Bruce
bedouglas@earthlink.net


-----Original Message-----
From: pgsql-novice-owner@postgresql.org
[mailto:pgsql-novice-owner@postgresql.org]On Behalf Of Jean-Christian
Imbeault
Sent: Monday, July 28, 2003 12:21 AM
To: christoph.dellavalle@goetheanum.ch; pgsql-novice
Subject: Re: [NOVICE] Accessing Postgres db from apache using PHP


>i then do a:
>    $tmp = "host=localhost dbname=gforge user=gforge
>password=gforge";
>    $conn = @pg_connect($tmp);


Just jumping in here but don't you need to specify the port?

$conn = pg_connect("host=localhost port=5432 dbname=gforge user=gforge");

Also, don't use the @, that way you can see any useful error messages
that are displayed :)

Oh, and you did change the conf file to let postgres use TCP/IP?

tcpip_socket = true
port=5432

Jc



---------------------------(end of broadcast)---------------------------
TIP 8: explain analyze is your friend


Re: Accessing Postgres db from apache using PHP

From
Josh Berkus
Date:
Christoph,

> I'm not sure, but I think you have to start postgres with the option -i to
> allow it to use TCP/IP.

As of 7.2, it's better to do this in the postgresql.conf file, just because
then you don't have to remember to use "-i" every time you start up
PostgreSQL.

--
Josh Berkus
Aglio Database Solutions
San Francisco

Re: [PHP] Accessing Postgres db from apache using PHP

From
Rod Taylor
Date:
> i then do a:
>     $tmp = "host=localhost dbname=gforge user=gforge password=gforge";
>     $conn = @pg_connect($tmp);

Since host=localhost, the client will attempt to connect to the server
on 127.0.0.1.  By default, TCPIP access is not enabled.  To do so, see
postgresql.conf, make the change to the tcpip variable, and HUP the
server.

However, if you leave host=<blank>, the client will attempt to connect
to the server via the local domain socket which is both enabled by
default and should be a touch faster due to more efficient data
transmission.

Attachment

Re: [PHP] Accessing Postgres db from apache using PHP

From
"Cornelia Boenigk"
Date:
Hi Bruce

> $conn = @pg_connect($tmp);

For testing you should remove the '@' from the PHP-commands to see the
errormessages from the PostgreSQL server. It may help you to find the
errors.

Regards
Conni