Gorm: Configuring TLS failed (sslmode invalid)

王林
Release: 2024-02-05 21:33:03
forward
1266 people have browsed it

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)
Copy after login

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>
Copy after login

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!

source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template