Re: [NOVICE] Connect to postgresql database using Perl

From: Sean Davis <sdavis2(at)mail(dot)nih(dot)gov>
To: dipti shah <shahdipti1980(at)gmail(dot)com>
Cc: Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>, "pgsql-general(at)postgresql(dot)org" <pgsql-general(at)postgresql(dot)org>, pgsql-novice <pgsql-novice(at)postgresql(dot)org>
Subject: Re: [NOVICE] Connect to postgresql database using Perl
Date: 2010-03-31 10:40:00
Message-ID: q2i264855a01003310340qc8d8594enc65f4740edf92116@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general pgsql-novice

On Wed, Mar 31, 2010 at 3:25 AM, dipti shah <shahdipti1980(at)gmail(dot)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

> 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(dot)stehule(at)gmail(dot)com>
> wrote:
>>
>> Hello
>>
>> 2010/3/31 dipti shah <shahdipti1980(at)gmail(dot)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
>> >
>
>

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Harald Fuchs 2010-03-31 11:58:42 Re: plPgSQL + CDIR/INET types ...
Previous Message Guillaume Lelarge 2010-03-31 08:30:00 Re: User action accounting

Browse pgsql-novice by date

  From Date Subject
Next Message Konstantin Kuzvesov 2010-03-31 12:16:30 libpq binary composite
Previous Message Machiel Richards 2010-03-31 09:15:18 Daily checks help