代码之家  ›  专栏  ›  技术社区  ›  Nir Berko

连接本地主机Postgres数据库

  •  1
  • Nir Berko  · 技术社区  · 6 年前

    我刚刚在我的电脑上第一次安装了Postgres,我可以通过JetBrains DataGrip连接我的数据库。

    jdbc:postgresql://localhost:5432/postgres
    

    我试着用这种方式联系:

    postgresql://postgres:12qwaszx@localhost:5432/postgres
    

    这样的话:

    db, _ := gorm.Open("postgres", "host=localhost port=5432 user=postgres dbname=postgres password=12qwaszx")
    

    以下是我连接数据库的方式:

    db, _ := gorm.Open("postgres", app.Config.DSN)
    
    1 回复  |  直到 6 年前
        1
  •  0
  •   nijm    6 年前

    根据 docs ,您应该这样连接到PostgreSQL:

    import (
      "github.com/jinzhu/gorm"
      _ "github.com/jinzhu/gorm/dialects/postgres"
    )
    
    func main() {
      db, err := gorm.Open("postgres", "host=myhost port=myport user=gorm dbname=gorm password=mypassword")
      defer db.Close()
    }