https://github.com/craigpastro/pgmq-go
A Go (Golang) client for Postgres Message Queue (PGMQ)
https://github.com/craigpastro/pgmq-go
go golang postgres postgres-extension postgresql queue
Last synced: 12 months ago
JSON representation
A Go (Golang) client for Postgres Message Queue (PGMQ)
- Host: GitHub
- URL: https://github.com/craigpastro/pgmq-go
- Owner: craigpastro
- License: mit
- Created: 2023-08-08T17:31:57.000Z (almost 3 years ago)
- Default Branch: main
- Last Pushed: 2025-05-02T00:52:30.000Z (about 1 year ago)
- Last Synced: 2025-05-20T09:07:48.185Z (about 1 year ago)
- Topics: go, golang, postgres, postgres-extension, postgresql, queue
- Language: Go
- Homepage:
- Size: 111 KB
- Stars: 57
- Watchers: 1
- Forks: 4
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# pgmq-go
[](https://pkg.go.dev/github.com/craigpastro/pgmq-go)
[](https://goreportcard.com/report/github.com/craigpastro/pgmq-go)
[](https://github.com/craigpastro/pgmq-go/actions/workflows/push_to_main.yaml)
[](https://codecov.io/github/craigpastro/pgmq-go)
A Go (Golang) client for
[Postgres Message Queue](https://github.com/tembo-io/pgmq) (PGMQ). Based loosely
on the [Rust client](https://github.com/tembo-io/pgmq/tree/main/pgmq-rs).
`pgmq-go` works with [pgx](https://github.com/jackc/pgx). The second argument of most functions only needs to satisfy the [DB](https://pkg.go.dev/github.com/craigpastro/pgmq-go#DB) interface, which means it can take, among others, a `*pgx.Conn`, `*pgxpool.Pool`, or `pgx.Tx`.
## Usage
Start a Postgres instance with the PGMQ extension installed:
```shell
docker run -d --name postgres -e POSTGRES_PASSWORD=password -p 5432:5432 quay.io/tembo/pgmq-pg:latest
```
Then
```go
package main
import (
"context"
"fmt"
"github.com/craigpastro/pgmq-go"
)
func main() {
ctx := context.Background()
pool, err := pgmq.NewPgxPool(ctx, "postgres://postgres:password@localhost:5432/postgres")
if err != nil {
panic(err)
}
err = pgmq.CreatePGMQExtension(ctx, pool)
if err != nil {
panic(err)
}
err = pgmq.CreateQueue(ctx, pool, "my_queue")
if err != nil {
panic(err)
}
// We can perform various queue operations using a transaction.
tx, err := pool.Begin(ctx)
if err != nil {
panic(err)
}
id, err := pgmq.Send(ctx, tx, "my_queue", json.RawMessage(`{"foo": "bar"}`))
if err != nil {
panic(err)
}
msg, err := pgmq.Read(ctx, tx, "my_queue", 30)
if err != nil {
panic(err)
}
// Archive the message by moving it to the "pgmq.a_" table.
// Alternatively, you can `Delete` the message, or read and delete in one
// call by using `Pop`.
_, err = pgmq.Archive(ctx, tx, "my_queue", id)
if err != nil {
panic(err)
}
// Commit the transaction.
err = tx.Commit(ctx)
if err != nil {
panic(err)
}
// Close the connection pool.
pool.Close()
}
```
## Contributions
We :heart: contributions.
## See also
- [joeychilson/pgmq](https://github.com/joeychilson/pgmq)