Added CT: Postgresql

This commit is contained in:
2025-01-05 17:43:29 +01:00
parent d425174eab
commit c767b21856
11 changed files with 171 additions and 1 deletions

View File

@@ -0,0 +1,35 @@
#!/bin/bash
read -p "Enter your e-mail for pgAdmin login: " PostgressAdminMail
read -p "Enter new password for pgAdmin and postgresql database admin: " -s NewPostgressPassword
echo
echo "Please wait..."
#Configure Postgresql
su postgres -c "psql -c \"alter user postgres with password '$NewPostgressPassword';\""
#Configure pgAdmin
##Create pgadmin db for storing pgAdmin config
PGADMIN_DB_PASSWORD=$(dd bs=20 count=1 if=/dev/urandom | base64 | tr +/ _.)
su postgres -c "psql -c \"CREATE ROLE pgadmin WITH LOGIN NOSUPERUSER NOCREATEDB NOCREATEROLE INHERIT NOREPLICATION NOBYPASSRLS CONNECTION LIMIT 25 PASSWORD '$PGADMIN_DB_PASSWORD';\""
su postgres -c "psql -c \"CREATE DATABASE pgadmin WITH OWNER = pgadmin ENCODING = 'UTF8' LOCALE_PROVIDER = 'libc' CONNECTION LIMIT = -1 IS_TEMPLATE = False;\""
sed -i "s/DBPassword/$PGADMIN_DB_PASSWORD/" /usr/pgadmin4/web/config_local.py
##Setup the pgAdmin base
export PGADMIN_SETUP_EMAIL=$PostgressAdminMail
export PGADMIN_SETUP_PASSWORD=$NewPostgressPassword
su -pc "/usr/pgadmin4/venv/bin/python3 /usr/pgadmin4/web/setup.py setup-db" pgadmin
systemctl restart pgadmin
## add localhost postges server as connection is pgAdmin
sed -i "s/Password/$NewPostgressPassword/" /opt/Setup/Configs/pgpassfile
mv /opt/Setup/Configs/pgpassfile /var/lib/pgadmin/storage/shared/pgpassfile
chown pgadmin: /var/lib/pgadmin/storage/shared/pgpassfile
chmod 600 /var/lib/pgadmin/storage/shared/pgpassfile
/usr/pgadmin4/venv/bin/python3 /usr/pgadmin4/web/setup.py load-servers /opt/Setup/Configs/pgadmin-server-import.json --user $PostgressAdminMail
## Set user preferences (i.e. set the theme to system)
/usr/pgadmin4/venv/bin/python3 /usr/pgadmin4/web/setup.py set-prefs $PostgressAdminMail --input-file /opt/Setup/Configs/pgadmin-user-preferences.json
##Clean-up
rm /root/ReadMe /opt/Setup/Configs/pgadmin-server-import.json /opt/Setup/Configs/pgadmin-user-preferences.json

View File

@@ -0,0 +1,16 @@
#!/bin/sh
#Setup postgresql
printf "\n#User entries (Make sure to reload postgressql after updating this file) \n# TYPE DATABASE USER ADDRESS METHOD\n" >> /etc/postgresql/17/main/pg_hba.conf
sed -i "s|#listen_addresses = 'localhost'|listen_addresses = '*' |" /etc/postgresql/17/main/postgresql.conf
#Setup pgadmin
adduser --system --shell /bin/false --ingroup www-data --disabled-password --disabled-login --home /var/lib/www/pgadmin pgadmin
mkdir -p /var/log/pgadmin /var/lib/pgadmin/storage/shared
chown pgadmin: /var/log/pgadmin /var/lib/pgadmin -R
mv /opt/Setup/Configs/pgadmin-uwsgi.ini /etc/uwsgi/apps-available/pgadmin.ini
mv /opt/Setup/Configs/pgadmin-config_local.py /usr/pgadmin4/web/config_local.py
#Setup uwsgi service for pgadmin
mv /opt/Setup/Configs/pgadmin.service /lib/systemd/system/pgadmin.service
chmod +x /lib/systemd/system/pgadmin.service
setcap 'cap_net_bind_service=+ep' /bin/uwsgi-core