I am trying to open a connection to a postgres database using pgx and I am getting the following error:
./dbservice.go:12:26: too many arguments in call to "github.com/jackc/pgx".Connect
have (context.Context, string)
want ("github.com/jackc/pgx".ConnConfig)
./dbservice.go:13:18: too many arguments in call to conn.Close
have (context.Context)
want ()
./dbservice.go:21:44: cannot use context.Background() (type context.Context) as type string in argument to conn.Query
I am not sure what the error is asking me to do here. pgx.Connect
works when I call it from the main file but here it doesn't work. Here's the code:
func initNodes(nodes *[]Node, searchNodes *[]SearchNode, storageNodes *[]StorageNode) error {
conn, err := pgx.Connect(context.Background(), DATABATE_URL)
defer conn.Close(context.Background())
if err != nil {
fmt.Printf("Connection failed: %v\n", err)
os.Exit(-1)
}
...
func main() {
a:= Arbiter{}
a.init()
}
Any ideas?
Most likely you are importing the v3
pgx
API in dbservice.go
, but the v4
API in your “main file”. The Connect
function in github.com/jackc/pgx/v4
accepts the two arguments you're passing. The Connect
function in v3
accepts a pkg.ConnConfig
instead.
So, check your import
statements in dbservice.go
: if you intend to be using the v4
API, import it as "github.com/jackc/pgx/v4"
.