diff --git a/cmd/version.go b/cmd/version.go index f9ef375..b14cdbd 100644 --- a/cmd/version.go +++ b/cmd/version.go @@ -12,7 +12,7 @@ const ( // ProgramVersion is the version of this program ProgramVersion = "0.6" // ProgramRelease is release number of this program - ProgramRelease = "1" + ProgramRelease = "1-devel" // ProgramIssuesUrl is the public URL for posting issues, bug reports and asking questions ProgramIssuesUrl = "https://github.com/lesovsky/pgcenter/issues" ) diff --git a/lib/utils/postgres.go b/lib/utils/postgres.go index 7852a2b..0b95b51 100644 --- a/lib/utils/postgres.go +++ b/lib/utils/postgres.go @@ -76,53 +76,29 @@ func assembleConnstr(c *Conninfo) string { // PQconnectdb connects to Postgres, asks password if required. func PQconnectdb(c *Conninfo, connstr string) (conn *sql.DB, err error) { - conn, err = sql.Open(dbDriver, connstr) - if err != nil { - return nil, err - } - - if err = PQstatus(conn); err != nil { - // handle libpq errors if found - if pqerr, ok := err.(*pq.Error); ok { + // try connecting in the loop and handle all known errors, give up if can;t handle an error + for { + conn, err = sql.Open(dbDriver, connstr) + if err = PQstatus(conn); err != nil { switch { - // Password required -- ask user and retry connection - case pqerr.Code == errCodeInvalidPassword: + case err == pq.ErrSSLNotSupported: + // By default pq-driver tries to connect with SSL. So if SSL is not enabled on the other side - fix our connection string and try to reconnect + connstr = connstr + " sslmode=disable" + case err.(*pq.Error).Code == errCodeInvalidPassword: fmt.Printf("Password for user %s: ", c.User) bytePassword, err := terminal.ReadPassword(0) if err != nil { return nil, err } connstr = fmt.Sprintf("%s password=%s ", connstr, string(bytePassword)) - conn, err = sql.Open(dbDriver, connstr) - if err != nil { - return nil, err - } - if err = PQstatus(conn); err != nil { - return nil, err - } + fmt.Println() default: - return nil, fmt.Errorf("error occurred during connection establishing: %s", err) + return nil, fmt.Errorf("failed connection establishing: %s", err) } - + } else { return conn, nil } - - // handle other golang 'pq' driver-specific errors (not related to libpq) - switch err { - case pq.ErrSSLNotSupported: - // By default pq-driver tries to connect with SSL. - // So if SSL is not enabled on the other side - fix our connection string and try to reconnect - connstr = connstr + " sslmode=disable" - conn, err = sql.Open(dbDriver, connstr) - if err = PQstatus(conn); err != nil { - return nil, err - } - default: - return nil, fmt.Errorf("error occurred during connection establishing: %s", err) - } } - - return conn, nil } // Fills empty connection settings by normal values