Skip to content

Instantly share code, notes, and snippets.

@mangalaman93
Created April 25, 2020 10:59
Show Gist options
  • Save mangalaman93/ea956abea624e045035f42395acc969f to your computer and use it in GitHub Desktop.
Save mangalaman93/ea956abea624e045035f42395acc969f to your computer and use it in GitHub Desktop.
Use gorm with schema in postgres database
package main
import (
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/postgres"
)
type User struct {
ID int
Username string
}
func main() {
db, err := gorm.Open("postgres", "host=localhost port=5432 sslmode=disable dbname=aman")
if err != nil {
panic(err)
}
defer db.Close()
db.LogMode(true)
// set schema here.
gorm.DefaultTableNameHandler = func(db *gorm.DB, tableName string) string {
return "s1." + tableName
}
db.CreateTable(&User{})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment