lib / pq

Pure Go Postgres driver for database/sql
https://pkg.go.dev/github.com/lib/pq
MIT License
9.01k stars 910 forks source link

Connecting to postgres with a username that has spaces #923

Closed samriddhi closed 4 years ago

samriddhi commented 4 years ago

I am trying to connect to Postgres but my username has space in between my last and first name. Every time I hit a ping an error with the following error keeps popping up:

2019/12/05 15:05:29 missing "=" after "'samriddhi" in connection info string"

My code is the following: const ( host = "localhost" port = 5432 user = "singh samriddhi" dbname = "db" )

psqlInfo := fmt.Sprintf("host=%s port=%d user=%s dbname=%s sslmode=disable", host, port, user, dbname)

How do I add space within my username? Or is there a workaround for this?

cbandy commented 4 years ago

Try %q instead of %s.

samriddhi commented 4 years ago

@cbandy I tried that it gives the same error with the missing =.

peterstace commented 4 years ago

Check the docs. In particular, you need to use single quotes around parameters with spaces. You could use QuoteLiteral for this, which would also handle the case where a parameter contains apostrophes or other special chars.

samriddhi commented 4 years ago

@peterstace I had to double escape singh\\ samriddhi. Maybe there can be an exampke in the docs about this because I was just trying a single escape. I could not get the QuoteLiteral to work but since that is what the function would have done it's not a big deal

Thank you so much.