PostgreSQL的客戶端接口
在PostgreSQL發(fā)行版中只包含兩個客戶端接口: libpq 和 ECPG
- libpq is included because it is the primary C language interface, and because many other client interfaces are built on top of it.
- ECPG is included because it depends on the server-side SQL grammar, and is therefore sensitive to changes in PostgreSQL itself.
其他語言客戶端接口:
| Name | Language | Comments | Website |
|---|---|---|---|
| DBD::Pg | Perl | Perl DBI driver | https://metacpan.org/release/DBD-Pg |
| JDBC | Java | Type 4 JDBC driver | https://jdbc.postgresql.org/ |
| libpqxx | C++ | C++ interface | https://pqxx.org/ |
| node-postgres | JavaScript | Node.js driver | https://node-postgres.com/ |
| Npgsql | .NET | .NET data provider | https://www.npgsql.org/ |
| pgtcl | Tcl | - | https://github.com/flightaware/Pgtcl |
| pgtclng | Tcl | - | https://sourceforge.net/projects/pgtclng/ |
| pq | Go | Pure Go driver for Go’s database/sql | https://github.com/lib/pq |
| psqlODBC | ODBC | ODBC driver | https://odbc.postgresql.org/ |
| psycopg | Python | DB API 2.0-compliant | https://www.psycopg.org/ |
概念
-
什么是 Perl?
我也不懂,參考后面鏈接,感覺說的挺詳細:https://www.runoob.com/perl/perl-intro.html -
DBI 模塊
DBI 英文全稱:Database Independent Interface,中文稱為數(shù)據(jù)庫獨立接口,是 Perl 編程語言的數(shù)據(jù)庫訪問模塊。
DBI 作為 Perl 語言中和數(shù)據(jù)庫進行通訊的標準接口,它定義了一系列的方法,變量和常量,提供一個和具體數(shù)據(jù)庫平臺無關的數(shù)據(jù)庫接口。
DBI 和具體數(shù)據(jù)庫平臺無關,我們可以將其應用在PostgreSQL, Oracle, MySQL, DB2 或 Informix 等數(shù)據(jù)庫中。

-
Perl連接數(shù)據(jù)庫一個應用場景
如果研究過Oracle到PostgreSQL數(shù)據(jù)庫的遷移,應該會了解一個遷移工具Ora2pg,Ora2pg就是使用perl語言編寫的,使用Ora2pg進行遷移的過程中,就會用到 DBD::Oracle 連接Oracle數(shù)據(jù)庫,DBD::Pg 連接PostgreSQL數(shù)據(jù)庫。
環(huán)境準備
- perl
一般LInux系統(tǒng)默認都會安裝perl,我的實驗環(huán)境的CentOS 7,默認安裝的perl是v5.16.3版本(命令:perl -v),如果沒有安裝,可以使用命令yum install perl -y進行安裝,或者去官方網(wǎng)站(https://www.perl.org/get.html)下載源碼安裝。
官網(wǎng)提示:目前版本是5.34.0,如果Perl的版本早于5.8.3,可能無法運行最新版本的 CPAN 模塊。
Perl-5.34.0源碼包下載地址:https://www.cpan.org/src/5.0/perl-5.34.0.tar.gz
# 源碼編譯安裝
[root@pgtest3 ~]# cd /enmo/soft/
[root@pgtest3 soft]# tar -zxvf perl-5.34.0.tar.gz
[root@pgtest3 soft]# cd perl-5.34.0
[root@pgtest3 perl-5.34.0]# mkdir /enmo/app/perl-5.34.0
[root@pgtest3 perl-5.34.0]# ln -s /enmo/app/perl-5.34.0 /enmo/app/perl
[root@pgtest3 perl-5.34.0]# ./Configure -des -Dprefix=/enmo/app/perl
[root@pgtest3 perl-5.34.0]# make
[root@pgtest3 perl-5.34.0]# make test # 這個過程挺漫長(Elapsed: 714 sec)
[root@pgtest3 perl-5.34.0]# make install
# 配置環(huán)境變量
[root@pgtest3 ~]# sed -i "s;:\$PATH:;:/enmo/app/perl/bin:\$PATH:;g" /etc/profile
[root@pgtest3 ~]# source /etc/profile
# 查看版本
[root@pgtest3 ~]# perl -v
This is perl 5, version 34, subversion 0 (v5.34.0) built for x86_64-linux
Copyright 1987-2021, Larry Wall
- DBD::Pg
Linux系統(tǒng)中也自帶了DBD::Pg(版本:2.19.3),但是默認不會安裝,使用命令yum install perl-DBD-Pg -y進行安裝,或者去Perl的模塊倉庫網(wǎng)站(https://metacpan.org/)下載源碼(當前最新:DBD-Pg-3.15.0)安裝,但是要打開這個網(wǎng)站得需要點運氣,如果像我一樣運氣不好的話,可以點這個進去找找https://cpan.metacpan.org/authors/id/T/TU/TURNSTEP/。
DBI源碼包下載地址:
https://cpan.metacpan.org/authors/id/T/TI/TIMB/DBI-1.643.tar.gz
DBD-Pg-3.15.0 源碼包下載地址:
https://cpan.metacpan.org/authors/id/T/TU/TURNSTEP/DBD-Pg-3.15.0.tar.gz
# 安裝 DBD::Pg 之前需要先安裝 DBI
[root@pgtest3 soft]# tar -zxvf DBI-1.643.tar.gz
[root@pgtest3 soft]# cd DBI-1.643
[root@pgtest3 DBI-1.643]# perl Makefile.PL
[root@pgtest3 DBI-1.643]# make
[root@pgtest3 DBI-1.643]# make test
[root@pgtest3 DBI-1.643]# make install
# 安裝 DBD::Pg
[root@pgtest3 ~]# cd /enmo/soft/
[root@pgtest3 soft]# tar -zxvf DBD-Pg-3.15.0.tar.gz
[root@pgtest3 soft]# cd DBD-Pg-3.15.0
[root@pgtest3 soft]# perl Makefile.PL
[root@pgtest3 soft]# make && make install
# 查看已安裝的模塊
[root@pgtest3 ~]# vi check.pl
#!/usr/bin/perl
use strict;
use ExtUtils::Installed;
my $inst=ExtUtils::Installed->new();
my @modules = $inst->modules();
foreach(@modules){
my $ver = $inst->version($_) || "???";
printf("%-12s -- %s\n",$_,$ver);
}
exit;
[root@pgtest3 ~]# perl check.pl
DBD::Pg -- 3.15.0
DBI -- 1.643
Perl -- 5.34.0
- PostgreSQL
參考文檔: PostgreSQL高可用測試系列之Patroni + etcd + HAProxy + Keepalived 離線部署(二)
Perl連接PostgreSQL
# 編輯腳本
[root@pgtest3 ~]# vi perl_conn_pg.pl
#! /usr/bin/perl
use strict;
use Data::Dumper; # 用于select返回數(shù)據(jù)集
use DBI;
# 連接數(shù)據(jù)庫,AutoCommit(default:1)表示是否自動提交,如果設置為0,不會自動提交,通過 $dbh->commit 來提交,或 $dbh->rollback 來回滾
# $dbh = DBI->connect("dbi:Pg:dbname=$dbname;host=$host;port=$port;options=$options", $username, $password, {AutoCommit => 0, RaiseError => 1, PrintError => 0});
my $dbh=DBI->connect("DBI:Pg:dbname=postgres;host=192.168.58.10;port=5432","postgres","postgres",{ AutoCommit => 0, RaiseError => 1 }) or die "can''t connect!";
# 查詢
my $sth = $dbh->prepare("select inet_server_addr(),pg_is_in_recovery(),current_database(),current_user" )or die "Syntax error:$!\n";
# 執(zhí)行
$sth->execute();
# 獲取查詢結果集
while(my $row=$sth->fetchrow_hashref())
{
print Dumper($row);
}
# 關閉數(shù)據(jù)庫
$dbh->disconnect();
# 執(zhí)行腳本
[root@pgtest3 ~]# perl perl_conn_pg.pl
$VAR1 = {
'current_database' => 'postgres',
'current_user' => 'postgres',
'pg_is_in_recovery' => 0,
'inet_server_addr' => '192.168.58.10'
};
DBI 和 DBD::Pg 的使用文檔
DBI 的使用文檔: https://metacpan.org/pod/DBI
DBD::Pg 的使用文檔: https://metacpan.org/pod/DBD::Pg
太難了,有需要再研究更新吧。
最后修改時間:2021-11-06 22:24:05
「喜歡這篇文章,您的關注和贊賞是給作者最好的鼓勵」
關注作者
【版權聲明】本文為墨天輪用戶原創(chuàng)內(nèi)容,轉載時必須標注文章的來源(墨天輪),文章鏈接,文章作者等基本信息,否則作者和墨天輪有權追究責任。如果您發(fā)現(xiàn)墨天輪中有涉嫌抄襲或者侵權的內(nèi)容,歡迎發(fā)送郵件至:contact@modb.pro進行舉報,并提供相關證據(jù),一經(jīng)查實,墨天輪將立刻刪除相關內(nèi)容。




