pgqrs
A PostgreSQL-backed job queue for Rust applications.
Features
- Simple Installation: Add
pgqrslibrary as a dependency in your Rust applications. - Compatible with Connection Poolers: Use with pgBouncer or pgcat to scale connections.
- Efficient: Uses PostgreSQL's
SKIP LOCKEDfor concurrent job fetching
Getting Started
Install the binary
cargo install pgqrs
Start a Postgres DB or get the DSN of an existing db.
You'll need a PostgreSQL database to use pgqrs. Here are your options:
Option 1: Using Docker (Recommended for development)
# Start a PostgreSQL container
# Your DSN will be:
# postgresql://postgres:postgres@localhost:5432/postgres
Option 2: Using an existing PostgreSQL database
Get your database connection string (DSN) in this format:
postgresql://username:password@hostname:port/database
Option 3: Using a cloud PostgreSQL service
- AWS RDS: Get the connection string from the RDS console
- Google Cloud SQL: Get the connection string from the Cloud Console
- Azure Database: Get the connection string from the Azure portal
- Heroku Postgres: Use the
DATABASE_URLfrom your Heroku config
Configure pgqrs
Set your database connection using one of these methods (in order of priority):
# Method 1: Command line argument (highest priority)
# Method 2: Environment variable
Create a pgqrs.yaml file:
dsn: "postgresql://postgres:postgres@localhost:5432/postgres"
Then run:
# Method 3: Use a yaml config file.
Install the pgqrs schema
pgqrs requires a few tables to store metadata. It creates these tables as well as
queue tables in the schema pgqrs.
Once you have your database configured, install the pgqrs schema:
# Verify the state
Test queue commands from the CLI
Items can be enqueued or dequeued using the CLI. This option is only available for testing or experiments.
# Create a test queue
# Send a message to the queue
# Send a delayed message (available after 30 seconds)
# Read and immediately consume one message
# Delete a specific message by ID
Queue API
Add to your Cargo.toml:
[]
= "0.1.0"
See examples/basic_usage.rs for a full example. Typical usage:
use PgqrsAdmin;
use Config;
use json;
async
Configuration
pgqrs uses a prioritized configuration system. Configuration is loaded in the following order (highest priority first):
1. Command Line Arguments (Highest Priority)
# Override DSN via command line
# Override config file location
2. Environment Variables
# Required: Database connection string
# Optional: Connection pool settings
# Optional: Default job settings
# Optional: Config file location
3. Configuration File
Create a YAML configuration file (default locations: pgqrs.yaml, pgqrs.yml):
# Required: Database connection string
dsn: "postgresql://user:pass@localhost/db"
# Optional: Connection pool settings (defaults shown)
max_connections: 16
connection_timeout_seconds: 30
# Optional: Default job settings (defaults shown)
default_lock_time_seconds: 5
default_max_batch_size: 100
4. Programmatic Configuration
use Config;
// Create from explicit DSN
let config = from_dsn;
// Load from environment variables
let config = from_env?;
// Load from specific file
let config = from_file?;
// Load automatically with priority order
let config = load?;
// Load with explicit overrides (for CLI tools)
let config = load_with_options?;
Configuration Reference
| Field | Environment Variable | Description | Default |
|---|---|---|---|
dsn |
PGQRS_DSN |
PostgreSQL connection string | Required |
max_connections |
PGQRS_MAX_CONNECTIONS |
Maximum database connections | 16 |
connection_timeout_seconds |
PGQRS_CONNECTION_TIMEOUT |
Connection timeout in seconds | 30 |
default_lock_time_seconds |
PGQRS_DEFAULT_LOCK_TIME |
Default job lock time | 5 |
default_max_batch_size |
PGQRS_DEFAULT_BATCH_SIZE |
Default batch size for operations | 100 |
CLI Usage
The CLI is defined in src/main.rs and supports the following commands:
Top-level commands
install— Install pgqrs schemauninstall— Uninstall pgqrs schemaverify— Verify installationqueue <subcommand>— Queue managementmessage <subcommand>— Message management
Queue commands
queue create <name>— Create a new queuequeue list— List all queuesqueue delete <name>— Delete a queuequeue purge <name>— Purge all messages from a queuequeue metrics [<name>]— Show metrics for a queue or all queues
Message commands
message send <queue> <payload> [--delay <seconds>]— Send a message (payload is JSON)message read <queue> [--count <n>] [--lock-time <seconds>] [--message-type <type>]— Read messagesmessage dequeue <queue>— Read and return one messagemessage delete <queue> <id>— Delete a message by IDmessage count <queue>— Show pending message count
Output and Logging Options
All commands support global flags:
-d, --dsn <DSN>— Database URL (highest priority, overrides all other config sources)-c, --config <CONFIG>— Config file path (overrides environment variables and defaults)--log-dest <stderr|file>— Log destination--log-level <error|warn|info|debug|trace>— Log level--format <json|table>— Output format--out <stdout|file>— Output destination
License
Licensed under either of:
- Apache License, Version 2.0
- MIT license
at your option.