What driver name do I use to connect Go sqlx to Postgres using the pgx driver?

huangapple go评论103阅读模式
英文:

What driver name do I use to connect Go sqlx to Postgres using the pgx driver?

问题

或者,我需要添加哪些额外的导入?

我想开始使用Postgres作为我正在进行的一些开发的主要DBMS,我的研究表明,pgx是目前最受推荐的数据库驱动程序。我已经设置了一个连接包:

package database

import (
	"fmt"
	_ "github.com/jackc/pgx/v5"
	"net/url"

	"github.com/jmoiron/sqlx"
	log "github.com/sirupsen/logrus"
)

func PgConnect(username, password, host, app string) *sqlx.DB {
	s := fmt.Sprintf("postgres://%s:%s@%s?app+name=%s&sslmode=allow", url.QueryEscape(username), url.QueryEscape(password), host, app)
	db, err := sqlx.Connect("postgres", s)
	if err != nil {
		log.Panicf("failed to connect to sqlserver://%s:%s@%s err: %s", username, "xxxxxxxxxxxx", host, err)
	}

	return db
}

URL的计算结果为:

postgres://user:password@database-host:5432/database-name?app+name=app-name&sslmode=allow

我还尝试了postgresql前缀,因为我在互联网上看到过这两种形式。

sqlx.Connect("postgres", s)调用中,我尝试了postgrespostgresqlpgx作为驱动程序名称。

在所有情况下,连接调用都失败,并显示以下错误:

sql: unknown driver "postgres" (forgotten import?)

相同的代码(使用驱动程序mssql和mssql URL)可以连接到Microsoft SQL Server。

有人可以帮助我解决这个问题吗?我在互联网上找到的关于这种语言/驱动程序/sqlx/Postgres组合的信息非常少。

英文:

Or alternatively, what additional imports do I need?

I'd like to start using Postgres as my main DBMS for some development I am doing, and my research suggests that pgx is the database driver of choice at this time. I have setup a connection package:

package database

import (
	"fmt"
	_ "github.com/jackc/pgx/v5"
	"net/url"

	"github.com/jmoiron/sqlx"
	log "github.com/sirupsen/logrus"
)

func PgConnect(username, password, host, app string) *sqlx.DB {
	s := fmt.Sprintf("postgres://%s:%s@%s?app+name=%s&sslmode=allow", url.QueryEscape(username), url.QueryEscape(password), host, app)
	db, err := sqlx.Connect("postgres", s)
	if err != nil {
		log.Panicf("failed to connect to sqlserver://%s:%s@%s err: %s", username, "xxxxxxxxxxxx", host, err)
	}

	return db
}

The URL evaluates to:

postgres://user:password@database-host:5432/database-name?app+name=app-name&sslmode=allow

and I've also tried the prefix of postgresql here because I've seen both in places on the internet.

For the driver name in the sqlx.Connect("postgres", s) call I have tried postgres, postgresql and pgx.

In all cases the call to connect fails with the error:

sql: unknown driver "postgres" (forgotten import?)

The same code (with driver mssql and mssql URL) works connection to Microsoft SQL Server.

Can anyone help me get this going? I've found very little on the internet for this combination of language/driver/sqlx/postgres.

答案1

得分: 3

文档中可以看到,驱动程序的名称是pgx

> 可以通过sql.Open建立一个database/sql连接。
>
> db, err := sql.Open("pgx", "postgres://pgx_md5:secret@localhost:5432/pgx_test?sslmode=disable")
> if err != nil {
> return err
> }

所以你需要做两件事:

  1. 使用正确的驱动程序名称:

        db, err := sqlx.Connect("pgx", s)
    
  2. 导入stdlib兼容性包:

    import (
        _ "github.com/jackc/pgx/v5/stdlib" // pgx的标准库绑定
    )
    
英文:

From the documentation, we can see that the driver's name is pgx:

> A database/sql connection can be established through sql.Open.
>
> db, err := sql.Open("pgx", "postgres://pgx_md5:secret@localhost:5432/pgx_test?sslmode=disable")
> if err != nil {
> return err
> }

So you'll need to do two things:

  1. Use the proper driver name:

        db, err := sqlx.Connect("pgx", s)
    
  2. Import the stdlib compatibility pacakge:

    import (
        _ "github.com/jackc/pgx/v5/stdlib" // Standard library bindings for pgx
    )
    

huangapple
  • 本文由 发表于 2022年11月7日 23:02:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/74348652.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定