Connect to postgresql database using Perl

Started by dipti shahabout 16 years ago6 messagesgeneral
Jump to latest
#1dipti shah
shahdipti1980@gmail.com

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.

Thanks,
Dipti

#2Pavel Stehule
pavel.stehule@gmail.com
In reply to: dipti shah (#1)
Re: Connect to postgresql database using Perl

Hello

2010/3/31 dipti shah <shahdipti1980@gmail.com>:

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

Show quoted text

Thanks,
Dipti

#3dipti shah
shahdipti1980@gmail.com
In reply to: Pavel Stehule (#2)
Re: Connect to postgresql database using Perl

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

On Wed, Mar 31, 2010 at 11:53 AM, Pavel Stehule <pavel.stehule@gmail.com>wrote:

Show quoted text

Hello

2010/3/31 dipti shah <shahdipti1980@gmail.com>:

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

#4John R Pierce
pierce@hogranch.com
In reply to: dipti shah (#3)
Re: Connect to postgresql database using Perl

dipti shah wrote:

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");
...

see http://search.cpan.org/~turnstep/DBD-Pg-2.16.1/Pg.pm#connect
<http://search.cpan.org/%7Eturnstep/DBD-Pg-2.16.1/Pg.pm#connect&gt;

add ;host=hostname-or-ip to the connect string...

#5dipti shah
shahdipti1980@gmail.com
In reply to: John R Pierce (#4)
Re: Connect to postgresql database using Perl

That was cool ! Thanks a ton. Got my things done.

On Wed, Mar 31, 2010 at 1:13 PM, John R Pierce <pierce@hogranch.com> wrote:

Show quoted text

dipti shah wrote:

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");
...

see http://search.cpan.org/~turnstep/DBD-Pg-2.16.1/Pg.pm#connect&lt;http://search.cpan.org/%7Eturnstep/DBD-Pg-2.16.1/Pg.pm#connect&gt;&lt;
http://search.cpan.org/%7Eturnstep/DBD-Pg-2.16.1/Pg.pm#connect&gt;

add ;host=hostname-or-ip to the connect string...

#6Sean Davis
sdavis2@mail.nih.gov
In reply to: dipti shah (#3)
Re: [NOVICE] Connect to postgresql database using Perl

On Wed, Mar 31, 2010 at 3:25 AM, dipti shah <shahdipti1980@gmail.com> wrote:

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

Show quoted text

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

On Wed, Mar 31, 2010 at 11:53 AM, Pavel Stehule <pavel.stehule@gmail.com>
wrote:

Hello

2010/3/31 dipti shah <shahdipti1980@gmail.com>:

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