2
use DBI;
my $db_host = 'localhost';
my $db_user = 'postgres';
my $db_pass = '12345';
my $db_name = 'postgres';
my $db = "dbi:pg:dbname=${db_name};host=${db_host}";
$dbh = DBI->connect($db, $db_user, $db_pass,{ RaiseError => 1, AutoCommit => 0 }) || die "Error connecting to the database: $DBI::errstr\n";
my $query = "SELECT * FROM random_table";
$ref = $dbh->selectcol_arrayref($query);
print join("\n", @$ref);

When I execute this file. I get an Error. It Says:

DBD::pg initialisation failed: Can't locate object method "driver" via package "DBD::pg"

Do I need to install some driver to connect to the database..?

1
  • For a start you must use strict and use warnings, and declare $dbh and $ref as you use them Commented Apr 11, 2013 at 23:28

1 Answer 1

13

It's dbi:Pg, not dbi:pg. Case matters.

Sign up to request clarification or add additional context in comments.

1 Comment

And it's PostgreSQL, not postgresql, which is likely to be the source of the mistake. I guess that's why the better programmers get so heated about the difference between perl and Perl: it makes a difference. And the first thing to do if you get a message like Can't locate object method "driver" via package "DBD::pg" is think did I spell this right?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.