go-pg /pg

Golang ORM with focus on PostgreSQL features and performance

★ 5,779⑂ 414👁 80⑃ 897● 1,085▤ 15
GoBSD-2-ClauseCreated 2013-04-24pg.uptrace.dev/
下载:v10.15.1v10.15.1 ↓10.15.12026-05-29
+0
日增长
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

Discussions

Latest release

v10.15.1 v10.15.1 2026-05-29
Important 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!

Snapshot: 2026-11-16T09:23:14Z · Data license Apache-2.0 · JSON