Centos安装了PostgreSQL之后,将考虑如何让Perl与Ruby连接它。
Perl连接方式
1,安装Perl的数据库连接包
- perl-DBD-Pg
- perl-DBI
yum install perl-DBI.x86_64 perl-DBD-Pg.x86_64
2,连接测试
vi testPerl.pl
#!/usr/bin/perl use DBI; # PostgreSQL
our $DB_NAME = "testDB";
our $DB_USER = "test";
our $DB_PASS = "Admin";
our $DB_HOST = "198.192.69.1";
our $DB_PORT = ""; my $dbh = DBI->connect("dbi:Pg:dbname=$DB_NAME;host=$DB_HOST","$DB_USER","$DB_PASS") or die "$!\n Error: failed to connect to DB.\n";
my $sth = $dbh->prepare("SELECT now();");
$sth->execute();
while (my $ary_ref = $sth->fetchrow_arrayref) {
my ($row) = @$ary_ref;
print $row , "\n";
}
$sth->finish;
$dbh->disconnect; ;
#修改执行权限
chmod testPerl.pl #执行测试
./testPerl.pl
-- ::28.357691+
成功的场合就会显示时间数据。
Ruby连接方式
1,安装Ruby的数据库连接包
pg-0.18.4.gem
下载地址:https://rubygems.org/gems/pg/versions/0.18.4
/usr/sbin/td-agent-gem install pg-0.18..gem Building native extensions. This could take a while...
Successfully installed pg-0.18.
Parsing documentation for pg-0.18.
Installing ri documentation for pg-0.18.
Done installing documentation for pg after seconds
WARNING: Unable to pull data from 'https://rubygems.org/': Errno::ENETUNREACH: Network is unreachable - connect() for "api.rubygems.org" port (https://api.rubygems.org/specs.4.8.gz)
gem installed
2,连接测试
vi test.rb require 'pg' connect = PG::connect(host:"198.192.69.1",user:"test",password:"Admin",dbname:"testDB",port:"")
results = connect.exec("select current_date hoge") results.each{|result|
p result["hoge"]
} connect.finish
#修改执行权限
chmod test.rb #执行测试
/opt/td-agent/embedded/bin/ruby test.rb
//
成功的场合就会显示时间数据。