Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Install Postgres on removable volume on linux?

Tags:

postgresql

Cloud platforms like Linode.com often provide hot-pluggable storage volumes that you can easily attach and detach from a Linux virtual machine without restarting it.

I am looking for a way to install Postgres so that its data and configuration ends up on a volume that I have mounted to the virtual machine. The end result should allow me to shut down the machine, detach the volume, spin up another machine with an identical version of Postgres already installed, attach the volume and have Postgres work just like it did on the old machine with all the data, file system permissions and server-wide configuration intact.

Is such a thing possible? Is there a reliable way to move installations (i.e databases and configuration, not the actual binaries) of Postgres across machines?

CLARIFICATION: the virtual machine has two disks:

  • the "built-in" one which is created when the VM is created and mounted to /. That's where Postgres gets installed to and you can't move this disk.
  • the hot-pluggable disk which you can easily attach and detach from a running VM. This is where I want Postgres data and configuration to be so I can just detach the disk (after shutting down the VM to prevent data loss/corruption) and attach it to another VM when I want my data to move so it behaves like it did on the old VM (i.e. no failures to start Postgres, no errors about permissions or missing files, etc).
like image 493
Eric Mutta Avatar asked Apr 24 '26 05:04

Eric Mutta


1 Answers

This works just fine. It is not really any different to starting and stopping PostgreSQL and not removing the disk. There are a couple of things to consider though.

  1. You have to make sure it is stopped + writing synced before unmounting the volume. Obvious enough, and I can't believe you'd be able to unmount before sync completed, but worth repeating.

  2. You will want the same version of PostgreSQL, probably on the same version of operating system with the same locales too. Different distributions might compile it with different options.

  3. Although you can put configuration and data in the same directory hierarchy, most distros tend to put config in /etc. If you compile from source yourself this won't be a problem. Alternatively, you can usually override the default locations or, and this is probably simpler, bind-mount the data and config directories into the places your distro expects.

Note that if your storage allows you to connect the same volume to multiple hosts in some sort of "read only" mode that won't work.

Edit: steps from comment moved into body for easier reading.

  1. start up PG, create a table put one row in it.
  2. Stop PG.
  3. Mount your volume at /mnt/db
  4. rsync /var/lib/postgresql/NN/main to /mnt/db/pg_data and /etc/postgresql/NN/main to /mnt/db/pg_etc
  5. rename /var/lib/postgresql/NN/main and add .OLD to the name and do the same with the /etc
  6. bind-mount the dirs from /mnt to replace them
  7. restart PG
  8. Test
  9. Repeat
  10. Return to step 8 until you are happy
like image 72
Richard Huxton Avatar answered Apr 26 '26 01:04

Richard Huxton



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!