我正在尝试通过教程使用postgresql来学习golang中的数据库。
虽然数据库存在,但程序提供输出:
go run dbase.go
2017/01/02 20:43:07 pq: database "bookstore" does not exist
这是psql输出:
deneme=# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+----------+----------+---------+-------+-----------------------
bookstore | postgres | UTF8 | C | C |
deneme | postgres | UTF8 | C | C |
这是go代码的相关部分:
const (
DB_USER = "postgres"
DB_PASSWORD = "root"
DB_NAME = "bookstore"
)
func main() {
dbinfo := fmt.Sprintf("user=%s password=%s dbname=%s sslmode=disable",
DB_USER, DB_PASSWORD, DB_NAME)
db, err := sql.Open("postgres", dbinfo)
if err != nil {
log.Fatal(err)
}
同样将连接代码更改为相同的输出:
db, err := sql.Open("postgres", "postgres://postgres:root@localhost/bookstore?sslmode=disable")
感谢;