Managing PostgreSQL HBA Rules
Quick Start
Pigsty uses declarative management: first define HBA rules in the inventory, then use bin/pgsql-hba <cls> to refresh.
For rule syntax, see HBA Configuration. For authentication methods, default boundaries, and credential management, see Authentication.
| Action | Description | Risk |
|---|---|---|
| Refresh HBA Rules | Re-render config files and reload service | Low |
| Verify HBA Rules | View current rules, test connection auth | Read |
| Common Scenarios | Add rules, block IP, role-based, post-expansion | Low |
| Troubleshooting | Connection rejected, auth failed, rules not applied | - |
| Pgbouncer HBA | Pgbouncer connection pool HBA management | Low |
Refresh HBA Rules
After modifying HBA rules in pigsty.yml, re-render config files and reload services.
Result: Renders PostgreSQL and Pgbouncer HBA config files based on inventory definitions, then reloads services to apply.
Config file locations
| Service | Config File Path | Template File |
|---|---|---|
| PostgreSQL | /pg/data/pg_hba.conf |
roles/pgsql/templates/pg_hba.conf |
| Pgbouncer | /etc/pgbouncer/pgb_hba.conf |
roles/pgsql/templates/pgbouncer.hba |
Directly editing /pg/data/pg_hba.conf or /etc/pgbouncer/pgb_hba.conf works temporarily, but will be overwritten next time Ansible playbook runs. All HBA rule changes should be in pigsty.yml, then execute bin/pgsql-hba to refresh.
Related Tags
| Tag | Description |
|---|---|
pg_hba |
Render PostgreSQL HBA config file |
pg_reload |
Reload PostgreSQL config (needs pg_reload=true) |
pgbouncer_hba |
Render Pgbouncer HBA config file |
pgbouncer_reload |
Reload Pgbouncer config |
Verify HBA Rules
After refreshing HBA rules, verify config is correctly applied.
View current HBA rules
Check HBA config syntax
Common Scenarios
Add New HBA Rule
Add rule to cluster config’s pg_hba_rules, then refresh:
Emergency IP Block
When detecting malicious IP, add high-priority (order: 0) deny rule:
Role-Based Rules
Configure different HBA rules for primary and replica using role parameter:
After refresh, rules auto-enable/disable based on instance’s pg_role.
Refresh HBA After Expansion
When cluster adds new instances, rules using addr: cluster need refresh to include new members:
Refresh HBA After Failover
After Patroni failover, instance pg_role may not match config. If HBA rules use role filtering, update config and refresh:
Troubleshooting
Connection Rejected
Symptom: FATAL: no pg_hba.conf entry for host "x.x.x.x", user "xxx", database "xxx"
Steps:
- Check current HBA rules, confirm if matching rule exists:
-
Confirm client IP, username, database matches any rule
-
Check rule order (HBA uses first-match-wins)
-
Add corresponding rule and refresh:
Authentication Failed
Symptom: FATAL: password authentication failed for user "xxx"
Steps:
- Confirm password is correct
- Check password encryption method (
pg_pwd_enc) compatibility with client - Check if user exists:
HBA Rules Not Applied
Steps:
- Confirm refresh command was executed
- Check if Ansible execution succeeded
- Confirm PostgreSQL reloaded:
- Check if config file was updated:
Rule Order Issues
HBA uses first-match-wins. If rules not working as expected:
- Check
ordervalues in rule definitions - Use
psql -c "TABLE pg_hba_file_rules"to view actual order - Adjust
ordervalues (lower numbers = higher priority)
Pgbouncer HBA
Pgbouncer HBA management is similar to PostgreSQL, with some differences.
Config differences
| Difference | PostgreSQL | Pgbouncer |
|---|---|---|
| Config file | /pg/data/pg_hba.conf |
/etc/pgbouncer/pgb_hba.conf |
| Replication | Supports db: replication |
Not supported |
| Local auth | Uses ident |
Uses peer |
Refresh Pgbouncer HBA
Best Practices
- Always manage in config files: Don’t edit
pg_hba.confdirectly - all changes throughpigsty.yml - Test environment first: HBA changes can cause connection issues - verify in test env first
- Use order for priority: Blocklist rules use
order: 0to ensure priority matching - Refresh promptly: Refresh HBA after adding/removing instances or failover
- Principle of least privilege: Only open necessary access - avoid
addr: world+auth: trust - Monitor auth failures: Watch for auth failures in
pg_stat_activity - Backup config: Backup
pigsty.ymlbefore important changes
Related Documentation
- HBA Configuration: HBA rule config syntax and parameter details
- User Management: User and role management operations
- Access Control: Role system and permission model
- Authentication: Authentication methods, default boundaries, and credential management
- Encrypted Communication: TLS and client certificate verification