generated from erangel1/generic-template
23 lines
533 B
Go
23 lines
533 B
Go
package main
|
|
|
|
import (
|
|
"gorm.io/driver/postgres"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
type Task struct {
|
|
gorm.Model
|
|
Title string `json:"title"`
|
|
}
|
|
|
|
func InitDB() *gorm.DB {
|
|
dsn := "host=postgresql://postgres:postgres@gopostgres-vlcjiq:5432/go_postgres user=postgres password=postgres dbname=go_postgres port=5432 sslmode=disable"
|
|
db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{})
|
|
if err != nil {
|
|
panic("Failed to connect to database")
|
|
}
|
|
// This automatically creates the 'tasks' table in Postgres
|
|
db.AutoMigrate(&Task{})
|
|
return db
|
|
}
|