go-pg is a Go PostgreSQL client and ORM focused on PostgreSQL-specific types, relations, and performance-oriented query features.
Overview
go-pg connects Go applications to PostgreSQL with an ORM layer for models, relations, and raw SQL, plus a client that exposes PostgreSQL features such as LISTEN/NOTIFY, COPY FROM/TO, array and hstore types, JSON marshalling for structs, and context-aware queries.
Models support nullable fields, composite keys, has-one, belongs-to, has-many, and many-to-many associations. The library offers insert-on-conflict helpers, bulk inserts and updates, common table expressions, EXPLAIN-based row estimates, and iterators that process rows without loading entire result sets into memory. Connection pooling includes circuit breaker support and retries on network errors.
Install with Go modules using github.com/go-pg/pg/v10. Connections accept option structs or postgres:// URLs, with Ping and ExecContext for health checks. Documentation and examples cover schema creation, inserts, selects with relations, and lower-level scanning APIs.
The project is in maintenance mode addressing only critical issues; new feature development has moved to the Bun ORM, which targets PostgreSQL, MySQL, MariaDB, and SQLite. Existing go-pg users are directed to Bun for ongoing enhancements.
Key features
- PostgreSQL ORM and raw SQL
- JSON, arrays, and hstore types
- LISTEN, NOTIFY, and COPY
- Relation mapping helpers
- Bulk insert and update
- Context-aware queries
- Connection pool with retries
- Insert on conflict support
Use cases
- Go apps on PostgreSQL
- JSONB and array-heavy schemas
- Relational model CRUD
- Pub/sub via NOTIFY