I'm trying go connect to my postgres database using the jackc/pgx package and I'm following the provided example. The problem is that the code gets stuck at the connect call. I've tried printing something right after it and it doesn't print.
var dbURL string = fmt.Sprintf("postgresql://%s:%s@%s:%s/%s", user, password, host, port, dbname)
fmt.Println(dbURL)
conn, err := pgx.Connect(context.Background(), dbURL)
if err != nil {
fmt.Fprintf(os.Stderr, "Unable to connect to database: %v\n", err)
os.Exit(1)
}
defer conn.Close(context.Background())
var text string
err = conn.QueryRow(context.Background(), "select text from questions where id=$1", 1).Scan(&text)
if err != nil {
fmt.Fprintf(os.Stderr, "QueryRow failed: %v\n", err)
os.Exit(1)
}
fmt.Println(text)
Output:
postgresql://postgres:[email protected]:52269/database_name
What am I doing wrong? I can connect to the db with pgAdmin 4 and I've connected to it before using the standard sql from go.
connectionString := fmt.Sprintf("user=%s password=%s dbname=%s sslmode=disable", user, password, dbname)
a.DB, err = sql.Open("postgres", connectionString)
postgres://...instead ofpostgresql://...?postgres://and you're seeing the same issue, fair enough.dbURLhas nosslmodesetting, while the allegedly workingconnectionStringdoes, so maybe that's your issue?