Skip to main content

Connecting to PostgreMQ

An SDK connection manages queue operations and the background services needed by its consumers. Keep it long-lived and close it during application shutdown. The PostgreMQ SQL schema must already be installed; connecting checks compatibility but does not migrate the database.

For package installation and a complete standalone program, use the Quick start. The examples below show how an existing application supplies its PostgreSQL pool.

Share an application pool​

Sharing a pool allows application writes and PostgreMQ operations to use the same database and transaction types. The application retains ownership of the pool and closes it after the queue connection has drained.

These snippets run inside your application's startup function, where pool is an existing pgx, SQLx, or node-postgres pool. Go also uses a startup context named ctx.

conn, err := postgremq.DialFromPool(ctx, pool,
postgremq.WithShutdownTimeout(10*time.Second))
if err != nil { return err }
// Use conn for publishers and consumers throughout the application's lifetime.

All three configurations allow ten seconds for queue shutdown. Use Graceful shutdown for the close sequence and active-delivery responsibilities. When the SDK creates its own pool through its normal connection API, it owns that pool instead.

Connect separate publishers and consumers​

A publisher needs the topic name. A consumer needs the queue name and enough information to establish that subscription's notifications and identity. The queue can be declared once during deployment; a Durable queue does not require redeclaration on every worker startup.

For a queue named jobs on topic events, use these stream-consumer declarations with the connections above:

consumer, err := conn.Consume("jobs", postgremq.WithTopic("events"))
if err != nil { return err }

Go and TypeScript use the supplied topic when the queue was not declared on their connection. Rust resolves it when starting the consumer. Continue with the stream loop; a TypeScript consumer starts reception when you call messages(). Iteration reads the deliveries it claims.

Handle startup and later failures separately​

Connecting verifies database reachability and schema compatibility through postgremq.info(). It can fail on connection settings, a missing schema, missing schema USAGE or EXECUTE on info(), or an incompatible SQL major. It does not validate every runtime grant or the existence of application topics and queues; those failures surface when the relevant operation runs. Verify a publish/consume/acknowledge flow with the runtime role before accepting application traffic.

A successful connection does not promise permanent availability. Observe operation errors, consumer termination, and queue-loss callbacks. An expired or replaced queue cannot be recovered merely by reconnecting the transport. Queue troubleshooting explains that distinction.

Reserve capacity for background work​

Consumers need pool capacity for claims, acknowledgements, and renewal alongside application queries. Go and TypeScript also retain a pooled listener connection; Rust's listener uses a separate pool. Count all replicas when sizing database connections.

LISTEN needs session continuity. Use direct PostgreSQL or a session-mode pooler for the SDK's listener path; transaction-mode pooling does not preserve that contract. Do not let long application transactions occupy every connection needed to settle their own queue work.

Use the normal PostgreSQL driver's TLS and connection settings, and keep credentials out of logs. See Planning and SDK behavior for pool ownership and configuration differences.