+0
Daily
5,779
stars
414
forks
80
watchers
897
prs
1,085
issues
15
releases
README
PostgreSQL client and ORM for Golang
Maintenance mode
go-pg is in a maintenance mode and only critical issues are addressed. New development happens in
Bun repo which offers similar functionality
but works with PostgreSQL, MySQL, MariaDB, and SQLite.
Golang ORM
---
- Documentation
- Reference
- Examples
- Example projects:
- monetr - budgeting application focused on planning for
recurring expenses
- bunrouter
- gin
- go-kit
- aah framework
Tutorials
- GraphQL Tutorial on YouTube.
- Modern API design with Golang, PostgreSQL and Docker
Ecosystem
- Migrations by vmihailenco and
robinjoseph08.
- Genna - cli tool for generating go-pg models.
- bigint - big.Int type for go-pg.
- urlstruct to decode url.Values into structs.
- Sharding.
- go-pg-monitor - Prometheus metrics based on go-pg
client stats.
Features
- Basic types: integers, floats, string, bool, time.Time, net.IP, net.IPNet.
- sql.NullBool, sql.NullString, sql.NullInt64, sql.NullFloat64 and
pg.NullTime.
- sql.Scanner and
sql/driver.Valuer interfaces.
- Structs, maps and arrays are marshalled as JSON by default.
- PostgreSQL multidimensional Arrays using
array tag
and Array wrapper.
- Hstore using
hstore tag
and Hstore wrapper.
- Composite types.
- All struct fields are nullable by default and zero values (empty string, 0, zero time, empty map
or slice, nil ptr) are marshalled as SQL NULL. pg:",notnull" is used to add SQL NOT NULL
constraint and pg:",use_zero" to allow Go zero values.
- Transactions.
- Prepared statements.
- Notifications using
LISTEN and NOTIFY.
- Copying data using
COPY FROM and COPY TO.
- Timeouts and canceling queries using
context.Context.
- Automatic connection pooling with
circuit breaker support.
- Queries retry on network errors.
- Working with models using
ORM and
SQL.
- Scanning variables using
ORM
and SQL.
- SelectOrInsert
using on-conflict.
- INSERT ... ON CONFLICT DO UPDATE
using ORM.
- Bulk/batch
inserts,
updates, and
deletes.
- Common table expressions using
WITH and
WrapWith.
- CountEstimate
using EXPLAIN to get
estimated number of matching rows.
- ORM supports
has one,
belongs to,
has many, and
many to many
with composite/multi-column primary keys.
- Soft deletes.
- Creating tables from structs.
- ForEach that calls
a function for each row returned by the query without loading all rows into the memory.
Installation
go-pg supports 2 last Go versions and requires a Go version with
modules support. So make sure to initialize a Go
module:
shell
go mod init github.com/my/repo
And then install go-pg (note _v10_ in the import; omitting it is a popular mistake):
shell
go get github.com/go-pg/pg/v10
Quickstart
go
package pg_test
import (
"fmt"
"github.com/go-pg/pg/v10"
"github.com/go-pg/pg/v10/orm"
)
type User struct {
Id int64
Name string
Emails []string
}
func (u User) String() string {
return fmt.Sprintf("User", u.Id, Top issues
#959How to mock a pg.DB in tests?31 comments · 2018-08-20#829Support for geometric data types18 comments · 2018-04-27#1322Why does Relation() use Left Outer Join instead of Inner Join? Isn't Inner Join faster?10 comments · 2019-08-02#287Does it support range type?10 comments · 2016-08-05#1178Foreign key is not created when I ignore inverse side relation. 8 comments · 2019-04-13#1135Has Many relation is searching by NULL Id8 comments · 2019-03-13#490How to do prepare transaction (two-phase commit)?8 comments · 2017-03-16#787Update only changed/dirty columns7 comments · 2018-03-10
Discussions
#2019Best configuration to tackle failover1 comments · 2025-04-01#2010Can the go-pg specify different database drivers for different types of pg databases?1 comments · 2024-07-18#2009Can we support "HighGo Database"?1 comments · 2024-07-15#2005Is it safe to use *pg.DB from multiple GO routines?1 comments · 2024-05-29#1999Multiple joins not deserializing correctly0 comments · 2024-02-08#1986Maybe support for Geometric Types?0 comments · 2023-10-10#1983Performance on bulk/batch insert / update / delete1 comments · 2023-08-04#1980How can i get data of joined table in nested object if using raw query?0 comments · 2023-06-08
Latest release
v10.15.1 v10.15.1 2026-05-29Important Security Update This update includes a fix for a security issue, please update to this release as soon as possible. - Fixed issue with update query values (2043) - Bumped golang.org/x/crypto from v0.36.0 to v0.45.0 (2040) (2037) (2038) - Fixed x/crypto/ssh security warnings (2039) - Bumped gopkg.in/yaml.v3 from v3.0.0 to v3.0.1 (2035) Please refer to CHANGELOG.md for details Thank you @molzri3 for reporting the security issue!