Thread: CGI program cannot access database

CGI program cannot access database

From
Samik Raychaudhuri
Date:
Hello,
I am trying to run a perl CGI program for accessing a database. The
program runs fine from command prompt, producing expected output. But it
gives errors while running as CGI.

--------- Program -------------
use DBI;
use DBD::Pg;
print "Content-type: text/plain\n\n";
$dbh=DBI->connect("dbi:Pg:dbname='samik';host='abc.org';port=5432","samik","xxxx");
$query="select * from temp";
$sth=$dbh->prepare($query);
$sth->execute ;
while(@data=$sth->fetchrow_array())
{       print $data[0].", ".$data[1]."<br>\n";        }
$sth->finish;
$dbh->disconnect();
-------------------------------------

This was giving me the following error:
DBI connect('dbname='samik';host='abc.org';port=5432','samik',...)
failed: could not translate host name "abc.org" to address: Unknown host
at /var/www/cgi-bin/test/dbex.pl line 6

When I replaced the host name with its IP address, I got the following
error:
DBI connect('dbname='samik';host='a.b.c.d';port=5432','samik',...)
failed: could not create socket: Operation not permitted at
/var/www/cgi-bin/test/dbex.pl line 6

Has anyone come across this problem before? Any pointers are welcome.
Thanks for your time and regards,
-Samik

Re: CGI program cannot access database

From
Steven Klassen
Date:
* Samik Raychaudhuri <samik@freeshell.org> [2004-10-07 21:12:31 -0500]:

> I am trying to run a perl CGI program for accessing a database. The
> program runs fine from command prompt, producing expected
> output. But it gives errors while running as CGI.

Has this ever worked?

> $dbh=DBI->connect("dbi:Pg:dbname='samik';host='abc.org';port=5432","samik","xxxx");

It seems that machine can't resolve abc.org (although I can). The
format of your connect string is a little unorthodox, but it seems to
work in general.

What does 'dig abc.org' return on this machine?

--
Steven Klassen - Lead Programmer
Command Prompt, Inc. - http://www.commandprompt.com/
PostgreSQL Replication & Support Services, (503) 667-4564

Re: CGI program cannot access database

From
Michael Fuhr
Date:
On Fri, Oct 08, 2004 at 08:17:07AM -0700, Steven Klassen wrote:
> * Samik Raychaudhuri <samik@freeshell.org> [2004-10-07 21:12:31 -0500]:
>
> > $dbh=DBI->connect("dbi:Pg:dbname='samik';host='abc.org';port=5432","samik","xxxx");
>
> It seems that machine can't resolve abc.org (although I can). The
> format of your connect string is a little unorthodox, but it seems to
> work in general.
>
> What does 'dig abc.org' return on this machine?

I wonder if "abc.org" was just an example and not the real domain
name.  Samik, what's the real name?  Should it be resolvable from
the public Internet?

The original message reported the following error when connecting
by IP address:

failed: could not create socket: Operation not permitted

I wonder of packet filters are preventing the PostgreSQL connection
from working, and possibly causing DNS problems as well.

--
Michael Fuhr
http://www.fuhr.org/~mfuhr/

Re: CGI program cannot access database

From
Samik Raychaudhuri
Date:
Hello,
Thanks very much for the replies. Please find follow-ups inline.

On 10/8/2004 11:05 AM, Michael Fuhr wrote:

>On Fri, Oct 08, 2004 at 08:17:07AM -0700, Steven Klassen wrote:
>
>
>>* Samik Raychaudhuri <samik@freeshell.org> [2004-10-07 21:12:31 -0500]:
>>
>>
>>
>>>$dbh=DBI->connect("dbi:Pg:dbname='samik';host='abc.org';port=5432","samik","xxxx");
>>>
>>>
>>It seems that machine can't resolve abc.org (although I can). The
>>format of your connect string is a little unorthodox, but it seems to
>>work in general.
>>
>>What does 'dig abc.org' return on this machine?
>>
>>
>
>I wonder if "abc.org" was just an example and not the real domain
>name.  Samik, what's the real name?  Should it be resolvable from
>the public Internet?
>
>
abc.org is just an example. The real name is freesql.org, it is
resolvable from the public internet. I can also run the same program
from command prompt in Cygwin shell, and it runs fine.

>The original message reported the following error when connecting
>by IP address:
>
>failed: could not create socket: Operation not permitted
>
>I wonder of packet filters are preventing the PostgreSQL connection
>from working, and possibly causing DNS problems as well.
>
>
>
What exactly you mean by packet filters? How do I check what packet
filters are active?
Thanks for your time.
Regards,
-Samik

Re: CGI program cannot access database

From
Samik Raychaudhuri
Date:
On 10/8/2004 10:17 AM, Steven Klassen wrote:

>$dbh=DBI->connect("dbi:Pg:dbname='samik';host='abc.org';port=5432","samik","xxxx");
>
>
>It seems that machine can't resolve abc.org (although I can). The
>format of your connect string is a little unorthodox, but it seems to
>work in general.
>
>
>
Thanks very much for your reply. Why do you say that the connect string
is a little unorthodox? I just followed the standard DBI documentation.
Regards,
-Samik

Re: CGI program cannot access database

From
Steven Klassen
Date:
* Samik Raychaudhuri <samik@freeshell.org> [2004-10-08 11:37:49 -0500]:

> Thanks very much for your reply. Why do you say that the connect
> string is a little unorthodox? I just followed the standard DBI
> documentation.

Just not used to seeing the semi-colons in the DSN. Usually the
dbname, host, and port directives are space delimited.

I didn't catch earlier that you said it works on the command line but
not as a CGI. I'll have another look at the original post.

--
Steven Klassen - Lead Programmer
Command Prompt, Inc. - http://www.commandprompt.com/
PostgreSQL Replication & Support Services, (503) 667-4564

Re: CGI program cannot access database

From
Samik Raychaudhuri
Date:
I solved the problem, getting hints from Maarten. This is for posterity.
I looked at this thread:
http://www.cygwin.com/ml/cygwin/2004-09/msg01159.html
This thread (as well as Maarten) suggests to set/pass SYSTEMROOT
environment variable in Apache. I didn't know how to do that, googling
helped. This is what I did:
I added the following lines in httpd.conf
<IfModule mod_env.c>
        PassEnv SYSTEMROOT
</IfModule>
Scripts are running fine now.
References:
1. http://httpd.apache.org/docs/mod/mod_env.html#passenv
2. http://httpd.apache.org/docs/env.html

Thanks again to all who replied.
Regards,
-Samik

Re: CGI program cannot access database

From
Michael Fuhr
Date:
On Fri, Oct 08, 2004 at 11:35:34AM -0500, Samik Raychaudhuri wrote:
> On 10/8/2004 11:05 AM, Michael Fuhr wrote:
> >
> >failed: could not create socket: Operation not permitted
> >
> >I wonder of packet filters are preventing the PostgreSQL connection
> >from working, and possibly causing DNS problems as well.
> >
> What exactly you mean by packet filters? How do I check what packet
> filters are active?

Packet filters are a firewalling mechanism -- they can be configured
to allow or prohibit certain network traffic.  How to check on them
is system-dependent.

I didn't notice that you said the script ran successfully from the
command line; that makes it less likely that a firewall was interfering
because if that were the case, the script would probably fail under
all circumstances.  Since you've reported that you solved the
problem, the packet filter hypothesis can be dismissed.

--
Michael Fuhr
http://www.fuhr.org/~mfuhr/

Re: CGI program cannot access database

From
dom@happygiraffe.net (Dominic Mitchell)
Date:
On Fri, Oct 08, 2004 at 10:09:13AM -0700, Steven Klassen wrote:
> * Samik Raychaudhuri <samik@freeshell.org> [2004-10-08 11:37:49 -0500]:
>
> > Thanks very much for your reply. Why do you say that the connect
> > string is a little unorthodox? I just followed the standard DBI
> > documentation.
>
> Just not used to seeing the semi-colons in the DSN. Usually the
> dbname, host, and port directives are space delimited.

Perl DBI uses a different format to the usual libpq one.  It's
documented in DBD::Pg.

  http://search.cpan.org/~rudy/DBD-Pg-1.32/Pg.pm#DBI_Class_Methods

-Dom