PostgreSQL on our VPS.

Installing PostgreSQL on a Linux server

As a user without administrator privileges

Date:

These days I've been reviewing alternatives for note management systems that I could install on my VPS, such as dnote, which has PostgreSQL database as a dependency.

Since I lack permissions to install PostgreSQL on the VPS, I had to look for an alternative. In this case, I decided to compile a binary on a "donor machine" and then transfer the necessary files to the target machine.

Reviewing system compatibility

It's crucial that the operating system of the donor machine is as similar as possible to the system on the target machine. This is to avoid compatibility issues with the system libraries.

For example: A version of binaries compiled on Manjaro could end up having references to system libraries fundamentally different from those of a target Ubuntu Server 22.04 LTS system. These differences can cause compatibility issues and make PostgreSQL not work correctly on the target machine.

For best results and to help ensure that libraries and dependencies are compatible between both systems, our donor machine should meet the following characteristics:

  • The same operating system as the target machine.
  • A version as close as possible to that of the target machine.
  • A similar update level to that of the target machine.

Preparation on the Donor Machine

After reviewing the operating system version of our VPS, we create a clean installation of Ubuntu Server 22.04 LTS on a virtual machine. As we're going to use it for compiling, we need to install the build-essential package which contains the necessary packages for compiling.

bash

> sudo apt-get install build-essential

We download the package of the PostgreSQL version we require on our donor virtual machine, in this case 16.3, then we decompress and enter inside. 1

bash

> wget https://ftp.postgresql.org/pub/source/v16.3/postgresql-16.3.tar.gz > tar -xzvf postgresql-16.3.tar.gz > cd postgresql-16.3

PostgreSQL in the 'INSTALL' file has a list of dependencies that are required to be compiled/function. To verify if we have the necessary libraries, we can check them on the target machine with ldconfig -p | grep library_name. If nothing appears, it means that the library doesn't exist or isn't installed. This process needs to be repeated for other necessary libraries.

The required libraries in their development version are for compiling, such as libreadline, so it's necessary to install them on our donor machine.

In this case, and taking the configuration options for compilation 2

bash

> ./configure --prefix=/home/user/postgresql --exec-prefix=/home/user/postgresql --without-icu --without-zlib

We run make to compile, which can take some time depending on the machine's resources

bash

> make

And when finished, we just run make install, which will place the PostgreSQL binary files in the directory we specified as the prefix option

bash

> make install

From here we can copy the content of this folder to our server with local permissions and try to run them there, or we can test them on our donor machine.

Running our binaries

With our binaries in the directory, we need to update our "path" so it knows where our binaries reside on the machine, as well as our "library path" so it can locate the libraries that PostgreSQL uses to function. Once we make sure everything works, we can place these commands at the end of our .bashrc (or your shell's configuration file) so we don't have to execute them when starting each session.

bash

> export PATH="$PATH:/home/user/postgresql/lib" > export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:/home/user/postgresql/lib"

If all this was successful, we should be able to follow the rest of the steps to initialize the PostgreSQL server. Although it's possible that if it indicates a missing library, we could just copy it in most cases from our donor machine to the directory we specified in the "library path". However, if the quantity is large, it would be better to try repeating the configuration without an option or seek a better emulation of our local environment.

[1] The PostgreSQL source code can be found here

[2] The detailed configuration options can be found here.