Adding basic go example for gorm with postgresql.
parent
e71a793a7b
commit
0f7094c61a
|
@ -0,0 +1,40 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
postgres "gorm.io/driver/postgres"
|
||||
gorm "gorm.io/gorm"
|
||||
"log"
|
||||
)
|
||||
|
||||
type User struct {
|
||||
Id uint `gorm:"primaryKey"`
|
||||
FirstName string
|
||||
LastName string
|
||||
Locked bool
|
||||
}
|
||||
|
||||
func main() {
|
||||
url := "host=localhost user=gorm password=gorm dbname=gorm port=5432 sslmode=disable TimeZone=Europe/Warsaw"
|
||||
config := gorm.Config{}
|
||||
con, err := gorm.Open(postgres.Open(url), &config)
|
||||
if err != nil {
|
||||
log.Panicf("Sorry, I couldn't create a connection to database %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
err = con.AutoMigrate(&User{})
|
||||
if err != nil {
|
||||
log.Panicf("I couldn't migrate user %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
u := User{
|
||||
FirstName: "Tomasz",
|
||||
LastName: "Półgrabia",
|
||||
Locked: false,
|
||||
}
|
||||
con.Create(&u)
|
||||
|
||||
fmt.Printf("Created first user %v", u)
|
||||
}
|
|
@ -0,0 +1,10 @@
|
|||
version: "3.9" # optional since v1.27.0
|
||||
services:
|
||||
postgresql:
|
||||
image: postgres:14
|
||||
environment:
|
||||
- POSTGRES_PASSWORD=123456
|
||||
ports:
|
||||
- "5432:5432"
|
||||
volumes:
|
||||
- c:\\developer\\bin\\postgresql\\data:/var/lib/postgresql/data
|
Loading…
Reference in New Issue