Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

556
Visualizações
Using pgx to connect to postgres db in Go

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:password@127.0.0.1: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)
over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

Documentation for pgx says both connection string and URL are allowed

https://pkg.go.dev/github.com/jackc/pgx/v4/pgxpool@v4.11.0#ParseConfig

Example DSN

user=jack password=secret host=pg.example.com port=5432 dbname=mydb sslmode=verify-ca pool_max_conns=10

Example URL

postgres://jack:secret@pg.example.com:5432/mydb?sslmode=verify-ca&pool_max_conns=10

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda