1. MagicESP
  2. Ubuntu Server 22.04
  3. PostgreSQL
  4. Move PostgreSQL data files, change cluster location

Move PostgreSQL data files, change cluster location

Show PostgreSQL Data Clusters

pg_lsclusters

Move Clusters to a New Path, Keep All Data

copy the data files

sudo service postgresql stop

sudo mkdir -p {{ path.value }}/12
sudo chown -R postgres:postgres {{ path.value }}
sudo cp -R /var/lib/postgresql/12/main {{ path.value }}/12/
sudo mv /var/lib/postgresql/12/main /var/lib/postgresql/12/main_old

change the config

sudo {{ texteditor.value }} /etc/postgresql/10/main/postgresql.conf
data_directory = '{{ path.value }}/12/main'

start postgres and test

sudo service postgresql start
pg_lsclusters

delete old data files

sudo rm -rf /var/lib/postgresql/12/main_old

Change Clusters to a New Path, LOSE ALL THE DATA

ALL THE DATA WILL BE LOSE!!! {class=red-text}

sudo mkdir -p {{ path.value }}/12/main
sudo chown -R postgres:postgres {{ path.value }}
 
sudo pg_dropcluster --stop 12 main
sudo pg_createcluster -d {{ path.value }}/12/main --locale en_US.UTF-8 --start 12 main
pg_lsclusters