On Debian-based systems like Ubuntu, PostgreSQL uses pg_ctlcluster
for cluster management, which is part of the postgresql-common
package. The service management is handled through systemd, with the main service file located at /lib/systemd/system/postgresql.service
.
First, list all available PostgreSQL clusters:
pg_lsclusters
Output example:
Ver Cluster Port Status Owner Data directory Log file
12 main 5432 online postgres /var/lib/postgresql/12/main /var/log/postgresql/postgresql-12-main.log
13 main 5433 down postgres /var/lib/postgresql/13/main /var/log/postgresql/postgresql-13-main.log
To prevent a specific cluster from starting automatically while keeping it installed:
- Create a systemd override file for the cluster:
sudo systemctl edit postgresql@<version>-<cluster_name>
For example, for PostgreSQL 12's main cluster:
sudo systemctl edit postgresql@12-main
Add these lines to the override file:
[Unit]
RefuseManualStart=yes
[Service]
ExecStart=
ExecStart=/bin/true
Check the service status:
systemctl status postgresql@12-main
The output should show the service as masked or inactive.
You can also manually control cluster startup:
sudo pg_ctlcluster 12 main stop
sudo -u postgres pg_dropcluster --stop 12 main
To temporarily prevent auto-start while keeping the cluster:
sudo pg_ctlcluster 12 main disable
When working with multiple clusters, you can specify which one to start:
sudo pg_ctlcluster 13 main start
To make your new cluster the default (starting on port 5432):
sudo pg_renamecluster 13 main main_new
sudo pg_renamecluster 12 main main_old
Before making these changes:
# Backup the cluster
sudo -u postgres pg_dumpall -c -f /path/to/backup.sql
# Verify the backup
ls -lh /path/to/backup.sql
To reactivate a disabled cluster later:
sudo systemctl unmask postgresql@12-main
sudo systemctl enable postgresql@12-main
When working with PostgreSQL on Ubuntu, you might encounter situations where you need to maintain an existing cluster (with potentially incorrect configurations) while creating a new properly configured cluster. A common case is when the original cluster was created with wrong locale settings like en_GB.UTF-8
when you actually need C
collation.
First, check your existing clusters:
pg_lsclusters
This will display output similar to:
Ver Cluster Port Status Owner Data directory Log file
12 main 5432 online postgres /var/lib/postgresql/12/main /var/log/postgresql/postgresql-12-main.log
Create a new cluster with correct parameters:
sudo pg_createcluster 12 newcluster --locale=C --start
Verify the new cluster is running:
pg_lsclusters
To prevent the old cluster from auto-starting while keeping it installed:
sudo pg_ctlcluster 12 main stop
sudo systemctl disable postgresql@12-main
The postgresql
service is actually a meta-service that starts all enabled clusters. To control individual clusters:
sudo systemctl list-unit-files | grep postgresql
To start/stop specific clusters:
# Start only the new cluster
sudo systemctl start postgresql@12-newcluster
# Start all enabled clusters (excluding our disabled main cluster)
sudo service postgresql start
After making these changes:
# Check cluster status
pg_lsclusters
# Verify systemd configuration
systemctl is-enabled postgresql@12-main
- The old cluster remains fully intact in its data directory
- You can manually start it with
sudo systemctl start postgresql@12-main
when needed - All configuration files remain unchanged
- This method preserves the ability to quickly revert if needed
You can also manage clusters directly:
# Disable auto-start at the PostgreSQL level
sudo pg_ctlcluster 12 main disable
# Re-enable when needed
sudo pg_ctlcluster 12 main enable