升级到 Gorm 版本后:
gorm.io/driver/postgres v1.0.2
gorm.io/gorm v1.20.2
无法连接到 Google Cloud SQL PostgreSQL v12
该连接在旧 GORM 版本上没有问题,具有:
db, err := gorm.Open("postgres", "host=/cloudsql/project_id:us-central1:sql_instance_name port=5432 user=... dbname=... password=... sslmode=disable")
新的 GORM 版本适用于 localhost_golang / localhost_postgres 的本地安装(mac)
谷歌云上的代码:
dsn := ""
dsn += "user=postgres "
dsn += "password=admin_password_here "
dsn += "host=/cloudsql/project_id:us-central1:sql_instance_name "
dsn += "dbname=db_name "
dsn += "port=5432 "
dsn += "sslmode=disable "
fmt.Println("dsn:%v", dsn)
db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{
NamingStrategy: schema.NamingStrategy{
SingularTable: true,
},
})
谷歌云上的错误:
[0m [31m [错误] [0m无法初始化数据库,出错无法连接host=/cloudsql/project_id:us-central1:sql_instance_name user=postgres database=db_name
:拨号错误(拨号unix /cloudsql/project_id:us-central1:sql_instance_name/.s.PGSQL.5432:连接:拒绝连接)
谢谢!