Postgres数据库中DSN设置timezone和TimeZone可能造成的异常情况

package main

import (
    "fmt"
    "gorm.io/driver/postgres"
    "gorm.io/gorm"
    "gorm.io/gorm/logger"
    "log"
    "net/url"
)

func main() {
    dsn := url.URL{
        Scheme: "postgres",
        User:   url.UserPassword("postgres", "postgres"),
        Host:   "127.0.0.1:5432",
        Path:   "test",
    }
    query := dsn.Query()
    query.Set("TimeZone", "Asia/Shanghai")
    query.Set("search_path", "public")
    dsn.RawQuery = query.Encode()
    dstStr := dsn.String()
    fmt.Println(dstStr)
    dialector := postgres.New(postgres.Config{
        DSN:                  dstStr,
        PreferSimpleProtocol: true,
    })
    cfg := gorm.Config{
        SkipDefaultTransaction:                   true,
        Logger:                                   logger.Default.LogMode(logger.Silent),
        DisableForeignKeyConstraintWhenMigrating: true,
    }

    db, _ := gorm.Open(dialector, &cfg)

    // 查询当前会话的时区设置
    var timeZone string
    err := db.Raw("SHOW TIMEZONE").Scan(&timeZone).Error
    if err != nil {
        log.Fatal("失败", err)
    }
    fmt.Println("Current session timezone:", timeZone)
}

在 这种情况下,连接数据库之后执行 SHOW TIMEZONE。查询出来当前时区有一定的概率会出现Asia%2FShanghai

PS C:\coder\Go\src\utils go build .\dsn.go;.\dsn.exe
postgres://postgres:postgres@127.0.0.1:5432/test?TimeZone=Asia%2FShanghai&search_path=public
Current session timezone: ASIA%2FSHANGHAI

PS C:\coder\Go\src\utils go build .\dsn.go;.\dsn.exe
postgres://postgres:postgres@127.0.0.1:5432/test?TimeZone=Asia%2FShanghai&search_path=public
Current session timezone: ASIA%2FSHANGHAI

PS C:\coder\Go\src\utils go build .\dsn.go;.\dsn.exe
postgres://postgres:postgres@127.0.0.1:5432/test?TimeZone=Asia%2FShanghai&search_path=public
Current session timezone: ASIA%2FSHANGHAI

PS C:\coder\Go\src\utils go build .\dsn.go;.\dsn.exe
postgres://postgres:postgres@127.0.0.1:5432/datestta_house?TimeZone=Asia%2FShanghai&search_path=public
Current session timezone: Asia/Shanghai

Comment From: github-actions[bot]

The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout https://github.com/go-gorm/playground for details. it will be closed in 30 days if no further activity occurs. if you are asking question, please use the Question template, most likely your question already answered https://github.com/go-gorm/gorm/issues or described in the document https://gorm.io ✨ Search Before Asking