Discussion:
[NOVICE] [GENERAL] Connect to postgresql database using Perl
(too old to reply)
dipti shah
2010-03-31 07:25:33 UTC
Permalink
Thanks Guys. DBI works fine. I have written below code but it executes only
on the server where I installed postgresql. Is there any way to run this
code from remote host. I get an error when trying to run it from remote
host. I think it is obvious because in below code there is no information
where to connect to. Could you please help me out.

use DBI;
$DB_name = 'mydb';
$DB_user = 'postgres';
$DB_pwd = '';
$dbh = DBI->connect("dbi:Pg:dbname=$DB_name","$DB_user","$DB_pwd");
if ( !defined $dbh ) { die "Cannot connect to database!\n"; }
$sth = $dbh->prepare("SELECT * FROM mytable");
$sth->execute();
while ( ($id,$name) = $sth->fetchrow_array() ) { print "$id\t\t $name \n";
}
$sth->finish();
$dbh->disconnect();

*remote-host# perl pg-connect.pl
DBI connect('dbname=sysdb','postgres',...) failed: could not connect to
server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/tmp/.s.PGSQL.5432"? at
pg-connect.pl line 7
Cannot connect to database!
*
Thanks,
Dipti
Hello
Hi,
Could anyone please provide me an example to connect to a postgresql
database using Perl language and accessing the tables, schemas, and other
postgresql objects.
http://www.felixgers.de/teaching/perl/perl_DBI.html
http://structbio.vanderbilt.edu/chazin/wisdom/dbi_howto.html
Regards
Pavel Stehule
Thanks,
Dipti
Sean Davis
2010-03-31 10:40:00 UTC
Permalink
Post by dipti shah
Thanks Guys. DBI works fine. I have written below code but it executes only
on the server where I installed postgresql. Is there any way to run this
code from remote host. I get an error when trying to run it from remote
host. I think it is obvious because in below code there is no information
where to connect to. Could you please help me out.
Hi, Dipti.

Have a look at the DBI documentation. As you suspect, you will need
to specify the host.

Sean
Post by dipti shah
use DBI;
$DB_name    = 'mydb';
$DB_user    = 'postgres';
$DB_pwd     = '';
$dbh = DBI->connect("dbi:Pg:dbname=$DB_name","$DB_user","$DB_pwd");
if ( !defined $dbh ) { die "Cannot connect to database!\n"; }
$sth  = $dbh->prepare("SELECT * FROM mytable");
$sth->execute();
while ( ($id,$name) = $sth->fetchrow_array() ) {  print "$id\t\t $name \n";
}
$sth->finish();
$dbh->disconnect();
remote-host# perl pg-connect.pl
DBI connect('dbname=sysdb','postgres',...) failed: could not connect to
server: No such file or directory
        Is the server running locally and accepting
        connections on Unix domain socket "/tmp/.s.PGSQL.5432"? at
pg-connect.pl line 7
Cannot connect to database!
Thanks,
Dipti
Hello
Hi,
Could anyone please provide me an example to connect to a postgresql
database using Perl language and accessing the tables, schemas, and
other
postgresql objects.
http://www.felixgers.de/teaching/perl/perl_DBI.html
http://structbio.vanderbilt.edu/chazin/wisdom/dbi_howto.html
Regards
Pavel Stehule
Thanks,
Dipti
--
Sent via pgsql-novice mailing list (pgsql-***@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-novice
Continue reading on narkive:
Loading...