You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
pgmq-go works with pgx. The second argument of most functions only needs to satisfy the 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:
docker run -d --name postgres -e POSTGRES_PASSWORD=password -p 5432:5432 quay.io/tembo/pgmq-pg:latest
Then
package main
import (
"context""fmt""github.com/craigpastro/pgmq-go"
)
funcmain() {
ctx:=context.Background()
pool, err:=pgmq.NewPgxPool(ctx, "postgres://postgres:password@localhost:5432/postgres")
iferr!=nil {
panic(err)
}
err=pgmq.CreatePGMQExtension(ctx, pool)
iferr!=nil {
panic(err)
}
err=pgmq.CreateQueue(ctx, pool, "my_queue")
iferr!=nil {
panic(err)
}
// We can perform various queue operations using a transaction.tx, err:=pool.Begin(ctx)
iferr!=nil {
panic(err)
}
id, err:=pgmq.Send(ctx, tx, "my_queue", json.RawMessage(`{"foo": "bar"}`))
iferr!=nil {
panic(err)
}
msg, err:=pgmq.Read(ctx, tx, "my_queue", 30)
iferr!=nil {
panic(err)
}
// Archive the message by moving it to the "pgmq.a_<queue_name>" 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)
iferr!=nil {
panic(err)
}
// Commit the transaction.err=tx.Commit(ctx)
iferr!=nil {
panic(err)
}
// Close the connection pool.pool.Close()
}