The following works reliably:
db, err := sql.Open("postgres", "dbname=my_db")
if err == nil {
err = db.Ping()
}
// err should be nil if my_db exists
However the following will randomly use the value of the PGDATABASE environment variable instead of the given DSN.
db, err := sql.Open("postgres", "database=my_db")
if err == nil {
err = db.Ping()
}
// err should be nil if my_db exists
This seems to be due to lib/pq implicitly relying on predictable map iteration internally, which Go does not provide.