• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

【Centos】Postgresql连接测试(Perl和Ruby)

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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 = "5432";

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;

1;
#修改执行权限
chmod 777 testPerl.pl

#执行测试
 ./testPerl.pl
2018-08-31 18:09:28.357691+09

成功的场合就会显示时间数据。

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.4.gem

Building native extensions.  This could take a while...
Successfully installed pg-0.18.4
Parsing documentation for pg-0.18.4
Installing ri documentation for pg-0.18.4
Done installing documentation for pg after 2 seconds
WARNING:  Unable to pull data from 'https://rubygems.org/': Errno::ENETUNREACH: Network is unreachable - connect(2) for "api.rubygems.org" port 443 (https://api.rubygems.org/specs.4.8.gz)
1 gem installed

2,连接测试

vi test.rb    
    
require 'pg'    
    
connect = PG::connect(host:"198.192.69.1",user:"test",password:"Admin",dbname:"testDB",port:"5432")    
results = connect.exec("select current_date hoge")    
    
results.each{|result|    
   p result["hoge"]    
}    
    
connect.finish    
#修改执行权限
chmod 777 test.rb                                                            
        
#执行测试                                                    
/opt/td-agent/embedded/bin/ruby test.rb                                                            
2018/8/31                                                            

成功的场合就会显示时间数据。

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Perl语言入门-第七章-漫游正则表达式王国-习题发布时间:2022-07-22
下一篇:
perl变量发布时间:2022-07-22
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap