如何使用perl连接到postgresql

时间:2013-04-11 21:47:12

标签: perl postgresql

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

当我执行此文件时。我收到一个错误。它说:

  

DBD :: pg初始化失败:无法通过包“DBD :: pg”找到对象方法“driver”

我是否需要安装一些驱动程序来连接数据库..?

1 个答案:

答案 0 :(得分:12)

它是dbi:Pg,而不是dbi:pg。案件很重要。