Getting started¶
Warning
go-pg is in a maintenance mode and will NOT receive new features. Please use Bun Golang ORM instead.
go-pg supports 2 last Go versions and requires support for Go modules. So make sure to initialize a Go module:
go mod init github.com/my/repo
And then install pg/v10 (note v10 in the import; omitting it is a popular mistake):
go get github.com/go-pg/pg/v10
To connect to a database:
db:=pg.Connect(&pg.Options{
Addr:":5432",
User:"user",
Password:"pass",
Database:"db_name",
})
Another popular way is using a connection string:
opt,err:=pg.ParseURL("postgres://user:pass@localhost:5432/db_name")
iferr!=nil{
panic(err)
}
db:=pg.Connect(opt)
To check if database is up and running:
ctx:=context.Background()
iferr:=db.Ping(ctx);err!=nil{
panic(err)
}
The same:
_,err:=db.ExecContext(ctx,"SELECT 1")
iferr!=nil{
panic(err)
}
To select PostgreSQL version:
varversionstring
_,err:=db.QueryOneContext(ctx,pg.Scan(&version),"SELECT version()")
iferr!=nil{
panic(err)
}
fmt.Println(version)
Following example demonstrates how to connect, create schema, insert, and select data:
packagepg_test
import(
"fmt"
"github.com/go-pg/pg/v10"
"github.com/go-pg/pg/v10/orm"
)
typeUserstruct{
Idint64
Namestring
Emails[]string
}
func(uUser)String()string{
returnfmt.Sprintf("User<%d %s %v>",u.Id,u.Name,u.Emails)
}
typeStorystruct{
Idint64
Titlestring
AuthorIdint64
Author*User
}
func(sStory)String()string{
returnfmt.Sprintf("Story<%d %s %s>",s.Id,s.Title,s.Author)
}
funcExampleDB_Model(){
db:=pg.Connect(&pg.Options{
User:"postgres",
})
deferdb.Close()
err:=createSchema(db)
iferr!=nil{
panic(err)
}
user1:=&User{
Name:"admin",
Emails:[]string{"admin1@admin","admin2@admin"},
}
_,err=db.Model(user1).Insert()
iferr!=nil{
panic(err)
}
_,err=db.Model(&User{
Name:"root",
Emails:[]string{"root1@root","root2@root"},
}).Insert()
iferr!=nil{
panic(err)
}
story1:=&Story{
Title:"Cool story",
AuthorId:user1.Id,
}
_,err=db.Model(story1).Insert()
iferr!=nil{
panic(err)
}
// Select user by primary key.
user:=&User{Id:user1.Id}
err=db.Model(user).WherePK().Select()
iferr!=nil{
panic(err)
}
// Select all users.
varusers[]User
err=db.Model(&users).Select()
iferr!=nil{
panic(err)
}
// Select story and associated author in one query.
story:=new(Story)
err=db.Model(story).
Relation("Author").
Where("story.id = ?",story1.Id).
Select()
iferr!=nil{
panic(err)
}
fmt.Println(user)
fmt.Println(users)
fmt.Println(story)
// Output: User<1 admin [admin1@admin admin2@admin]>
// [User<1 admin [admin1@admin admin2@admin]> User<2 root [root1@root root2@root]>]
// Story<1 Cool story User<1 admin [admin1@admin admin2@admin]>>
}
// createSchema creates database schema for User and Story models.
funccreateSchema(db*pg.DB)error{
models:=[]interface{}{
(*User)(nil),
(*Story)(nil),
}
for_,model:=rangemodels{
err:=db.Model(model).CreateTable(&orm.CreateTableOptions{
Temp:true,
})
iferr!=nil{
returnerr
}
}
returnnil
}
Bun¶
go-pg is in a maintenance mode and will NOT receive new features. Please use Bun Golang ORM instead.
