https://github.com/henvic/pgq
pgq is a query builder for PostgreSQL written in Go.
https://github.com/henvic/pgq
go golang postgres postgresql
Last synced: 6 months ago
JSON representation
pgq is a query builder for PostgreSQL written in Go.
- Host: GitHub
- URL: https://github.com/henvic/pgq
- Owner: henvic
- License: other
- Created: 2022-10-16T18:40:42.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2025-04-10T23:40:29.000Z (about 1 year ago)
- Last Synced: 2025-04-11T00:27:45.516Z (about 1 year ago)
- Topics: go, golang, postgres, postgresql
- Language: Go
- Homepage: https://github.com/henvic/pgqbenchmark
- Size: 362 KB
- Stars: 49
- Watchers: 2
- Forks: 4
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
# pgq
[](https://godoc.org/github.com/henvic/pgq) [](https://github.com/henvic/pgq/actions?query=workflow%3ATests) [](https://coveralls.io/r/henvic/pgq)
pgq is a query builder for PostgreSQL written in Go.
It is a fork of [Squirrel](https://github.com/Masterminds/squirrel) more suitable for working with the PostgreSQL database when you are able to use the native PostgreSQL protocol directly, rather than the slower textual protocol used by database/sql.
You can use it with the [pgx](https://github.com/jackc/pgx) driver.
## Usage example
Something like this:
```go
sql, args, err := pgq.Update("employees").
Set("salary_bonus", pgq.Expr("salary_bonus + 1000")).
From("accounts").
Where("accounts.team = ?", "engineering").
Returning("id", "name", "salary").SQL()
if err != nil {
panic(err) // bug: this should never happen.
}
type employee struct {
ID string
Name string
Salary int
}
var data []employee
rows, err := pool.Query(context.Background(), sql, args...)
if err == nil {
defer rows.Close()
data, err = pgx.CollectRows(rows, pgx.RowTo[employee])
}
```
## Main benefits
* API is crafted with only PostgreSQL compatibility so it has a somewhat lean API.
* It uses ANY and ALL [operators for slices](https://www.postgresql.org/docs/current/functions-comparisons.html) by default, which means it supports slices out of the box and you get to reuse your prepared statements.
* It's throughly tested (including integration tests to check for invalid queries being generated).
* If you already use pgx with Squirrel and the native PostgreSQL protocol, switching is very straightforward with just a few breaking changes (example: Alias is a type rather than a function).
## Main drawback
* It's still a query builder. You can go a long way writing pure SQL queries. Consider doing so.
## FAQ
**Why forking a query builder then?** Whatever it takes to make people ditch using an [ORM](https://alanilling.com/exiting-the-vietnam-of-programming-our-journey-in-dropping-the-orm-in-golang-3ce7dff24a0f), I guess.
## See also
* [pgtools](https://github.com/henvic/pgtools/)
* [pgxtutorial](https://github.com/henvic/pgxtutorial)