CGI program cannot access database

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Samik Raychaudhuri

    CGI program cannot access database

    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:P g:dbname='samik ';host='abc.org ';port=5432","s amik","xxxx");
    $query="select * from temp";
    $sth=$dbh->prepare($query );
    $sth->execute ;
    while(@data=$st h->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=5 432','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=5 432','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?



  • Steven Klassen

    #2
    Re: CGI program cannot access database

    * Samik Raychaudhuri <samik@freeshel l.org> [2004-10-07 21:12:31 -0500]:
    [color=blue]
    > 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.[/color]

    Has this ever worked?
    [color=blue]
    > $dbh=DBI->connect("dbi:P g:dbname='samik ';host='abc.org ';port=5432","s amik","xxxx");[/color]

    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?



    Comment

    • Michael Fuhr

      #3
      Re: CGI program cannot access database

      On Fri, Oct 08, 2004 at 08:17:07AM -0700, Steven Klassen wrote:[color=blue]
      > * Samik Raychaudhuri <samik@freeshel l.org> [2004-10-07 21:12:31 -0500]:
      >[color=green]
      > > $dbh=DBI->connect("dbi:P g:dbname='samik ';host='abc.org ';port=5432","s amik","xxxx");[/color]
      >
      > 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?[/color]

      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


      ---------------------------(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

      Comment

      • Samik Raychaudhuri

        #4
        Re: CGI program cannot access database

        Hello,
        Thanks very much for the replies. Please find follow-ups inline.

        On 10/8/2004 11:05 AM, Michael Fuhr wrote:
        [color=blue]
        >On Fri, Oct 08, 2004 at 08:17:07AM -0700, Steven Klassen wrote:
        >
        >[color=green]
        >>* Samik Raychaudhuri <samik@freeshel l.org> [2004-10-07 21:12:31 -0500]:
        >>
        >>
        >>[color=darkred]
        >>>$dbh=DBI->connect("dbi:P g:dbname='samik ';host='abc.org ';port=5432","s amik","xxxx");
        >>>
        >>>[/color]
        >>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?
        >>
        >>[/color]
        >
        >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?
        >
        >[/color]
        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.
        [color=blue]
        >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.
        >
        >
        >[/color]
        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

        Comment

        • Samik Raychaudhuri

          #5
          Re: CGI program cannot access database


          On 10/8/2004 10:17 AM, Steven Klassen wrote:
          [color=blue]
          >$dbh=DBI->connect("dbi:P g:dbname='samik ';host='abc.org ';port=5432","s amik","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.
          >
          >
          >[/color]
          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

          Comment

          • Steven Klassen

            #6
            Re: CGI program cannot access database

            * Samik Raychaudhuri <samik@freeshel l.org> [2004-10-08 11:37:49 -0500]:
            [color=blue]
            > 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.[/color]

            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

            Comment

            • Samik Raychaudhuri

              #7
              Re: CGI program cannot access database

              I solved the problem, getting hints from Maarten. This is for posterity.
              I looked at this thread:

              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

              Comment

              • Michael Fuhr

                #8
                Re: CGI program cannot access database

                On Fri, Oct 08, 2004 at 11:35:34AM -0500, Samik Raychaudhuri wrote:[color=blue]
                > On 10/8/2004 11:05 AM, Michael Fuhr wrote:[color=green]
                > >
                > >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.
                > >[/color]
                > What exactly you mean by packet filters? How do I check what packet
                > filters are active?[/color]

                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


                ---------------------------(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

                Comment

                • Dominic Mitchell

                  #9
                  Re: CGI program cannot access database

                  On Fri, Oct 08, 2004 at 10:09:13AM -0700, Steven Klassen wrote:[color=blue]
                  > * Samik Raychaudhuri <samik@freeshel l.org> [2004-10-08 11:37:49 -0500]:
                  >[color=green]
                  > > 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.[/color]
                  >
                  > Just not used to seeing the semi-colons in the DSN. Usually the
                  > dbname, host, and port directives are space delimited.[/color]

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



                  -Dom

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



                  Comment

                  Working...