DocumentationpgBalancer Documentation

CLI Management (bctl)

Install and Configure bctl

bctl is the unified CLI tool that replaces multiple pcp_* commands:

Install bctl

# bctl is included with pgBalancer
# After installing pgbalancer, bctl is in /usr/local/bin

# Verify installation
which bctl
bctl --version

# Configuration file location
~/.config/pgbalancer/bctl.conf

Check Cluster Status

View overall cluster status and health:

Overall Status

# Show cluster status (table format - default)
bctl status

# JSON format for scripting
bctl status --format json | jq

Health Check

# Quick health check
bctl health

# Exit code 0 = healthy, non-zero = unhealthy
# Useful in monitoring scripts

if bctl health --quiet; then
    echo "Cluster is healthy"
else
    echo "Cluster has issues!"
    bctl health --verbose
fi

Node Management

Manage backend nodes using bctl:

List Nodes

# List all backend nodes (table format)
bctl nodes list

# Get detailed info about node 0
bctl nodes info 0

Node Operations

# Detach node for maintenance
bctl nodes detach 2

# Attach node back
bctl nodes attach 2

# Promote standby to primary
bctl nodes promote 1 --force

Pool Management

Monitor and manage connection pools:

View Pool Processes

# List all pool processes
bctl pool processes

Pool Statistics

# Get pool statistics
bctl pool stats

Watchdog Management

Monitor watchdog status and coordination:

Watchdog Status

# View watchdog status
bctl watchdog status

Configuration and Logs

View configuration and logs:

View Configuration

# Show current configuration
bctl config show

# Get specific parameter
bctl config get pool_mode

View Logs

# Tail pgBalancer logs
bctl logs --follow

# Filter by level
bctl logs --level error --tail 50

# Search logs
bctl logs --grep "failover" --tail 100

Command Reference

CommandDescriptionExample
statusShow cluster statusbctl status
healthQuick health checkbctl health
nodes listList all nodesbctl nodes list
nodes infoGet node detailsbctl nodes info 0
nodes detachDetach nodebctl nodes detach 2
nodes attachAttach nodebctl nodes attach 2
nodes promotePromote to primarybctl nodes promote 1
pool processesList pool processesbctl pool processes
pool statsPool statisticsbctl pool stats
watchdog statusWatchdog statusbctl watchdog status
config showShow configurationbctl config show
logsView logsbctl logs --follow

Automation Scripts

Health Check Script

#!/bin/bash
# Monitor cluster health

if ! bctl health --quiet; then
    echo "⚠️  Cluster unhealthy!"
    
    # Get node status
    bctl nodes list --format json | jq -r '.nodes[] | select(.status=="down") | "Node \(.node_id) DOWN: \(.hostname)"'
    
    # Send alert
    curl -X POST https://alerts.example.com/webhook \
      -d '{"text": "pgBalancer cluster has issues"}'
    
    exit 1
fi

echo "✓ Cluster healthy"
exit 0

Pool Utilization Alert

#!/bin/bash
# Alert on high pool utilization

UTILIZATION=$(bctl pool stats --format json | jq -r '.utilization_percent')

if (( ${UTILIZATION%.*} > 90 )); then
    echo "⚠️  High pool utilization: $UTILIZATION%"
    # Send alert
fi

Best Practices

✓ DO

  • • Use --format json for scripting and automation
  • • Use --format table for human-readable output
  • • Check exit codes in scripts (bctl health --quiet)
  • • Use --verbose for troubleshooting
  • • Set environment variables for default host/port

✗ DON'T

  • • Don't parse table format output in scripts (use JSON)
  • • Don't run promote without understanding impact
  • • Don't detach nodes during peak traffic
  • • Don't ignore error messages and exit codes