1
$ df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda5        33G   31G  467M  99% /var
/dev/sda6       120G   22G   93G  19% /var/lib/postgresql

I'd like to merge them into single /var partition (without losing data)

I can shut it down for a while

eugene
  • 183
  • 1
  • 5
  • 1
    You have to copy the data from one to the other but you can’t fit 22G to 467M. Do you have any spare partition or disk where you can fit the data temporarily? Is it OK for you to shut down PostgreSQL while manipulating its data (maybe for several hours)? – Melebius Jul 02 '19 at 06:30
  • @Melebius yes i can shut it down , i can find spare space (partition) – eugene Jul 02 '19 at 06:32

1 Answers1

1
  1. Check that the partition /dev/sda6 is located right after /dev/sda5, for example using GParted.

  2. Backup! Backup!! Backup!! (Make 3 backups)

    Working with partitions is dangerous. Make sure you have made backups and copies of important data in external media. Avoid making backups in the same disk you are going to work on.

    Source: https://askubuntu.com/a/1005841/250300

  3. Remove (or comment out) the /dev/sda6 entry in the /etc/fstab file, so it’s not looked for during the following boot anymore.

  4. Boot your live medium now. You could only shut down PostgreSQL (generally: anything that could modify the data while copying them) for the next step but anyway, we’ll have to boot a live medium in order to resize partitions.

  5. Mount the partition to be deleted. Note that the device letter (a in /dev/sda) might be altered in the live session, so adapt the commands if necessary or mount the partition using GUI.

    export p=sda6
    sudo mkdir /media/$p
    sudo mount /dev/$p /media/$p
    
  6. Copy all your data from the partition to another place. You can use cp, rsync or tar (with or without compression). I’d use tar with compression as it creates a read-only copy and with today’s equipment, it’s faster to compress big data than to write them uncompressed.

    I’ve chosen /media/data/ as the target location, adapt it to your own.

    I am using sudo since some data in /var/lib/postgresql might not be readable for the logged in user but perhaps it might be not necessary here.

    sudo tar -cJvf /media/data/var-lib-postgresql.txz /media/sda6/
    
  7. Unmount the partition.

    sudo umount /media/sda6
    
  8. Now run GParted, delete the original /dev/sda6 and expand /dev/sda5 to utilize the freed space. Detailed instructions can be found at https://askubuntu.com/a/1005841/250300.

  9. Mount the target partition.

    export p=sda5
    sudo mkdir /media/$p
    sudo mount /dev/$p /media/$p
    
  10. Check the folder /media/sda5/postgresql is empty. If not, clear it as it most likely contains data used before creating /dev/sda6.

  11. Restore your data:

    cd /media/sda5/lib/postgresql && sudo tar -xvf /media/data/var-lib-postgresql.txz
    
  12. Reboot your system and enjoy your newly available free space.

Melebius
  • 11,431
  • 9
  • 52
  • 78