Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Postgres DSN with multiple hosts fails #2158

Open
netsensei opened this issue Oct 5, 2023 · 1 comment
Open

Postgres DSN with multiple hosts fails #2158

netsensei opened this issue Oct 5, 2023 · 1 comment

Comments

@netsensei
Copy link

netsensei commented Oct 5, 2023

Problem

I'm trying to connect to a Postgres cluster using the DSN in atlas.hcl. Note how this contains multiple host entries, which is possible according to the libpq documentation

url = postgres://user:pass@host1:5432,host2:5432,host3:5432/db?target_session_attrs=read-write

However, I get this error message when I run e.g. atlas migrate apply --env myenvironment.

Error: postgres: scanning system variables: dial tcp: lookup host1,host2,host3: no such host

Additionally, this will fail too:

url = postgres://user:pass@host1:5432/db?target_session_attrs=read-write

With this error:

Error: postgres: scanning system variables: pq: unrecognized configuration parameter "target_session_attrs"

Whereas target_session_attrs is important to determine the primary server in a cluster which allows / is suitable for read-write operations; rather then accidentally connecting to a replication node.

Additional information

The same string works without a hitch with github.com/jackc/pgx/v5 e.g. the listing below yields <nil> indicating that a successful connection could be made with a DSN containing multiple hosts. We also use pgx in conjunction with ent and that works perfectly fine.

package main

import (
	"database/sql"
	"log"

	_ "github.com/jackc/pgx/v5/stdlib"
)

func main() {
	db, err := sql.Open("pgx", "postgres://user:pass@localhost,localhost:5432/db?target_session_attrs=read-write")
	if err != nil {
		log.Println(err)
	}

	err = db.Ping()
	log.Println(err)
}
@netsensei
Copy link
Author

Taking a little bit of a closer look, it seems like atlas relies on lib/pq however that projects seems to be in maintenance mode.

Moreover, they have an open PR to add multihost support, however it's not getting merged and a key contributor even suggests moving to pgx instead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant