前面几篇博文,我们成功安装了数据库,本文开始我们介绍使用C和C++的开发接口,连接数据库和读取数据的方法。
修改PostgreSQL配置文件
在使用我们写的客户端程序连接数据库之前,我们需要修改服务端配置文件完成如下两次调整:
- 第一,服务端默认监听在 127.0.0.1:5432端口上,其他主机无法通过局域网连接,需要修改 /etc/postgresql/14/main/postgresql.conf 文件,使得服务监听在对外端口上。
- 第二,服务端默认允许本机可以通过网络连接 127.0.0.1:5432 端口,但即使我们按上述方法开放了对外端口,其他外部主机也无法连接上,我们还修改 /etc/postgresql/14/main/pg_hba.conf ,以开通外部连接权限。
针对第一处修改,在 /etc/postgresql/14/main/postgresql.conf 中修改 listen_addresses = 'localhost'
配置项,修改后如下图所示:
针对第二处修改,在 /etc/postgresql/14/main/postgresql.conf 配置文件中增加如下内容:
1 | host all all 0.0.0.0/0 scram-sha-256 |
修改后如下图所示:
上述两处修改之后,使用如下命令重启PostgreSQL服务端:
1 | $ sudo systemctl restart postgresql |
C开发接口
libpq是PostgreSQL官方提供的开发库,在Ubuntu服务器上我们使用如下命令安装:
1 | $ sudo apt install libpq-dev |
安装成功之后,我们看下libpq-dev开发包含了哪些文件,使用如下命令查看:
mancode@manos:/var/lib/dpkg/info$ cat /var/lib/dpkg/info/libpq-dev.list /. /usr /usr/bin /usr/bin/pg_config /usr/include /usr/include/postgresql /usr/include/postgresql/internal /usr/include/postgresql/internal/c.h /usr/include/postgresql/internal/libpq /usr/include/postgresql/internal/libpq/pqcomm.h /usr/include/postgresql/internal/libpq-int.h /usr/include/postgresql/internal/port.h /usr/include/postgresql/internal/postgres_fe.h /usr/include/postgresql/internal/pqexpbuffer.h /usr/include/postgresql/libpq /usr/include/postgresql/libpq/libpq-fs.h /usr/include/postgresql/libpq-events.h /usr/include/postgresql/libpq-fe.h /usr/include/postgresql/pg_config.h /usr/include/postgresql/pg_config_ext.h /usr/include/postgresql/pg_config_manual.h /usr/include/postgresql/pg_config_os.h /usr/include/postgresql/postgres_ext.h /usr/lib /usr/lib/x86_64-linux-gnu /usr/lib/x86_64-linux-gnu/libpq.a /usr/lib/x86_64-linux-gnu/pkgconfig /usr/lib/x86_64-linux-gnu/pkgconfig/libpq.pc /usr/share /usr/share/doc /usr/share/doc/libpq-dev /usr/share/doc/libpq-dev/copyright /usr/share/man /usr/share/man/man1 /usr/share/man/man1/pg_config.1.gz /usr/lib/x86_64-linux-gnu/libpq.so /usr/share/doc/libpq-dev/changelog.Debian.gz mancode@manos:/var/lib/dpkg/info$
可见,该开发库包含了我们需要的头文件、静态库和动态库。
安装成功之后,我们使用如下 测试用例文件 来连接数据库以及从数据表t_map中读取数据。
1 |
|
编译测试用例,使用如下命令:
1 | $ gcc -o pgtest pgtest.c `pkg-config libpq -libs -cflags` |
或者
1 | $ gcc -o pgtest pgtest.c -I/usr/include/postgresql -lpq |
在服务器上运行程序,结果如下图所示:
C++开发接口
libpqxx是 PostgreSQL 的官方C++客户端 API,其源代码在BSD许可下使用,但其并不是Ubuntu的官方维护软件包。我们安装libpqxx可以通过命令或者自行编译安装,本文我们使用apt来安装。
1 | sudo apt install libpqxx-dev |
安装之后,我们使用C++来重写上面的 测试用例文件,如下文所示:
1 |
|
编译测试用例,使用如下命令:
1 | $ g++ -o pgtest pgtest.cpp `pkg-config libpqxx -libs -cflags` |
或者
1 | $ g++ -o pgtest pgtest.cpp -I/usr/include/postgresql -lpqxx |
在服务器上运行程序,结果如下图所示:
参考资料
https://www.postgresql.org/docs/16/libpq.html
https://www.postgresql.org/docs/16/libpq-example.html
https://www.jiyik.com/w/postgresql/postgresql-c-cpp