DBD:Pg unable to connect as CGI process -- could not create socket

Discussion in 'Perl Misc' started by Jason, Jul 29, 2005.

  1. Jason

    Jason Guest

    All of the cgi scripts I create to access postgres databases work from
    the command line but do not work as CGI scripts. When accessed from a
    browser the following line:

    my $dbh =
    DBI->connect( dbi:pg:dbname=$DatabaseName;host=localhost;port=5432;",
    "postgres","mydog")

    Produces the following error:
    [Thu Jul 28 23:14:45 2005] [error] [client 207.161.60.253] DBI
    connect('dbname=myRunningData;host=localhost;port=5432;' ,
    'postgres',...) failed: could not create socket: Permission denied


    I have tried to establish different identity rules for apache and have
    ensured that the password for postgres is correct.

    I am currently running Fedora Core 3 with

    Postgresql 7.4.8-1.FC3.1
    Apache 2.0.52-3.1
    perl 5.8.5-14.FC3
    DBD::pg 1.43

    The lines below show my postgres config files.

    Thanks in advance.

    Jason


    pg_hba.conf:

    host all all 127.0.0.1 255.255.255.255
    trust
    local all all ident sameuser


    postgresql.conf (relavant portions)

    tcpip_socket = true
    max_connections = 100
    # note: increasing max_connections costs about 500 bytes of
    shared
    # memory per connection slot, in addition to costs from
    shared_buffers
    # and max_locks_per_transaction.
    #superuser_reserved_connections = 2
    port = 5432
    #unix_socket_directory = ''
    #unix_socket_group = ''
    unix_socket_permissions = 0777 # octal
     
    Jason, Jul 29, 2005
    #1
    1. Advertising

  2. Jason

    J. Gleixner Guest

    Jason wrote:
    > All of the cgi scripts I create to access postgres databases work from
    > the command line but do not work as CGI scripts. When accessed from a
    > browser the following line:
    >
    > my $dbh =
    > DBI->connect( dbi:pg:dbname=$DatabaseName;host=localhost;port=5432;",
    > "postgres","mydog")
    >
    > Produces the following error:
    > [Thu Jul 28 23:14:45 2005] [error] [client 207.161.60.253] DBI
    > connect('dbname=myRunningData;host=localhost;port=5432;' ,
    > 'postgres',...) failed: could not create socket: Permission denied


    I've never used PostgreSQL, however simply using Google and searching on
    the error message, found the following:
    http://sources.redhat.com/ml/cygwin/2004-09/msg01396.html

    No idea what SYSTEMROOT is, but it sounds like the issue is with the
    environment not being the same between the account that you use to run
    it via the command line and the account that's running the Web server.
     
    J. Gleixner, Jul 29, 2005
    #2
    1. Advertising

  3. Jason

    Jason Guest

    Unfortunately those articles are for cygwin and SYSTEMROOT is not a
    factor for me.

    However the difference between the apache account and is not clear to
    me. I cannot su apache because I get:

    This account is currently not available.

    Any idea how I can run the script as apache and/or get a prompt as
    apache?
     
    Jason, Jul 29, 2005
    #3
  4. Jason

    J. Gleixner Guest

    Jason wrote:
    > Unfortunately those articles are for cygwin and SYSTEMROOT is not a
    > factor for me.
    >
    > However the difference between the apache account and is not clear to
    > me. I cannot su apache because I get:
    >
    > This account is currently not available.
    >
    > Any idea how I can run the script as apache and/or get a prompt as
    > apache?


    Yes, but you could see your Web server's ENVIRONMENT variables, by
    running a CGI. e.g.

    # cat /usr/local/www/cgi-bin/env.cgi
    #!/usr/local/bin/perl
    use CGI qw:)standard);
    print header, start_html;
    print li($_ . ' : ' . $ENV{$_}), "\n" for keys %ENV;
    print end_html;

    Actually, it'd be easier to just look at the ENVIRONMENT variables for
    the account that works, and add the PostgreSQL related ones found in
    that environemnt, to your CGI script.
     
    J. Gleixner, Jul 29, 2005
    #4
  5. Jason

    Jason Guest

    Bill,

    Thank you for pointing to that article. Unfortunately my problem is
    more fundamental than PostgreSQL, and the information there did not fix
    my problem.

    I cannot get a socket connection via CGI at all. I opened my machine
    to allow tcp finger requests and using the code from chapter 10.3 of
    O'Rielly's CGI Programming and from
    http://www.infocopter.com/perl/socket-server.htm I created a program
    that returns the results of the finger command (see below).

    >From the command prompt the perl script creates the socket and gets the

    result. *As a cgi program it cannot create a socket*. This could be
    an issue for an apache group but I figured perl-cgi programmers would
    recognize my symptoms more easily.

    Thanks Jason

    finger.cgi program:

    #!/usr/bin/perl -w

    use strict;
    use Socket;
    use CGI::pretty;

    sub page {
    my ($q, $title, $content) = @_;
    print $q->header(), $q->start_html("$title");
    print $q->h2("$title"), $q->p("$content"), $q->end_html();
    exit;
    }

    my $q = new CGI::pretty();


    # initialize protocol, host and port
    my $proto = getprotobyname('tcp');
    my $host = 'localhost';
    my $port = getservbyname('finger','tcp');

    # get the host internet address and port address
    my $iaddr = inet_aton($host);
    my $paddr = sockaddr_in($port, $iaddr);

    # create the socket, connect to the port
    socket(my $SOCKET, PF_INET, SOCK_STREAM, $proto) or &page($q , "Error",
    "Socket: $!.\n");
    connect($SOCKET, $paddr) or &page($q , "Error", "connect: $!");

    #get reply from socket
    my $result = "";
    my $line;
    while ($line = <$SOCKET>) {
    $line = $q->escapeHTML($line);
    $result .= $q->p($line);
    }
    close ($SOCKET);

    #output reply from socket
    &page($q, "No Errors!", $result);
     
    Jason, Aug 1, 2005
    #5
  6. Jason

    Jason Guest

    Thanks Bill I will move the query to the suggested location.

    Jason
     
    Jason, Aug 1, 2005
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Adam Knight
    Replies:
    1
    Views:
    1,131
    Ken Cox - Microsoft MVP
    Dec 5, 2005
  2. John Smith
    Replies:
    0
    Views:
    3,134
    John Smith
    May 15, 2006
  3. Andrew Crook

    DBD::CSV questions and is there a DBD::XML?

    Andrew Crook, Sep 2, 2003, in forum: Perl Misc
    Replies:
    2
    Views:
    173
    Andrew Crook
    Sep 11, 2003
  4. Mike Solomon

    DBD::mysqlPP v DBD::mysql

    Mike Solomon, Feb 16, 2004, in forum: Perl Misc
    Replies:
    1
    Views:
    224
    James Willmore
    Feb 16, 2004
  5. Jim Cochrane
    Replies:
    0
    Views:
    119
    Jim Cochrane
    Aug 25, 2007
Loading...

Share This Page