PostgreSQL Clusters

Back to SIG Tooling & Set Up Tutorials

Why clusters?

1. Your DBs are sandboxed so you cant accidently drop the wrong DB.

2. Some DB UI applications (pgadmin4) only store one password per server. This means you cant easily log into different DBs.

3. You can have two copies of the same db. Say a copy that you are doing major changes to, and a main branch copy. You can swap between them to do work on either branch easily.

The follow assumes PG version 12. If different update all the 12 to current version.

To list existing

pg_lsclusters

To switch between clusters you have stop the old one, then start the new one.

sudo pg_ctlcluster 12 ceo stop
sudo pg_ctlcluster 12 ceo_alt start

Alternatively you can stop the entire postgres process and then start the new one.

sudo systemctl stop postgresql
sudo pg_ctlcluster 12 ceo_alt start

To create a new cluster stop all clusters and run

sudo systemctl stop postgresql
sudo pg_createcluster 12 [clust_name] --port 5432 --start-conf=manual --local=en_US.UTF-8 -- --auth-host=trust --auth-local=trust

Then run `build-db build-all` to initialize the database. You can run `build-db restore` after initializing. You must initialize first because the back up does not contain roles.

I recommend updating or replacing the cluster main if using multiple. main is set to start automatically, and the script above to add new clusters is not. This way when you boot, you can just start the one you need. You can add the start command to scripts for switching projects.