I am having trouble creating a connection to a postgres database via ssh tunneling with the "github.com/jackc/pgx/v4/pgxpool" library. I cannot seem to figure out how to set the DialFunc for the *pgxpool.Config that I would like to use to establish a connection. This is what I'm trying to do:
sshConnStr := mkPostgresXViaSSHConnStr(config, tz)
config, err := parseSshConnStr(sshConnStr)
// var config *pgsshConfig
sshcon, err := ssh.Dial("tcp", fmt.Sprintf("%s:%d", config.tunnelHost, config.tunnelConfig), &config.tunnelConfig)
// var sshcon *ssh.Client
cfg, err = pgxpool.ParseConfig(config.pgConnStr)
// var cfg *pgxpool.Config
cfg.ConnConfig.DialFunc = func(network, addr string) (net.Conn, error) {
return sshcon.Dial(network, addr)
}
When I try to set the DialFunc I get the following error: cannot use (func(network, addr string) (net.Conn, error) literal) (value of type func(network string, addr string) (net.Conn, error)) as pgconn.DialFunc value in assignment
Any suggestions about how to get this to work?
Add context and cast to pgconn.DialFunc
:
package main
import (
"context"
"net"
"github.com/jackc/pgconn"
"github.com/jackc/pgx/v4/pgxpool"
)
func main() {
cfg, _ := pgxpool.ParseConfig("")
myDial := func(ctx context.Context, network, addr string) (net.Conn, error) {
return nil, nil
}
cfg.ConnConfig.DialFunc = pgconn.DialFunc(myDial)
}