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

The below code is connecting to PostGreSQL.

There are 2 databases but they are not getting printed.

use DBI; $user = "root"; $password = "rinrs123"; $hostnm = "localhost"; $port = "5432"; my $dbh = DBI->connect("dbi:Pg:dbname=postgres;host=$hostnm;port=$port +",$user,$password); my @databases = DBI->data_sources('Pg'); print "@databases";

Replies are listed 'Best First'.
Re: Getting all the databases of a PostGreSQL database server.
by Corion (Patriarch) on Jan 06, 2009 at 12:51 UTC

    Where do you think does the following line get the information to which database server to connect?

    my @databases = DBI->data_sources('Pg');

    The DBI documentation tells you what to do in that case.

    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: Getting all the databases of a PostGreSQL database server.
by kyle (Abbot) on Jan 06, 2009 at 13:10 UTC

    You could query for this info too.

    my $sql = 'SELECT datname FROM pg_database'; my @databases = map { $_->[0] } @{ $dbh->selectall_arrayref( $sql ) };

    That query works with my PostgreSQL 8.3.3, but I haven't tried the Perl code.

      I am using the dbhandle and its working.