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/ |
C++语言连接PostgreSQL数据库
libpqxx 文档参考:https://libpqxx.readthedocs.io/en/latest/index.html
libpqxx 当前最新版本:7.7.0 下载地址:国内 https://gitee.com/mirrors/libpqxx/tree/master 国外 https://github.com/jtv/libpqxx
需要注意,使用libpqxx 7.x版本的需要C++ 17,本次测试还是使用旧版本的libpqxx(4.0.1)。
下面是一段C++语言连接PostgreSQL并做查询的一段代码
[root@pgtest3 ~]# vi test.cpp
#include <iostream>
#include <pqxx/pqxx>
using namespace std;
using namespace pqxx;
int main(int argc, char* argv[])
{
const char* sql;
try{
connection conn("dbname=postgres user=postgres password=postgres hostaddr=192.168.58.10 port=5432");
nontransaction ntx(conn);
sql = "select inet_server_addr(),pg_is_in_recovery(),current_database(),current_user";
result r(ntx.exec(sql));
for(result::const_iterator c=r.begin(); c!=r.end(); ++c){
cout<<"inet_server_addr: "<<c[0].as<string>()<<endl;
cout<<"pg_is_in_recovery: "<<c[1].as<string>()<<endl;
cout<<"current_database: "<<c[2].as<string>()<<endl;
cout<<"current_user: "<<c[3].as<string>()<<endl;
}
conn.disconnect ();
}catch (const std::exception &e){
cerr << e.what() << std::endl;
return 1;
}
return 0;
}
复制
如果不安装libpqxx,编译会报出以下错误:
[root@pgtest3 ~]# g++ test.cpp -lpqxx -lpq
test.cpp:2:22: fatal error: pqxx/pqxx: No such file or directory
#include <pqxx/pqxx>
^
compilation terminated.
复制
编译安装最新版的libpqxx(当前最新版本:7.7.0),也会提示需要C++ 17,当前使用的是CentOS 7自带的C++,还得升级太麻烦,退而求其次还是使用老版本吧。
老版本libpqxx-4.0.1下载地址:
http://pqxx.org/download/software/libpqxx/libpqxx-4.0.1.tar.gz
编译安装libpqxx-4.0.1时遇到以下报错:
[root@pgtest3 ~]# cd /enmo/soft/libpqxx-4.0.1
[root@pgtest3 libpqxx-4.0.1]# ./configure
config.status: executing configitems commands
Traceback (most recent call last):
File "./tools/splitconfig", line 154, in <module>
generate_config(original_header, items_map, publication, factor)
File "./tools/splitconfig", line 118, in generate_config
% (publication, factor))
TypeError: a bytes-like object is required, not 'str'
复制
百度搜了一下,全是跟Python报这个错相关的,当前系统安装了Python3,转念一想这么老的libpqxx版本,应该不会用到Python3,改会Python2编译成功:
[root@pgtest3 libpqxx-4.0.1]# rm -f /usr/bin/python
[root@pgtest3 libpqxx-4.0.1]# ln -s /usr/bin/python2 /usr/bin/python
[root@pgtest3 libpqxx-4.0.1]# ./configure
[root@pgtest3 libpqxx-4.0.1]# make
[root@pgtest3 libpqxx-4.0.1]# make install
复制
试着再次编译一下代码,又报错了
[root@pgtest3 ~]# g++ test.cpp -lpqxx -lpq
/usr/bin/ld: cannot find -lpq
collect2: error: ld returned 1 exit status
复制
找不到libpq库,那就安装一下 postgresql-devel 再次编译执行成功,返回查询结果
[root@pgtest3 ~]# yum install -y postgresql-devel
[root@pgtest3 ~]# g++ test.cpp -lpqxx -lpq
[root@pgtest3 ~]# ./a.out
inet_server_addr: 192.168.58.10
pg_is_in_recovery: f
current_database: postgres
current_user: postgres
复制
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。
评论
您好,您的文章已入选合格奖,10墨值奖励已经到账请查收!
❤️我们还会实时派发您的流量收益。
3年前

评论
相关阅读
外国CTO也感兴趣的开源数据库项目——openHalo
小满未满、
1710次阅读
2025-04-21 16:58:09
3月“墨力原创作者计划”获奖名单公布
墨天轮编辑部
373次阅读
2025-04-15 14:48:05
中国PostgreSQL培训认证体系新增PGAI应用工程师方向
开源软件联盟PostgreSQL分会
185次阅读
2025-05-06 10:21:13
转发有奖 | PostgreSQL 16 PGCM高级认证课程直播班招生中!
墨天轮小教习
185次阅读
2025-04-14 15:58:34
PG生态赢得资本市场青睐:Databricks收购Neon,Supabase融资两亿美元,微软财报点名PG
老冯云数
146次阅读
2025-05-07 10:06:22
SQL 优化之 OR 子句改写
xiongcc
136次阅读
2025-04-21 00:08:06
告别老旧mysql_fdw,升级正当时
NickYoung
122次阅读
2025-04-29 11:15:18
华象新闻 | PostgreSQL 18 Beta 1、17.5、16.9、15.13、14.18、13.21 发布
严少安
109次阅读
2025-05-09 11:34:10
PostgreSQL中文社区亮相于第八届数字中国峰会
PostgreSQL中文社区
106次阅读
2025-05-07 10:06:20
PostgreSQL的dblink扩展模块使用方法
szrsu
104次阅读
2025-04-24 17:39:30
TA的专栏
目录