暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

使用Perl语言连接PostgreSQL数据库的一知半解

原创 张玉龙 2021-11-06
2582

PostgreSQL的客户端接口

在PostgreSQL发行版中只包含两个客户端接口: 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/

概念

  1. 什么是 Perl?
    我也不懂,参考后面链接,感觉说的挺详细:https://www.runoob.com/perl/perl-intro.html

  2. DBI 模块
    DBI 英文全称:Database Independent Interface,中文称为数据库独立接口,是 Perl 编程语言的数据库访问模块。
    DBI 作为 Perl 语言中和数据库进行通讯的标准接口,它定义了一系列的方法,变量和常量,提供一个和具体数据库平台无关的数据库接口。
    DBI 和具体数据库平台无关,我们可以将其应用在PostgreSQL, Oracle, MySQL, DB2 或 Informix 等数据库中。
    image.png

  3. Perl连接数据库一个应用场景
    如果研究过Oracle到PostgreSQL数据库的迁移,应该会了解一个迁移工具Ora2pg,Ora2pg就是使用perl语言编写的,使用Ora2pg进行迁移的过程中,就会用到 DBD::Oracle 连接Oracle数据库,DBD::Pg 连接PostgreSQL数据库。

环境准备

  1. perl
    一般LInux系统默认都会安装perl,我的实验环境的CentOS 7,默认安装的perl是v5.16.3版本(命令:perl -v),如果没有安装,可以使用命令yum install perl -y进行安装,或者去官方网站(https://www.perl.org/get.html)下载源码安装。
    官网提示:目前版本是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 # 配置环境变量 [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
  1. DBD::Pg
    Linux系统中也自带了DBD::Pg(版本:2.19.3),但是默认不会安装,使用命令yum install perl-DBD-Pg -y进行安装,或者去Perl的模块仓库网站(https://metacpan.org/)下载源码(当前最新:DBD-Pg-3.15.0)安装,但是要打开这个网站得需要点运气,如果像我一样运气不好的话,可以点这个进去找找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
  1. PostgreSQL
    参考文档: PostgreSQL高可用测试系列之Patroni + etcd + HAProxy + Keepalived 离线部署(二)

Perl连接PostgreSQL

# 编辑脚本 [root@pgtest3 ~]# vi perl_conn_pg.pl #! /usr/bin/perl use strict; use Data::Dumper; # 用于select返回数据集 use DBI; # 连接数据库,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"; # 执行 $sth->execute(); # 获取查询结果集 while(my $row=$sth->fetchrow_hashref()) { print Dumper($row); } # 关闭数据库 $dbh->disconnect(); # 执行脚本 [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
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论