Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Postgres数据库DSN设置TimeZone可能造成的异常情况 #7312

Open
helays opened this issue Dec 11, 2024 · 1 comment
Open

Postgres数据库DSN设置TimeZone可能造成的异常情况 #7312

helays opened this issue Dec 11, 2024 · 1 comment
Assignees
Labels

Comments

@helays
Copy link

helays commented Dec 11, 2024

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
@github-actions github-actions bot added the type:missing reproduction steps missing reproduction steps label Dec 11, 2024
Copy link

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.ioSearch Before Asking

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants