Installing PostgreSQL 14.1 on a VM without a Previous Management Console Installation

This topic describes the steps to install PostgreSQL 14.1 on a host VM operating system that has not had a Management Console installation.

Proper permissions

CentOS/RHEL logged in user performing these change must have sudo privileges

  1. Login to the CentOS/RHEL host VM operating system and create a new folder named postgresql14_dependencies under the logged in users directory.

    cd ~
    mkdir postgresql14_dependencies
    
  2. Copy teradicimc-postgresql14-offline-dependencies.tgz to the <logged in user directory>/postgresql14_dependencies directory.

    You can use a third party tool such as WinSCP to copy and move files into the CentOS/RHEL host operating system.

  3. From the users logged in directory, create a file called post14_install.sh.

    cd ~
    vi post14_install.sh
    
  4. Copy the following script to the post14_install.sh file.

    #!/usr/bin/env bash
    #"""File for install PostgreSQL14.1 before RPM install"""
    
    #Declaring PostgreSQL version as 14
    postgresql_version=14
    #Declaring PostgreSQL minor version as 14.1
    postgresql_minor_version=14.1
    #Assigning first command line argument value to postgresql_dependencies_tarfile variable
    postgresql_dependencies_tarfile=$1
    #Fetching the current directory path
    current_directory_path=$(getent passwd "$SUDO_USER" | cut -d: -f6)
    
    #Validating number of arguments are equal to one and it is not null
    if [ -z "$postgresql_dependencies_tarfile" ] || [ "$#" -ne 1 ];  then
        echo -e "Usage: sudo sh ./post14_install.sh <postgresql_dependencies_tarfile>\nExample: sudo sh ./post14_install.sh teradicimc-postgresql14-offline-dependencies.tgz"
        exit 0
    else
        echo 'INFO: PostgreSQL tarball file name is' "$postgresql_dependencies_tarfile"
    fi
    
    #Validating postgresql_dependencies_tarfile is having PostgreSQL rpm
    tar -tvf "$current_directory_path"/postgresql14_dependencies/"${postgresql_dependencies_tarfile}" | grep -i postgresql > /dev/null
    
    if [ $? -ne 0 ]; then
        echo 'ERROR: Invalid archive, PostgreSQL rpm is missing'
        exit 0
    fi
    
    #Validating postgresql_dependencies_tarfile existing in the specific path
    if [ ! -f "$current_directory_path"/postgresql14_dependencies/"${postgresql_dependencies_tarfile}" ]; then
        echo 'ERROR: '"$postgresql_dependencies_tarfile"' file is missing'
        exit 0
    fi
    
    #Checking if system has PostgreSQL version 14.1 or below
    #If PostgreSQL14.1 exists, skip the installation
    #If PostgreSQL version below 14.1 exists, uninstall the existing PostgreSQL version
    #If PostgreSQL version below 14.1 exists and pcoip_mc_db database exists, skip the installation
    
    echo 'INFO: Searching for the existing PostgreSQL version installed'
    is_postgresql14_exist=$(rpm -qa | grep -i postgresql14-server)
    is_postgresql9_exist=$(rpm -qa | grep -e ^postgresql-server-[0-9]\\. -e ^postgresql-server-[1][0-3] -e ^postgresql-[0-9]\\. -e ^postgresql-[1][0-3] -e ^postgresql[0-9]- -e ^postgresql[1][0-3])
    
    if [ ! -z "$is_postgresql9_exist" ]; then
        echo 'INFO: Found PostgreSQL version below 14.1'
        db_name=pcoip_mc_db
        echo 'INFO: Checking the status of PostgreSQL service'
        is_postgresql9_status=$(systemctl is-active postgresql | grep -w 'inactive')
        if [ ! -z "$is_postgresql9_status" ];then
            echo 'INFO: Starting PostgreSQL service'
            systemctl enable postgresql --now
        fi   
        psql -U postgres ${db_name} --command="SELECT version();" >/dev/null 2>&1
        db_result=$?
        if [ "$db_result" -eq 0 ]; then
            echo 'INFO: Please run update script to upgrade PostgreSQL14.1'
            exit 0
        fi
        echo 'INFO: Uninstalling the existing PostgreSQL'
        rpm -qa | grep -e ^postgresql-libs-[0-9]\\. -e ^postgresql-contrib-[0-9]\\. -e ^postgresql-server-[0-9]\\. -e ^postgresql-[0-9]\\. -e ^postgresql-libs-[1][0-3] -e ^postgresql-contrib-[1][0-3] -e ^postgresql-server-[1][0-3] -e ^postgresql-[1][0-3] -e ^postgresql[0-9]- -e ^postgresql[1][0-3] | xargs sudo rpm -e --nodeps
    else
        echo 'INFO: PostgreSQL version below 14.1 is not found'
    fi
    
    #Checking the availability of postgresql14_dependencies tar file
    #If tar file exists, extracting the file and installing the PostgreSQL14.1
    #If tar file not exists, exit from script execution
    if [ -z "$is_postgresql14_exist" ]; then
        if [ -f "$current_directory_path"/postgresql14_dependencies/"${postgresql_dependencies_tarfile}" ]; then
            echo "INFO: Installing PosttgreSQL14.1 from $current_directory_path/postgresql14_dependencies"
            cd "$current_directory_path"/postgresql14_dependencies && \
            tar xvf "$postgresql_dependencies_tarfile" && \
            yum -y install ./*.rpm
            cd
        else
            echo 'ERROR: '"$postgresql_dependencies_tarfile"' file is missing'
            exit 0
        fi
    else
        echo 'INFO: Found PostgreSQL14.1. Installation is not required'
        exit 0
    fi
    
    #Checking the PostgreSQL version   
    echo 'INFO: Verifying the PostgreSQL version is' "$postgresql_minor_version"
    psql -V | awk {'print $3'} | awk {'print $1'} | grep $postgresql_minor_version
    
    if [ $? -eq 0 ]; then
        echo 'INFO: Checking PostgreSQL path is exists in /etc/profile'
        if [[ $(grep -i /usr/pgsql-$postgresql_version/bin /etc/profile) ]]; then
            echo 'INFO: PostgreSQL path is present in /etc/profile'
        else
            echo 'INFO: PostgreSQL path is not present in /etc/profile'
            echo 'INFO: Exporting PostgreSQL path to /etc/profile'
            echo export 'PATH=$PATH:'/usr/pgsql-${postgresql_version}/bin | sudo tee -a /etc/profile
            echo 'INFO: PostgreSQL14.1 installation is completed'
            echo 'INFO: Removing the postgresql14_dependencies folder'
            rm -rf "$current_directory_path"/postgresql14_dependencies
        fi
    else
        echo 'ERROR: PostgreSQL14.1 installation is failed'
        exit 0
    fi
    
  5. Save the script file and exit vi editor.

    Esc:wq

  6. Give permissions to the post14_install.sh script file to run the script.

    chmod +x ./post14_install.sh

  7. Run the script file.

    sudo sh ./post14_install.sh teradicimc-postgresql14-offline-dependencies.tgz

    Verify install

    Verify the installed PostgreSQL version is 14.1.
    psql -V
    Sample output
    PostgreSQL Version Verification