explaingit

go-pg/pg

5,786GoAudience · developerComplexity · 3/5Setup · moderate

TLDR

A Go library that connects apps to PostgreSQL using an ORM, so you work with Go data structures instead of writing raw SQL. Now in maintenance mode, new projects should use its successor, Bun.

Mindmap

mindmap
  root((go-pg))
    PostgreSQL Features
      Array columns
      JSONB support
      LISTEN and NOTIFY
      hstore columns
    ORM Layer
      Struct mapping
      Relationships
      Soft deletes
      Bulk operations
    Connection Management
      Connection pooling
      Auto retry
    Project Status
      Maintenance mode
      Migrate to Bun
    Setup
      Go modules
      Version 10 import
Click or tap to explore — scroll the page freely

Code map

Detail Auto

An interactive map of this repo's files and how they connect — its source is parsed live in your browser. Click Visualize to build it.

filefunction / class

Things people build with this

USE CASE 1

Build a Go web app that reads and writes data to a PostgreSQL database without writing raw SQL.

USE CASE 2

Store and query structured JSON data in PostgreSQL using JSONB columns from Go code.

USE CASE 3

Model related records like users and orders in Go structs and let the library handle the database joins.

USE CASE 4

Get live database change notifications in a Go app using PostgreSQL LISTEN and NOTIFY.

Tech stack

GoPostgreSQLORMJSONBhstoreconnection pooling

Getting it running

Difficulty · moderate Time to first run · 30min

Requires a recent Go version with module support. Import path must include the version suffix v10, omitting it is a common mistake. Active development has moved to Bun.

License not mentioned in the explanation.

In plain English

go-pg is a library for Go developers that connects their applications to PostgreSQL databases. An ORM, which stands for Object-Relational Mapper, means it lets you work with database records as regular Go data structures instead of writing raw SQL by hand. You define your data shapes as Go structs, and the library handles translating operations like inserting a record or finding rows that match a condition into the appropriate database queries. The project is now in maintenance mode. Only critical bugs are being fixed, and active development has moved to a newer library called Bun, which offers similar functionality but also supports MySQL, MariaDB, and SQLite in addition to PostgreSQL. go-pg was built specifically around PostgreSQL and exposes features particular to that database: PostgreSQL array columns, the hstore key-value column type, JSONB columns, bulk insert and update operations, and live notifications through the database's LISTEN and NOTIFY system. The library manages a pool of database connections automatically and retries queries if a network error interrupts them. Relationships between tables are handled through the ORM layer. You can express that one record belongs to another, that one record has many related records, or that two types of records relate to each other through a joining table. Soft deletes are also supported, where a record is flagged as deleted rather than physically removed from the database. Installation requires a recent version of Go with module support. The library is imported as version 10, and the README notes that leaving the version number out of the import path is a common mistake.

Copy-paste prompts

Prompt 1
How do I define a Go struct that maps to a PostgreSQL table using go-pg, and how do I insert a new record?
Prompt 2
Show me how to query rows from PostgreSQL with a filter condition using go-pg without writing raw SQL.
Prompt 3
How do I set up a one-to-many relationship between two tables in go-pg, for example a user who has many posts?
Prompt 4
What is soft delete in go-pg and how do I mark a record as deleted without removing it from the database?
Prompt 5
I'm using go-pg but want to migrate to Bun, what are the main differences and how do I get started?
Open on GitHub → Explain another repo

← go-pg on gitmyhub — every repo by this author, as a profile.

Verify against the repo before relying on details.