Npgmq 1.13.0

dotnet add package Npgmq --version 1.13.0
                    
NuGet\Install-Package Npgmq -Version 1.13.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Npgmq" Version="1.13.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Npgmq" Version="1.13.0" />
                    
Directory.Packages.props
<PackageReference Include="Npgmq" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Npgmq --version 1.13.0
                    
#r "nuget: Npgmq, 1.13.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Npgmq@1.13.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Npgmq&version=1.13.0
                    
Install as a Cake Addin
#tool nuget:?package=Npgmq&version=1.13.0
                    
Install as a Cake Tool

Npgmq

A .NET client for Postgres Message Queue (PGMQ).

Build Nuget License

Installation

To install the package via Nuget, run the following command:

dotnet add package Npgmq

Creating NpgmqClient

When creating an NpgmqClient instance, the recommended approach is to use an NpgsqlDataSource. This provides the best connection pooling, performance, and safety.

await using var dataSource = NpgsqlDataSource.Create("<YOUR CONNECTION STRING HERE>");
var npgmq = new NpgmqClient(dataSource);

You can also construct the client with a connection string or an existing NpgsqlConnection, but those options have tradeoffs. See Database Connection below.

Using Dependency Injection (ASP.NET Core)

For ASP.NET Core applications, register NpgmqClient in the DI container using one of the following methods:

builder.Services.AddNpgmqClient("<YOUR CONNECTION STRING HERE>");
builder.Services.AddNpgmqClient(); // If you already have an NpgsqlDataSource registered

Basic Example

using Npgmq;

var npgmq = new NpgmqClient("<YOUR CONNECTION STRING HERE>");

await npgmq.InitAsync(); // Optional (ensures the pgmq extension has been created in postgres)

await npgmq.CreateQueueAsync("my_queue");

var msgId = await npgmq.SendAsync("my_queue", new MyMessageType
{
    Foo = "Test",
    Bar = 123
});
Console.WriteLine($"Sent message with id {msgId}");

var msg = await npgmq.ReadAsync<MyMessageType>("my_queue");
if (msg != null)
{
    Console.WriteLine($"Read message with id {msg.MsgId}: Foo = {msg.Message?.Foo}, Bar = {msg.Message?.Bar}");
    await npgmq.ArchiveAsync("my_queue", msg.MsgId);
}
public class MyMessageType
{
    public string Foo { get; set; } = null!;
    public int Bar { get; set; }
}

Additional Examples

For more complete examples, see the following projects included in this repository:

JSON Messages (Custom Serialization)

If you want full control over JSON serialization, you can send and receive messages as string:

var msgId = await npgmq.SendAsync("my_queue", "{\"foo\":\"Test\",\"bar\":123}");
Console.WriteLine($"Sent message with id {msgId}");

var msg = await npgmq.ReadAsync<string>("my_queue");
if (msg != null)
{
    Console.WriteLine($"Read message with id {msg.MsgId}: {msg.Message}");
    await npgmq.ArchiveAsync("my_queue", msg.MsgId);
}

Important:

  • When sending messages as string, the value must contain valid JSON.
  • NpgmqClient does not validate or escape string messages. Invalid JSON will cause the database call to fail.

Database Connection

Npgmq uses Npgsql internally to connect to the database.

await using var myDataSource = NpgsqlDataSource.Create("<YOUR CONNECTION STRING HERE>");
var npgmq = new NpgmqClient(myDataSource);

This is the preferred approach and provides optimal pooling and configuration.

Using a Connection String

var npgmq = new NpgmqClient("<YOUR CONNECTION STRING HERE>");

Connections are created as needed using the provided connection string.

Using an Existing NpgsqlConnection

await using var myConnection = new NpgsqlConnection("<YOUR CONNECTION STRING HERE>");
var npgmq = new NpgmqClient(myConnection);

When using this constructor:

  • You are responsible for managing the connection lifecycle.
  • NpgmqClient will open the connection if necessary, but will not close or dispose it.
  • Concurrent usage is not supported. Ensure only one operation uses the connection at a time.

Concurrency

NpgmqClient is safe for concurrent use when constructed with:

  • an NpgsqlDataSource
  • a connection string

When constructed with an existing NpgsqlConnection, concurrent operations are not supported.

PGMQ Version Requirements

Npgmq is tested with pgmq versions 1.5.1 and higher.

Some features require minimum versions of the pgmq extension:

Feature Minimum pgmq version
PopAsync(queue, qty) 1.7.0
SetVtBatchAsync 1.8.0
FIFO grouped reads 1.9.0
Topic support 1.11.0
ReadGroupedHeadAsync 1.11.1

You can check the installed version:

var version = await npgmq.GetPgmqVersionAsync();

License

MIT License. See LICENSE for details.

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on Npgmq:

Package Downloads
Motor.Extensions.Hosting.PgMq

Motor.NET is a microservice framework based on Microsoft.Extensions.Hosting.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.13.0 2,702 4/21/2026
1.12.0 560 4/13/2026
1.11.0 1,818 2/10/2026
1.10.0 583 1/30/2026
1.9.0 212 1/24/2026
1.8.1 145 1/24/2026
1.8.0 1,428 12/9/2025
1.7.0 487 12/9/2025
1.6.0 2,727 9/13/2025
1.5.0 2,625 1/22/2025
1.4.0 247 12/27/2024
1.3.0 4,332 8/8/2024