开发环境:
windows10上安装的vagrant,
vagrant中运行centos7,
centos7中安装Golang和PostgreSQL,编译和运行go代码。
问题:
使用gorm连接不上PostgreSQL。gorm文档是这样写的:
按照示例写的代码:
package main
import (
"fmt"
"apiserver/model"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/postgres"
)
func main() {
db, err := gorm.Open("postgres", "host=127.0.0.1 port=5432 user=postgres dbname=postgres password=123")
if err != nil {
panic("failed to connect database")
}
defer db.Close()
db.AutoMigrate(&model.UserModel{})
}
上面代码编译报错:
[root@vagrant-dev web]# go run main.go
panic: failed to connect database
goroutine 1 [running]:
main.main()
/var/www/go_work/src/apiserver/cmd/web/main.go:20 +0x1fa
exit status 2
错误信息里面说的20行就是panic("failed to connect database")
。
同时我在windows上用navicat是可以远程连接PostgreSQL的:
是什么原因编译报错呢?
能不能把err输出一下,你直接panic怎么知道是什么错误呢?