Managing PostgreSQL Component Services
Overview
The Pigsty PGSQL module has multiple components. Each component runs as a systemd service on nodes, except pgBackRest.
To maintain production PostgreSQL clusters, learn how these components work and how to manage them.
| Component | Port | Service Name | Description |
|---|---|---|---|
| Patroni | 8008 | patroni | HA manager, manages PostgreSQL lifecycle |
| PostgreSQL | 5432 | postgres | Placeholder service, not used, for emergency |
| Pgbouncer | 6432 | pgbouncer | Connection pooling middleware, traffic entry |
| PgBackRest | - | - | pgBackRest has no daemon service |
| HAProxy | 543x | haproxy | Load balancer, exposes database services |
| pg_exporter | 9630 | pg_exporter | PostgreSQL metrics exporter |
| pgbouncer_exporter | 9631 | pgbouncer_exporter | Pgbouncer metrics exporter |
| vip-manager | - | vip-manager | Optional, manages L2 VIP address floating |
Do not manage the PostgreSQL service directly with systemctl.
Patroni manages PostgreSQL. Use patronictl commands.
Direct PostgreSQL operations can make Patroni state inconsistent and trigger an unexpected failover.
Use the postgres service only for emergencies when Patroni fails.
Quick Reference
| Operation | Command |
|---|---|
| Start | systemctl start <service> |
| Stop | systemctl stop <service> |
| Restart | systemctl restart <service> |
| Reload | systemctl reload <service> |
| Status | systemctl status <service> |
| Logs | journalctl -u <service> -f |
| Enable | systemctl enable <service> |
| Disable | systemctl disable <service> |
Common service names: patroni, pgbouncer, haproxy, pg_exporter, pgbouncer_exporter, vip-manager
Patroni
Patroni manages PostgreSQL high availability. It controls startup, shutdown, failure detection, and automatic failover. It is the core component of the PGSQL module and manages the PostgreSQL process. Do not manage the postgres service directly with systemctl.
Start Patroni
After startup, Patroni automatically starts PostgreSQL. On the first startup, the instance role determines the action:
- Primary: Initialize or recover the data directory.
- Replica: Clone data from the primary and establish replication.
Stop Patroni
Stopping Patroni shuts down PostgreSQL gracefully. If the instance is the primary and automatic failover is active, this operation may trigger a failover.
Restart Patroni
A restart causes a short service interruption.
For production clusters, use pg restart for a rolling restart.
Reload Patroni
A reload reads the configuration file again. It applies parameters that PostgreSQL can change without a restart.
View Status & Logs
Config file: /etc/patroni/patroni.yml
Best Practice: Use
patronictlinstead of systemctl to manage PostgreSQL clusters.
Pgbouncer
Pgbouncer provides lightweight PostgreSQL connection pooling. Business traffic usually goes through Pgbouncer on port 6432, rather than directly to PostgreSQL on port 5432. This reuses connections and protects the database.
Start Pgbouncer
Stop Pgbouncer
Note: Stopping Pgbouncer disconnects all pooled business connections.
Restart Pgbouncer
A restart disconnects all existing connections.
To apply only configuration changes, use reload.
Reload Pgbouncer
A reload reads configuration files, such as user lists and pool parameters, again. It does not disconnect existing connections.
View Status & Logs
Config files:
- Main config:
/etc/pgbouncer/pgbouncer.ini - HBA rules:
/etc/pgbouncer/pgb_hba.conf - User list:
/etc/pgbouncer/userlist.txt - Database list:
/etc/pgbouncer/database.txt
Admin Console
Common admin commands:
HAProxy
HAProxy is a high-performance load balancer. It routes traffic to the correct PostgreSQL instances. Pigsty uses HAProxy to expose services. HAProxy routes traffic by instance role (primary or replica) and health status.
Start HAProxy
Stop HAProxy
Note: Stopping HAProxy disconnects all load-balanced connections.
Restart HAProxy
Reload HAProxy
HAProxy can reload without disconnecting existing connections.
Use reload to apply configuration changes.
View Status & Logs
Config files: The main configuration is /etc/haproxy/haproxy.cfg.
Pigsty stores generated service fragments in /etc/haproxy/conf.d/.
Admin Interface
HAProxy provides a web admin interface, default port 9101:
The default administration username is admin.
Set its password with haproxy_admin_password.
pg_exporter
pg_exporter exports PostgreSQL performance metrics for Prometheus.
Start pg_exporter
Stop pg_exporter
After you stop the exporter, Prometheus cannot collect PostgreSQL metrics from this instance.
Restart pg_exporter
View Status & Logs
Config file: /etc/pg_exporter.yml
Verify Metrics
pgbouncer_exporter
pgbouncer_exporter is Pgbouncer’s Prometheus metrics exporter.
Start/Stop/Restart
View Status & Logs
Verify Metrics
vip-manager
vip-manager is an optional component that moves an L2 VIP between nodes.
When pg_vip_enabled is enabled, vip-manager binds the VIP to the current primary node.
Start vip-manager
Stop vip-manager
After you stop vip-manager, it releases the VIP address from the current node.
Restart vip-manager
View Status & Logs
Config file: /etc/default/vip-manager
Verify VIP Binding
Startup Order & Dependencies
Recommended PGSQL module component startup order:
Stop the components in reverse order. Pigsty playbooks handle these dependencies automatically.
Batch Start All Services
Batch Stop All Services
Common Troubleshooting
Service Startup Failure
Patroni Won’t Start
| Symptom | Possible Cause | Solution |
|---|---|---|
| Can’t connect to etcd | etcd cluster unavailable | Check etcd service status |
| Data dir permission error | File ownership not postgres | chown -R postgres:postgres /pg/data |
| Port in use | Leftover PostgreSQL process | pg_ctl stop -D /pg/data or kill |
Pgbouncer Won’t Start
| Symptom | Possible Cause | Solution |
|---|---|---|
| Config syntax error | INI format error | Check /etc/pgbouncer/pgbouncer.ini |
| Port in use | Port 6432 already used | lsof -i :6432 |
| userlist.txt permissions | Incorrect file permissions | chmod 600 /etc/pgbouncer/userlist.txt |
HAProxy Won’t Start
| Symptom | Possible Cause | Solution |
|---|---|---|
| Config syntax error | Invalid main configuration or service fragment | haproxy -Ws -f /etc/haproxy/haproxy.cfg -f /etc/haproxy/conf.d -c -q |
| Port in use | Service port conflict | lsof -i :5433 |
Related Documentation
- Patroni Management: Manage PostgreSQL HA with patronictl
- Cluster Management: Create, scale, destroy clusters
- Service Configuration: HAProxy service definition and config
- Monitoring System: PostgreSQL monitoring and alerting