ralphie has asked for the wisdom of the Perl Monks concerning the following question:

hi all,

i have what seems to me to be an interesting problem ... i'm converting a series of html docs and cgi scripts that i use as a query engine to a couple of cgi scripts, one which receives authentication information while the other handles query processing. the query page is frame-based, and follows the example included in the cgi.pm readme.

the problem i'm having is that the connect string in the converted script won't work using variables for username and password. here's the output from the dbitrace log from the connect attempt...

DBI 1.20-nothread dispatch trace level set to 2 -> DBI->connect(DBI:Pg:dbname=jrado;host=raider-ralph, , ****) -> DBI->install_driver(Pg) for linux perl=5.006001 pid=3030 ruid=1 +001 euid=1001 install_driver: DBD::Pg version 1.01 loaded from /usr/local/lib +/perl/5.6.1/DBD/Pg.pm <- install_driver= DBI::dr=HASH(0x82459e4) -> default_user in DBD::_::dr for DBD::Pg::dr (DBI::dr=HASH(0x8245 +9e4)~0x82ec3d8 undef undef HASH(0x8109d38)) <- default_user= ( undef undef ) [2 items] at DBI.pm line 423 -> connect for DBD::Pg::dr (DBI::dr=HASH(0x82459e4)~0x82ec3d8 'dbn +ame=jrado;host=raider-ralph' undef **** HASH(0x8109d38)) pg_db_login pg_db_login: conn_str = >dbname=jrado host=raider-ralph<br> No Postgres username specified in startup packet. error 1 recorded: No + Postgres username specified in startup packet. !! ERROR: 1 'No Postgres username specified in startup packet.'

the log from a successful connection attempt, which i get when i explicitly pass a valid set of credentials in the connect string, is identical to that which i receive in the relevant portion of the earlier version when i pass the credentials in scalars. that log looks like this:

DBI 1.20-nothread dispatch trace level set to 2 -> DBI->connect(DBI:Pg:dbname=jrado;host=raider-ralph, (username d +eleted for post), ****) -> DBI->install_driver(Pg) for linux perl=5.006001 pid=32422 ruid= +1001 euid=1001 install_driver: DBD::Pg version 1.01 loaded fro +m /usr/local/lib/perl/5.6.1/DBD/Pg.pm <- install_driver= DBI::dr=HASH(0x8225fd8) -> connect for DBD::Pg::dr (DBI::dr=HASH(0x8225fd8)~0x8100750 'dbn +ame=jrado;host=raider-ralph' '(username deleted for post)' **** HASH( +0x80f6174)) pg_db_login pg_db_login: conn_str = >dbname=jrado host=raider-ralph user=(username + deleted) password=(password deleted) <- connect= DBI::db=HASH(0x8100714) at DBI.pm line 426

in both scripts the $username and $password scalars are successfully filled from parameters, and the connect statement is the standard $dbh=DBI->connect($dsn,$username,$password) or die print "error connecting to database ", $DBI::errstr, "\n";

obviously, the scalar values are not getting passed in the connect string in the problematic context, but at this point i'll be darned if i can figure out why. (those who feel i'm being excessively politically-correct by not using the more appropriate word above should recognize that i'm at a state computer, and lawyerz are everywhere.)

has anyone run into this before? i don't have enough hair left to keep pulling it out like this ...

Replies are listed 'Best First'.
Re: postgresql dbi connection problem
by krazken (Scribe) on Dec 18, 2001 at 22:50 UTC
    I have come across this problem with Postgres as well as Oracle and DB2. DBI is a little qwerky for me, and sometimes I have to explicitly put single quotes around the username and the password in order for it to connect successfully. So you might try
    $username="'user'"; $password="'password'";
    type of thing. hope this helps.
Re: postgresql dbi connection problem
by Zapawork (Scribe) on Dec 19, 2001 at 13:38 UTC
    Hi Ralphie,

    Is there any chance we can see the actual suspect code? I suspect there may be a small diference in the way the dbh is called and possible if it is now be called in a seperate module or subroutine the value may need to be passed in a different fashion.

    Dave -- Saving the world one node at a time

Re: postgresql dbi connection problem
by ralphie (Friar) on Dec 20, 2001 at 01:35 UTC
    thanks to you both for your comments ... i've only been able to address this a little since my original post. krazken seems close to the mark, although single-quoting the value held in the scalar results in the same lack of recognition, other quoting schemes do result in strings getting passed to the statement, although the strings themselves don't quite correspond to the username and as a result authentication fails. i'll be playing with this more.

    for zapawork, here's the beginning of of the script from which the problem arises...

    #!/usr/bin/perl -w sub BEGIN { $ENV{DISPLAY}= "machine:0.0"; } use Carp () ; use CGI qw( ::standard );; use CGI::Carp qw( fatalsToBrowser ); use DBI; use DBD::Pg; our $counter = 0; our $full=new CGI; print $full->header; our $dbh; $path_info = $full->path_info; my ($param1,$param2); $param1='username'; $param2='password'; my $username; $username=$full->param($param1); my $password; $password=$full->param($param2); my $database='jrado'; my $driver='Pg'; my $host='raider-ralph'; my $dsn="DBI:$driver:dbname=$database;host=$host"; $username=~s/^\s+//; $password=~s/^\s+//; unlink '/home/www/dbitrace.log6a' if -e '/home/www/dbitrace.log6a'; DBI->trace(2,'/home/www/dbitrace.log6a'); $dbh=DBI->connect($dsn,$username,$password) or die print "error connec +ting to database ", $DBI::errstr, "\n";

    the initial portion of the working script is as follows:

    #! /usr/bin/perl -Tw ##use strict; use CGI qw(:all); ##list.cgi ##set up the postgresql interface use DBI; use DBD::Pg; my $match; my $begdate; my $enddate; our $username; our $password; $match=''; $begdate=''; $enddate=''; our $list=new CGI; $begdate=$list->param('begdate'); $enddate=$list->param('enddate'); $match=$list->param('match'); $username=$list->param('username'); $password=$list->param('password'); ##declare scalars ##dsn-related my $host; my $db; my $driver; my $dsn; my $database; our $key; $database='jrado'; $driver='Pg'; $user=''; $host='raider-ralph'; $dsn="DBI:$driver:dbname=$database;host=$host"; ##database handle my $dbh_list; unlink '/home/www/dbitrace.log4' if -e '/home/www/dbitrace.log4'; DBI->trace(2,'/home/www/dbitrace.log4'); $username=~s/^\s+//; $password=~s/^\s+//; $dbh_list=DBI->connect($dsn,$username,$password) or die print "ACCESS +DENIED";

    as you can see, there's nothing fancy in either one. in about an hour, i'll be out of here for the next week and a half and i'll probably only intermittently be able to cloister, but i appreciate any input you have.

    ralphie