1

使用go语言连接postgresql数据库,pq可能要被pgx取代 -利来国际app

张玉龙 2021-11-10
525

在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
jdbc java type 4 jdbc driver
libpqxx c c interface
node-postgres javascript node.js driver
npgsql .net .net data provider
pgtcl tcl -
pgtclng tcl -
pq go pure go driver for go’s database/sql
psqlodbc odbc odbc driver
psycopg python db api 2.0-compliant

之前研究postgresql高可用 patroni etcd,其中 etcd 就是基于go语言编写的,关于 etcd 和高可用请参考:
postgresql高可用测试系列之patroni etcd haproxy keepalived 离线部署(三)

go 的下载地址:
pq 驱动的文档:
pq 通过 go 命令下载,所以运行 go 的操作系统需要连接互联网

在一个可以连接互联网的 linux 系统安装 go

# 解压即是安装 [[email protected] ~]# tar -zxvf /enmo/soft/go1.17.3.linux-amd64.tar.gz -c /enmo/app/ [[email protected] ~]# mv /enmo/app/go /enmo/app/go-1.17.3 [[email protected] ~]# ln -s /enmo/app/go-1.17.3 /enmo/app/go # 配置环境变量 [[email protected] ~]# vi /etc/profile export goroot=/enmo/app/go # go的软件目录 export gopath=$home/go # 写代码的项目目录 export path=$path:$goroot/bin:$gopath/bin [[email protected] ~]# source /etc/profile # 检查安装 [[email protected] ~]# go version go version go1.17.3 linux/amd64

创建项目的目录结构

[[email protected] ~]# mkdir go [[email protected] ~]# cd go [[email protected] go]# mkdir bin [[email protected] go]# mkdir pkg [[email protected] go]# mkdir src [[email protected] go]# cd src [[email protected] src]# go mod init test

安装pq驱动

# 安装之前需要先设置一下 goproxy ,默认是从 https://proxy.golang.org 下载驱动,但是这个网站被墙了,建议换成 https://proxy.golang.org [[email protected] ~]# go env |grep goproxy goproxy="https://proxy.golang.org,direct" [[email protected] ~]# go env -w goproxy=https://goproxy.cn,direct # 安装pq驱动 [[email protected] ~]# cd go/src/ [[email protected] src]# go get github.com/lib/pq go: downloading github.com/lib/pq v1.10.4

连接postgresql数据库

[[email protected] src]# vi test.go package main import ( "database/sql" "fmt" "log" _ "github.com/lib/pq" ) func main() { connstr := "host=192.168.58.10 port=5432 dbname=postgres user=postgres password=postgres sslmode=disable" db, err := sql.open("postgres", connstr) if err != nil { log.fatal(err) } rows, err := db.query("select inet_server_addr(),pg_is_in_recovery(),current_database(),current_user") if err != nil { log.fatal(err) } for rows.next() { var inet_server_addr string var pg_is_in_recovery string var current_database string var current_user string err = rows.scan(&inet_server_addr, &pg_is_in_recovery, ¤t_database, ¤t_user) if err != nil { log.fatal(err) } fmt.println("inet_server_addr: " inet_server_addr) fmt.println("pg_is_in_recovery: " pg_is_in_recovery) fmt.println("current_database: " current_database) fmt.println("current_user: " current_user) } }
# 编译 [[email protected] src]# go build # 执行 [[email protected] src]# ./test inet_server_addr: 192.168.58.10 pg_is_in_recovery: false current_database: postgres current_user: postgres

将编译后的文件传给其他服务器就可以直接执行

[[email protected] src]# scp test 192.168.58.10:/root/ # 在 192.168.58.10 服务器上直接就能执行 [[email protected] ~]# ./test inet_server_addr: 192.168.58.10 pg_is_in_recovery: false current_database: postgres current_user: postgres

pq 可能要被 pgx 取代

  pq 驱动的文档: 中提到:

this package is currently in maintenance mode, which means: 1. it generally does not accept new features. 2. it does accept bug fixes and version compatability changes provided by the community. 3. maintainers usually do not resolve reported issues. 4. community members are encouraged to help each other with reported issues. for users that require new features or reliable resolution of reported bugs, we recommend using pgx which is under active development.

  pgx 驱动的文档:

# 创建项目目录 [[email protected] ~]# cd go/src/ [[email protected] src]# mkdir test_pgx [[email protected] src]# cd test_pgx/ [[email protected] test_pgx]# go mod init go: creating new go.mod: module test_pgx # 安装 pgx [[email protected] test_pgx]# go get github.com/jackc/pgx/v4 # 脚本 [[email protected] test_pgx]# vi test_pgx.go package main import ( "context" "fmt" "os" "github.com/jackc/pgx/v4" ) func main() { conn, err := pgx.connect(context.background(), os.getenv("database_url")) if err != nil { fmt.fprintf(os.stderr, "unable to connect to database: %v\n", err) os.exit(1) } defer conn.close(context.background()) var greeting string err = conn.queryrow(context.background(), "select 'hello, world!'").scan(&greeting) if err != nil { fmt.fprintf(os.stderr, "queryrow failed: %v\n", err) os.exit(1) } fmt.println(greeting) } # 定义环境变量,编译执行 [[email protected] test_pgx]# export database_url="postgres://postgres:[email protected]:5432/postgres?sslmode=disable" [[email protected] test_pgx]# go build [[email protected] test_pgx]# ./test_pgx hello, world!
最后修改时间:2021-11-11 10:35:56
「喜欢文章,快来给作者赞赏墨值吧」
【利来手机国际的版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:[email protected]进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论