If I get this correctly you're running your CGI against Database #1 and it works fine from both the command line and the browser. Changing the CGI to run against Database #2 runs fine from the command line, but not from the browser. Is this correct? If that's the case, you should make sure that Database #2 is accessible from the user that your CGI is running as... that is, if it works from the command line then Database #2 is cool with you as the user... if it don't work from the browser then it seems clear to me that Database #2 has issues with the "web" user ("nobody" on many machines.)
Just a thought...
Gary Blackburn
Trained Killer