Open oomxp598 opened 1 year ago
@oomxp598 与gorm 无关,现在你有两个选择:
@mrcuix 感谢回复,应该是我没有描述清楚,是这样的,我有两个项目,连的同一个数据库(centos7裸机部署),数据库的时区为Asia/Shanghai,两个项目连接数据库的配置完全一样,唯一不同的是,引用的依赖包gorm.io/driver/postgres版本不一样(其中一个项目创建的稍晚,引用的依赖包版本较新),引用旧版本v1.3.1的项目是正常的,引用新版本v1.5.0的项目出现了时区问题,当我将版本回退至v1.3.1时,两个项目的时区都正常了
Hi @jinzhu, I think I'm seeing the same issue, It worked fine with:
gorm.io/driver/postgres v1.2.3
gorm.io/gorm v1.22.5
And no longer worked with
gorm.io/driver/postgres v1.3.10
gorm.io/gorm v1.23.7
The value I'm getting from
err = processor.dbc.Raw("show TIMEZONE").Scan(&result).Error
Is the correct value I set (UTC
), however when I save UTC values and load them again they become time.Local
instead of time.UTC
@moolitayer same problem from me.
But if you use .UTC()
again it was correct for me.
I used the following DSN:
dsn := fmt.Sprintf("host=%s port=%s user=%s dbname=%s password=%s TimeZone=UTC", host, port, user, dbName, pw)
@mrcuix 感谢回复,应该是我没有描述清楚,是这样的,我有两个项目,连的同一个数据库(centos7裸机部署),数据库的时区为Asia/Shanghai,两个项目连接数据库的配置完全一样,唯一不同的是,引用的依赖包gorm.io/driver/postgres版本不一样(其中一个项目创建的稍晚,引用的依赖包版本较新),引用旧版本v1.3.1的项目是正常的,引用新版本v1.5.0的项目出现了时区问题,当我将版本回退至v1.3.1时,两个项目的时区都正常了
@oomxp598 我尝试了几个版本,发现从1.4.6开始,pgx的版本从v4升到了v5,之后设置时区就都没用了,1.4.5及以下的版本都没问题
看了下它初始化连接的方式,如下图:
如果在配置中指定了 DriverName,就可以用驱动本身的方式去初始化连接,所以可以用以下方式去解决这个问题:
db, err := gorm.Open(postgres.New(postgres.Config{
DriverName: "pgx/v5",
DSN: "postgres://user:password@127.0.0.1:5432/dbname?Timezone=Asia/Shanghai",
}))
Description
I use 'TimeZone=Asia/Shanghai' to create DSN.
when v1.3.1 it‘s ok. but when I update to v1.5.0 or v1.5.2 it's not work.
The log is ok But the database saved time is less 8 hours.