Home  >  Article  >  Backend Development  >  Gorm: Configuring TLS failed (sslmode invalid)

Gorm: Configuring TLS failed (sslmode invalid)

王林
王林forward
2024-02-05 21:33:031193browse

Gorm:配置 TLS 失败(sslmode 无效)

Question content

I'm trying to learn how to use gorm, but I'm facing a small, silly problem. When I try to connect to the database, the program throws me the following error:

[error] failed to initialize database, got error cannot parse `host=localhost, user=postgres, password=xxxxx dbname=astra, port=5432, sslmode=disable, TimeZone=UTC`: failed to configure TLS (sslmode is invalid)

Honestly, I don't know how to fix it and what could go wrong.

This is my code:

<code>func setupDb() {
    dsn := "host=localhost, user=postgres, password=postgres, dbname=astra, port=5432, sslmode=disable, TimeZone=UTC"
    db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{})
    if err != nil {
        logger_config.ErrorLogger.Println("Couldn't connect to database")
    }
}
</code>

So where is the problem? If you know, please tell me. I am truely thankful!


Correct answer


The connection string should be host=localhost user=postgres password=postgres dbname=astra port=5432 sslmode=disable TimeZone=UTC

Demonstrated here in the gorm documentation

This is actually one of the formats PostgreSQL accepts for connection strings .

The above is the detailed content of Gorm: Configuring TLS failed (sslmode invalid). For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:stackoverflow.com. If there is any infringement, please contact admin@php.cn delete