1

Использую lib/pq и jmoiron/sqlx.

package repository

import (
    "github.com/jmoiron/sqlx"
    "fmt"
)

type Config struct {
    Host string
    Port string
    Username string
    Password string
    DBName string
    SSLMode string
}

func NewPostgresDB(cfg Config) (*sqlx.DB, error) {
    fmt.Println(fmt.Sprintf("host=%s port=%s user=%s dbname=%s password=%s sslmode=%s", cfg.Host, cfg.Port, cfg.Username, cfg.DBName, cfg.Password, cfg.SSLMode))
    db, err := sqlx.Open("postgres", fmt.Sprintf("host=%s post=%s user=%s dbname=%s password=%s sslmode=%s", cfg.Host, cfg.Port, cfg.Username, cfg.DBName, cfg.Password, cfg.SSLMode))
    if err != nil {
        return nil, err
    }

    err = db.Ping()
    if err != nil {
        return nil, err
    }

    return db, nil
}

go run выдает следующую ошибку:

host=localhost port=5432 user=postgres dbname=test1 password= sslmode=disable 2022/01/26 15:02:47 failed to initialize db: %spq: SSL is not enabled on the server exit status 1

То есть sslmode у нас в строке подключения выключен, но ошибка выходите следующая, что SSL для postgres не включен. Как решить данную проблему?

1 ответ 1

1

Проблема была решена за счет добавления одинарных ковычек в строку:

db, err := sqlx.Connect("postgres", fmt.Sprintf("host=%s port=%s user=%s dbname=%s password='%s' sslmode=%s", cfg.Host, cfg.Port, cfg.Username, cfg.DBName, cfg.Password, cfg.SSLMode))

Поле password= было нечитаемым видимо. Поэтому добавление кавычек password='' решило проблему.

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.