gitmyhub

pg

Go ★ 5.8k updated 1mo ago

Golang ORM with focus on PostgreSQL features and performance

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.

GoPostgreSQLORMJSONBhstoreconnection poolingsetup: moderatecomplexity 3/5

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.

Where it fits