Use the pgx library to connect to PostgreSQL by calling pgx.Connect with a connection string containing your host, port, user, password, and database name.
package main
import (
"context"
"log"
"github.com/jackc/pgx/v5"
)
func main() {
conn, err := pgx.Connect(context.Background(), "host=localhost port=5432 user=postgres password=postgres dbname=pgx_test")
if err != nil {
log.Fatal(err)
}
defer conn.Close(context.Background())
}