Install postgresql after hesticap installed

What is the best way to add postgresql as a database server (in addition to mariadb) after hesticp has already been installed? this is on debian.
Is it to just install postgresql like on a normal debian system, or is there a hestiacp way?
thanks.

Yes, there is an Hestia way but Hestia doesn’t provide a way to do it after the installation…

Anyway, I’ve stripped the install script to only install postgresql and configure it for Hestia. I’ve tested it a couple of times and it works as expected but use it at your own risk.

install-postgres-debian.sh

#!/bin/bash

# ======================================================== #
#
# Hestia Control Panel Installer for Debian
# https://www.hestiacp.com/
#
# Currently Supported Versions:
# Debian 11 12
#
# ======================================================== #

#----------------------------------------------------------#
#                  Variables&Functions                     #
#----------------------------------------------------------#
export PATH=$PATH:/sbin
export DEBIAN_FRONTEND=noninteractive
VERSION='debian'
HESTIA='/usr/local/hestia'
spinner="/-\|"
os='debian'
release="$(cat /etc/debian_version | tr "." "\n" | head -n1)"
codename="$(cat /etc/os-release | grep VERSION= | cut -f 2 -d \( | cut -f 1 -d \))"
architecture="$(arch)"
HESTIA_INSTALL_DIR="$HESTIA/install/deb"
HESTIA_COMMON_DIR="$HESTIA/install/common"
postgresql="yes"
source "$HESTIA"/install/upgrade/upgrade.conf

case $architecture in
x86_64)
    ARCH="amd64"
    ;;
aarch64)
    ARCH="arm64"
    ;;
*)
    echo
    echo -e "\e[91mInstallation aborted\e[0m"
    echo "===================================================================="
    echo -e "\e[33mERROR: $architecture is currently not supported!\e[0m"
    echo -e "\e[33mPlease verify the achitecture used is currenlty supported\e[0m"
    echo ""
    echo -e "\e[33mhttps://github.com/hestiacp/hestiacp/blob/main/README.md\e[0m"
    echo ""
    check_result 1 "Installation aborted"
    ;;
esac

if "$HESTIA"/bin/v-list-sys-config json | jq -r '.[].DB_SYSTEM' | grep -qi 'pgsql'; then
    echo "Seems PostgreSQL is already installed"
    exit 1
fi

# Defining password-gen function
gen_pass() {
    matrix=$1
    length=$2
    if [ -z "$matrix" ]; then
        matrix="A-Za-z0-9"
    fi
    if [ -z "$length" ]; then
        length=16
    fi
    head /dev/urandom | tr -dc $matrix | head -c$length
}

# Defining return code check function
check_result() {
    if [ $1 -ne 0 ]; then
        echo "Error: $2"
        exit $1
    fi
}

# Checking root permissions
if [ "x$(id -u)" != 'x0' ]; then
    check_result 1 "Script can be run executed only by root"
fi

type=$(grep "^ID=" /etc/os-release | cut -f 2 -d '=')
if [ "$type" = "ubuntu" ]; then
    check_result 1 "You are running the wrong installer for Ubuntu. Please run hst-install.sh or hst-install-ubuntu.sh instead."
elif [ "$type" != "debian" ]; then
    check_result 1 "You are running an unsupported OS."
fi

# Configure apt to retry downloading on error
if [ ! -f /etc/apt/apt.conf.d/80-retries ]; then
    echo "APT::Acquire::Retries \"3\";" >/etc/apt/apt.conf.d/80-retries
fi

# Update apt repository
apt-get -qq update

#----------------------------------------------------------#
#                   Install repository                     #
#----------------------------------------------------------#

# Define apt conf location
apt=/etc/apt/sources.list.d

# Create new folder if it doesn't exist
mkdir -p /root/.gnupg/ && chmod 700 /root/.gnupg/

# Installing PostgreSQL repo
echo "[ * ] PostgreSQL"
echo "deb [arch=$ARCH signed-by=/usr/share/keyrings/postgresql-keyring.gpg] https://apt.postgresql.org/pub/repos/apt/ $codename-pgdg main" >$apt/postgresql.list
curl -s https://www.postgresql.org/media/keys/ACCC4CF8.asc | gpg --dearmor | tee /usr/share/keyrings/postgresql-keyring.gpg >/dev/null 2>&1

apt-get -y install postgresql postgresql-common
BACK_PID=$!

# Check if package installation is done, print a spinner
spin_i=1
while kill -0 $BACK_PID >/dev/null 2>&1; do
    printf "\b${spinner:spin_i++%${#spinner}:1}"
    sleep 0.5
done

# Do a blank echo to get the \n back
echo

# Check Installation result
wait $BACK_PID
check_result $? "apt-get install failed"
installed_db_types="$("$HESTIA"/bin/v-list-sys-config json | jq -r '.[].DB_SYSTEM')"
installed_db_types="$installed_db_types,pgsql"
if [ -n "$installed_db_types" ]; then
    db=$(echo "$installed_db_types" |
        sed "s/,/\n/g" |
        sort -u |
        sed "/^$/d" |
        sed ':a;N;$!ba;s/\n/,/g')
    "$HESTIA"/bin/v-change-sys-config-value "DB_SYSTEM" "$db"
fi

#----------------------------------------------------------#
#                   Configure PostgreSQL                   #
#----------------------------------------------------------#

echo "[ * ] Configuring PostgreSQL database server..."
ppass=$(gen_pass)
cp -f $HESTIA_INSTALL_DIR/postgresql/pg_hba.conf /etc/postgresql/*/main/
systemctl restart postgresql
sudo -iu postgres psql -c "ALTER USER postgres WITH PASSWORD '$ppass'" >/dev/null 2>&1

mkdir -p /etc/phppgadmin/
mkdir -p /usr/share/phppgadmin/

wget --retry-connrefused --quiet https://github.com/hestiacp/phppgadmin/releases/download/v$pga_v/phppgadmin-v$pga_v.tar.gz
tar xzf phppgadmin-v$pga_v.tar.gz -C /usr/share/phppgadmin/

cp -f $HESTIA_INSTALL_DIR/pga/config.inc.php /etc/phppgadmin/

ln -s /etc/phppgadmin/config.inc.php /usr/share/phppgadmin/conf/

# Configuring phpPgAdmin
if [ -d /etc/apache2/conf.d/ ]; then
    cp -f $HESTIA_INSTALL_DIR/pga/phppgadmin.conf /etc/apache2/conf.d/phppgadmin.inc
fi

rm phppgadmin-v$pga_v.tar.gz
"$HESTIA"/bin/v-change-sys-config-value "DB_PGA_ALIAS" "phppgadmin"
"$HESTIA"/bin/v-change-sys-db-alias 'pga' "phppgadmin"

# Limit access to /etc/phppgadmin/
chown -R root:hestiamail /etc/phppgadmin/
chmod 640 /etc/phppgadmin/config.inc.php

# Configuring PostgreSQL host
$HESTIA/bin/v-add-database-host pgsql localhost postgres $ppass
2 Likes

Your script worked perfectly! Thank you!

1 Like

Maybe you could submit a PR