-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathpgadmin.sh
More file actions
executable file
·76 lines (68 loc) · 1.58 KB
/
pgadmin.sh
File metadata and controls
executable file
·76 lines (68 loc) · 1.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#!/bin/bash
#
# Script para administrar una base de datos PostgreSQL
# @author Esteban De La Fuente Rubio, DeLaF (esteban[at]delaf.cl)
#
function useradd {
su - postgres -c "createuser $1"
passwd $1
}
function userdel {
dbdel $1
su - postgres -c "dropuser $1"
}
function passwd {
echo -n "¿Cambiar clave al usuario $1? [n]: "; read OK
if [ "$OK" = "y" ]; then
# iterar hasta tener una clave válida
while true; do
# solicitar clave hasta que haya una válida
passwd_get
# si todo esta ok se rompe el ciclo
if [ "$PASSWORD" != "1" ]; then
break
fi
done
# cambiar la clave
su - postgres -c "psql -d template1" << EOF
ALTER USER $1 WITH PASSWORD '$PASSWORD';
EOF
fi
}
# Solicitar clave al usuario (2 veces)
function passwd_get {
echo -n "Ingresar clave: "
stty -echo; read PASSWORD1; stty echo; echo ""
echo -n "Repetir clave: "
stty -echo; read PASSWORD2; stty echo; echo ""
# si las claves son diferentes se piden denuevo
if [ "$PASSWORD1" != "$PASSWORD2" ]; then
PASSWORD=1
else
PASSWORD=$PASSWORD1
fi
}
function dbadd {
su - postgres -c "createdb --owner=$1 $2"
}
function dbdel {
su - postgres -c "dropdb $1"
}
function help {
echo "Modo de ejecución: $0 {opción}"
echo "Opciones: useradd USUARIO"
echo " userdel USUARIO"
echo " passwd USUARIO"
echo " dbadd USUARIO BASE_DE_DATOS"
echo " dbdel BASE_DE_DATOS"
exit 1
}
# Determinar que ejecutar según se haya indicado por $1
case "$1" in
useradd) useradd $2;;
userdel) userdel $2;;
passwd) passwd $2;;
dbadd) dbadd $2 $3;;
dbdel) dbdel $2;;
*) help;;
esac