By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,177 Members | 976 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,177 IT Pros & Developers. It's quick & easy.

CGI program cannot access database

P: n/a
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';por t=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

---------------------------(end of broadcast)---------------------------
TIP 5: Have you checked our extensive FAQ?

http://www.postgresql.org/docs/faqs/FAQ.html

Nov 23 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
* Samik Raychaudhuri <sa***@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';por t=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

---------------------------(end of broadcast)---------------------------
TIP 6: Have you searched our list archives?

http://archives.postgresql.org

Nov 23 '05 #2

P: n/a
On Fri, Oct 08, 2004 at 08:17:07AM -0700, Steven Klassen wrote:
* Samik Raychaudhuri <sa***@freeshell.org> [2004-10-07 21:12:31 -0500]:
$dbh=DBI->connect("dbi:Pg:dbname='samik';host='abc.org';por t=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/

---------------------------(end of broadcast)---------------------------
TIP 9: the planner will ignore your desire to choose an index scan if your
joining column's datatypes do not match

Nov 23 '05 #3

P: n/a
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 <sa***@freeshell.org> [2004-10-07 21:12:31 -0500]:
$dbh=DBI->connect("dbi:Pg:dbname='samik';host='abc.org';por t=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

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

Nov 23 '05 #4

P: n/a

On 10/8/2004 10:17 AM, Steven Klassen wrote:
$dbh=DBI->connect("dbi:Pg:dbname='samik';host='abc.org';por t=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

---------------------------(end of broadcast)---------------------------
TIP 7: don't forget to increase your free space map settings

Nov 23 '05 #5

P: n/a
* Samik Raychaudhuri <sa***@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

---------------------------(end of broadcast)---------------------------
TIP 4: Don't 'kill -9' the postmaster

Nov 23 '05 #6

P: n/a
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

---------------------------(end of broadcast)---------------------------
TIP 9: the planner will ignore your desire to choose an index scan if your
joining column's datatypes do not match

Nov 23 '05 #7

P: n/a
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/

---------------------------(end of broadcast)---------------------------
TIP 9: the planner will ignore your desire to choose an index scan if your
joining column's datatypes do not match

Nov 23 '05 #8

P: n/a
On Fri, Oct 08, 2004 at 10:09:13AM -0700, Steven Klassen wrote:
* Samik Raychaudhuri <sa***@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-..._Class_Methods

-Dom

---------------------------(end of broadcast)---------------------------
TIP 5: Have you checked our extensive FAQ?

http://www.postgresql.org/docs/faqs/FAQ.html

Nov 23 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.