create script create_role for pg
This commit is contained in:
@@ -1,2 +1,3 @@
|
|||||||
create_pg_db.sh
|
create_pg_db.sh
|
||||||
|
create_pg_role.sh
|
||||||
setup-postgres17.sh
|
setup-postgres17.sh
|
||||||
|
|||||||
40
scripts/server-postgres/create_pg_role.sh
Normal file
40
scripts/server-postgres/create_pg_role.sh
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
# Basé sur un travail de Cédric Abonnel / Cédrix sous licence CC BY-NC 4.0
|
||||||
|
|
||||||
|
# Importer les fonctions communes
|
||||||
|
source "$(dirname "$0")/../common/common_utils.sh"
|
||||||
|
|
||||||
|
# Vérifier si le script est exécuté en root
|
||||||
|
check_root
|
||||||
|
|
||||||
|
set -e
|
||||||
|
|
||||||
|
ROLE_NAME="$1"
|
||||||
|
|
||||||
|
if [[ -z "$ROLE_NAME" ]]; then
|
||||||
|
echo "❌ Usage: $0 <nom_du_role>"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo "🔎 Vérification de l'existence du rôle PostgreSQL '$ROLE_NAME'..."
|
||||||
|
ROLE_EXISTS=$(sudo -u postgres psql -tAc "SELECT 1 FROM pg_roles WHERE rolname='${ROLE_NAME}'")
|
||||||
|
|
||||||
|
if [[ "$ROLE_EXISTS" == "1" ]]; then
|
||||||
|
echo "⚠️ Le rôle '$ROLE_NAME' existe déjà."
|
||||||
|
else
|
||||||
|
echo "🔐 Génération d'un mot de passe fort pour le rôle '$ROLE_NAME'..."
|
||||||
|
ROLE_PASSWORD=$(generate_token 20)
|
||||||
|
|
||||||
|
echo "🛠 Création du rôle '$ROLE_NAME' avec LOGIN et mot de passe..."
|
||||||
|
sudo -u postgres psql -c "CREATE ROLE ${ROLE_NAME} WITH LOGIN PASSWORD '${ROLE_PASSWORD}';"
|
||||||
|
|
||||||
|
echo ""
|
||||||
|
echo "✅ Rôle '$ROLE_NAME' créé avec succès."
|
||||||
|
echo "-------------------------------------------"
|
||||||
|
echo " Nom du rôle : ${ROLE_NAME}"
|
||||||
|
echo " Mot de passe : ${ROLE_PASSWORD}"
|
||||||
|
echo "-------------------------------------------"
|
||||||
|
echo " Pensez à sauvegarder ces informations."
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo "🎉 Opération terminée."
|
||||||
Reference in New Issue
Block a user