HBA Rules
Overview
HBA (Host-Based Authentication) controls “who can connect to the database, from where, and how”. See Authentication for the authentication model and default rules.
Pigsty manages HBA rules declaratively through pg_default_hba_rules and pg_hba_rules.
Pigsty renders the following config files during cluster init or HBA refresh:
| Config File | Path | Description |
|---|---|---|
| PostgreSQL HBA | /pg/data/pg_hba.conf |
PostgreSQL server HBA rules |
| PgBouncer HBA | /etc/pgbouncer/pgb_hba.conf |
Connection pool HBA rules |
HBA rules are controlled by these parameters:
| Parameter | Level | Description |
|---|---|---|
pg_default_hba_rules |
G | PostgreSQL global default HBA |
pg_hba_rules |
G/C/I | PostgreSQL cluster/instance add |
pgb_default_hba_rules |
G | PgBouncer global default HBA |
pgb_hba_rules |
G/C/I | PgBouncer cluster/instance add |
Rule features:
- Role filtering: Rules support
rolefield, auto-filter based on instance’spg_role - Order sorting: Rules support
orderfield, controls position in final config file - Two syntaxes: Supports alias form (simplified) and raw form (direct HBA text)
Refresh HBA
After modifying config, re-render config files and reload services:
Script executes the following playbook:
PostgreSQL only: ./pgsql.yml -l <cls> -t pg_hba,pg_reload -e pg_reload=true
PgBouncer only: ./pgsql.yml -l <cls> -t pgbouncer_hba,pgbouncer_reload
Don’t directly edit /pg/data/pg_hba.conf or /etc/pgbouncer/pgb_hba.conf - they’ll be overwritten on next playbook run.
All changes should be made in pigsty.yml, then execute bin/pgsql-hba to refresh.
Parameter Details
pg_default_hba_rules
PostgreSQL global default HBA rule list, usually defined in all.vars, provides base access control for all clusters.
- Type:
rule[], Level: Global (G)
pg_hba_rules
PostgreSQL cluster/instance-level additional HBA rules, can override at cluster or instance level, merged with default rules and sorted by order.
- Type:
rule[], Level: Global/Cluster/Instance (G/C/I), Default:[]
pgb_default_hba_rules
PgBouncer global default HBA rule list, usually defined in all.vars.
- Type:
rule[], Level: Global (G)
pgb_hba_rules
PgBouncer cluster/instance-level additional HBA rules.
- Type:
rule[], Level: Global/Cluster/Instance (G/C/I), Default:[]
Note: PgBouncer HBA does not support
db: replication.
Rule Fields
Each HBA rule is a YAML dict supporting these fields:
| Field | Type | Required | Default | Description |
|---|---|---|---|---|
user |
string | No | all |
Username, supports all, placeholders, +rolename |
db |
string | No | all |
Database name, supports all, replication, db name |
addr |
string | Yes* | - | Address alias or CIDR, see Address Aliases |
auth |
string | No | pwd |
Auth method alias, see Auth Methods |
title |
string | No | - | Rule description, rendered as comment in config |
role |
string | No | common |
Instance role filter, see Role Filtering |
order |
int | No | 1000 |
Sort weight, lower first, see Order Sorting |
rules |
list | Yes* | - | Raw HBA text lines, mutually exclusive with addr |
Either
addrorrulesmust be specified. Userulesto write raw HBA format directly.
Address Aliases
Pigsty provides address aliases to simplify HBA rule writing:
| Alias | Expands To | Description |
|---|---|---|
local |
Unix socket | Local Unix socket |
localhost |
Unix socket + 127.0.0.1/32 + ::1/128 |
Loopback addresses |
admin |
${admin_ip}/32 |
Admin IP address |
infra |
All infra group node IPs | Infrastructure nodes |
cluster |
All current cluster member IPs | Same cluster instances |
intra / intranet |
10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16 |
Intranet CIDRs |
world / all |
0.0.0.0/0 + ::/0 |
Any address (IPv4 + IPv6) |
<CIDR> |
Direct use | e.g., 192.168.1.0/24 |
Intranet CIDRs can be customized via node_firewall_intranet:
Auth Methods
Pigsty provides auth method aliases for simplified config:
| Alias | Actual Method | Connection Type | Description |
|---|---|---|---|
pwd |
scram-sha-256 or md5 |
host |
Auto-select based on pg_pwd_enc |
ssl |
scram-sha-256 or md5 |
hostssl |
Force SSL + password |
ssl-sha |
scram-sha-256 |
hostssl |
Force SSL + SCRAM-SHA-256 |
ssl-md5 |
md5 |
hostssl |
Force SSL + MD5 |
cert |
cert |
hostssl |
Client certificate auth |
trust |
trust |
host |
Unconditional trust (dangerous) |
deny / reject |
reject |
host |
Reject connection |
ident |
ident |
host |
OS user mapping (PostgreSQL) |
peer |
peer |
local |
OS user mapping (PgBouncer/local) |
pg_pwd_encdefaults toscram-sha-256, can be set tomd5for legacy client compatibility.
User Variables
HBA rules support these user placeholders, auto-replaced with actual usernames during rendering:
| Placeholder | Default | Corresponding Param |
|---|---|---|
${dbsu} |
postgres |
pg_dbsu |
${repl} |
replicator |
pg_replication_username |
${monitor} |
dbuser_monitor |
pg_monitor_username |
${admin} |
dbuser_dba |
pg_admin_username |
Role Filtering
The role field in HBA rules controls which instances the rule applies to:
| Role | Description |
|---|---|
common |
Default, applies to all instances |
primary |
Primary instance only |
replica |
Replica instance only |
offline |
Offline instance only (pg_role: offline or pg_offline_query: true) |
standby |
Standby instance |
delayed |
Delayed replica instance |
Role filtering matches based on instance’s pg_role variable. Non-matching rules are commented out (prefixed with #).
Order Sorting
PostgreSQL HBA is first-match-wins, rule order is critical. Pigsty controls rule rendering order via the order field.
Order Interval Convention
| Interval | Usage |
|---|---|
0 - 99 |
User high-priority rules (before all defaults) |
100 - 650 |
Default rule zone (spaced by 50 for insertion) |
1000+ |
User rule default (rules without order) |
PostgreSQL Default Rules Order
| Order | Rule Description |
|---|---|
| 100 | dbsu local ident |
| 150 | dbsu replication local |
| 200 | replicator localhost |
| 250 | replicator intra replication |
| 300 | replicator intra postgres |
| 350 | monitor localhost |
| 400 | monitor infra |
| 450 | admin infra ssl |
| 500 | admin world ssl |
| 550 | dbrole_readonly localhost |
| 600 | dbrole_readonly intra |
| 650 | dbrole_offline intra |
PgBouncer Default Rules Order
| Order | Rule Description |
|---|---|
| 100 | dbsu local peer |
| 150 | all localhost pwd |
| 200 | monitor pgbouncer intra |
| 250 | monitor world deny |
| 300 | admin intra pwd |
| 350 | admin world deny |
| 400 | all intra pwd |
Syntax Examples
Alias Form: Using Pigsty simplified syntax
Rendered result:
Raw Form: Using PostgreSQL HBA syntax directly
Rendered result:
Common Scenarios
Blacklist IP: Use order: 0 to ensure first match
Whitelist App Server: High priority for specific IP
Admin Force Certificate: Override default SSL password auth
Offline Instance Dedicated Network: Only on offline instances
Restrict Access by Database: Sensitive databases limited to specific networks
PgBouncer Dedicated Rules: Note no db: replication support
Complete Cluster Example
Verification & Troubleshooting
View Current HBA Rules
Test Connection Auth
Common Issues
| Error Message | Possible Cause | Solution |
|---|---|---|
no pg_hba.conf entry for host... |
No matching HBA rule | Add corresponding rule and refresh |
password authentication failed |
Wrong password or enc | Check password and pg_pwd_enc |
| Rule not taking effect | Not refreshed or order | Run bin/pgsql-hba, check order |
Important Notes
- Order sensitive: PostgreSQL HBA is first-match-wins, use
orderwisely - Role matching: Ensure
rolefield matches target instance’spg_role - Address format: CIDR must be correct, e.g.,
10.0.0.0/8not10.0.0.0/255.0.0.0 - PgBouncer limitation: Does not support
db: replication - TLS prerequisite:
sslandcertrequire server-side TLS; clients must still useverify-fullto authenticate the server - Test first: Validate in test environment before modifying HBA
- Refresh on scale: Rules using
addr: clusterneed refresh after cluster membership changes
Related Documentation
- HBA Management: Refresh, verification, and troubleshooting
- User Config: User and role configuration
- Access Control: Role system and permission model
- Authentication: HBA, SCRAM, client certificates, and default rules
- Encrypted Communication: TLS and server certificate verification