Managing PostgreSQL HBA Rules
Quick Start
Pigsty manages HBA rules declaratively.
First define HBA rules in the inventory.
Then run bin/pgsql-hba <cls> to refresh them.
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 you change HBA rules in pigsty.yml, generate the configuration files again and reload the services.
Result: Pigsty generates PostgreSQL and Pgbouncer HBA configuration files from inventory definitions. It then reloads the services to apply the rules.
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 |
Direct changes to /pg/data/pg_hba.conf or /etc/pgbouncer/pgb_hba.conf work temporarily.
The next Ansible playbook run overwrites them.
Make all HBA rule changes in pigsty.yml, then run bin/pgsql-hba to refresh the rules.
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 you refresh HBA rules, verify that the configuration applies correctly.
View current HBA rules
Check HBA config syntax
Common Scenarios
Add New HBA Rule
Add the rule to pg_hba_rules in the cluster configuration.
Then refresh the rules:
Emergency IP Block
If you detect a malicious IP address, add a deny rule with high priority (order: 0):
Role-Based Rules
Configure different HBA rules for primary and replica using role parameter:
After a refresh, the instance pg_role determines which rules are enabled.
Refresh HBA After Expansion
After you add cluster instances, refresh rules that use addr: cluster.
The refreshed rules include the new members:
Refresh HBA After Failover
After a Patroni failover, instance roles may differ from pg_role values in the configuration.
If HBA rules filter by role, update the configuration and refresh the rules:
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 the first matching rule. If rules do not work 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
- Manage rules in the inventory: Make all changes in
pigsty.yml. Do not editpg_hba.confdirectly. - Test first: Verify HBA changes in a test environment first. Changes can cause connection problems.
- Set rule priority: Use
order: 0for blocklist rules to match them first. - Refresh promptly: Refresh HBA after you add or remove instances, or after failover.
- Grant minimum access: Allow only necessary access. Avoid
addr: worldwithauth: trust. - Monitor authentication failures: Check
pg_stat_activityfor authentication failures. - Back up configuration: Back up
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
Was this page helpful?
Thanks—your feedback helps us improve this page.
What got in the way? (optional)