docker + golang lib/pq "dial tcp 127.0.0.1:5432: connect: connection refused"

10,171

Using the connection string as a URL worked:

    url := fmt.Sprintf("postgres://%v:%v@%v:%v/%v?sslmode=disable",
        pql.conf.Postgres.User,
        pql.conf.Postgres.Password,
        pql.conf.Postgres.Host,
        pql.conf.Postgres.Port,
        pql.conf.Postgres.DB)

See lib/pq docs here: https://godoc.org/github.com/lib/pq

I was stuck on this for more than a day and I owe the fix to Nikolay Sandalov's comment here in GitHub: https://github.com/coreos/clair/issues/134#issuecomment-491300639

Thank you, Nikolay 🙇🏻‍♂️

Share:
10,171
Mikeumus
Author by

Mikeumus

We're the Space Generation. and if ya don't know... http://www.googlelunarxprize.org/ http://www.virgingalactic.com/ http://www.spacex.com/ now ya know... @mikeumus

Updated on June 04, 2022

Comments

  • Mikeumus
    Mikeumus almost 2 years

    sql.Open() wouldn't error:

    if db, err = sql.Open("postgres", url); err != nil {
        return nil, fmt.Errorf("Postgres connect error : (%v)", err)
    }
    

    but db.Ping() would error:

    if err = db.Ping(); err != nil {
        return nil, fmt.Errorf("Postgres ping error : (%v)", err)
    }
    

    and it was simply because the lib/pq connection string wouldn't connect from within a docker container with the seperated connection parameters.

    For example:

    url := fmt.Sprintf("user=%v password=%v host=%v port=%v dbname=%v",
        rs.conf.Redshift.User,
        rs.conf.Redshift.Password,
        rs.conf.Redshift.Host,
        rs.conf.Redshift.Port,
        rs.conf.Redshift.DB)
    
    • Shepmaster
      Shepmaster over 4 years
      FWIW, you don't have to close your question, and we aren't trying to say you are doing it wrong, but I am saying that what you have described isn't an effective debugging technique. Pop into the chat if you want. Please flag this comment as "no longer needed" once you see it to keep this Q&A tidy.
    • Mikeumus
      Mikeumus over 4 years
      @Shepmaster, I'm here now: meta.stackoverflow.com/questions/334761/… my luck right now is 💯
    • Shepmaster
      Shepmaster over 4 years
      weird. It looks like you have some weird vestigial account chat.stackoverflow.com/users/244193/mikeumus
  • mondayguy
    mondayguy over 3 years
    Worked for me, plussed from 2 accounts=)
  • kokemomuke
    kokemomuke over 2 years
    Worked like charm