skip to Main Content

Trying to make use of the official postgres docker image with:

docker run --rm -d 
        --name my-postgres 
        --network my-network 
        -e POSTGRES_USER=postgres 
        -e POSTGRES_PASSWORD=mysuperduperlongpwstring 
        -e POSTGRES_DB=postgres 
        -v /path/to/postgres/data/:/var/lib/postgresql/data
        postgres

However, when creating postgres connection with Go,

        psqlInfo := fmt.Sprintf(
                "host=%s port=%s user=%s password=%s dbname=%s sslmode=%s",
                os.Getenv("DB_HOST"),
                os.Getenv("DB_PORT"),
                os.Getenv("DB_USER"),
                os.Getenv("DB_PASSWORD"),
                os.Getenv("DB_NAME"),
                os.Getenv("DB_SSL_MODE"),
        )
        db, err := sql.Open("postgres", psqlInfo)
        if err != nil {
                panic(err)
        }

I always ended up with

panic: pq: password authentication failed for user "postgres"

and I have no any other ways to verify mysuperduperlongpwstring:

  • Trying to make the DB connect from within docker container with psql -U postgres -d postgres --password, and give anything as password would work just fine.
  • Trying to make change to the password from within docker container, will get

ERROR: must be superuser to alter replication roles or change replication attribute

I.e., basically the answers from the following are not working for me any more:

Lacking of any resources, I’ve changed mysuperduperlongpwstring several times when starting docker, and change my Go PW accordingly, but every time I ended up with the same failure.

How to troubleshoot where the problem is please?
How to reset the PASSWORD with the official postgres docker?

2

Answers


  1. it seems you have special characters in your super strong password which leads to this, look at this question, my suggestion is to use url.QueryEscape like this:

    psqlInfo := fmt.SprintF("postgresql://%s:%s@%s:%s/%s?sslmode=%s",
      os.Getenv("DB_USER"),
      url.QueryEscape(os.Getenv("DB_PASSWORD")),
      os.Getenv("DB_HOST"),
      os.Getenv("DB_PORT"),
      os.Getenv("DB_NAME"),
      os.Getenv("DB_SSL_MODE"),
    )
    
    Login or Signup to reply.
  2. In a docker container set up the say you show and starting from an empty volume, ‘postgres’ would be a superuser, and so you would not get the error about it needing to be a superuser.

    The only plausible explanations I see for that part is either you didn’t run psql where you thought you did, or you didn’t start with an empty volume and so your docker run command just started up an existing database and thus your "-e POSTGRES*" configurations were all ignored. So one way or another, you are either not connecting to what you think you are, or it is not configured the way you think it is.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search