blob: c3d00d1ea719a3bc9fafbd58e3488c99a803bf1c (
plain)
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
|
#!/bin/sh
#-------------------------------------------------------------------------
#
# createuser--
# Utility for creating a user in the PostgreSQL database
#
# Copyright (c) 1994, Regents of the University of California
#
#
# IDENTIFICATION
# $Header: /cvsroot/pgsql/src/bin/scripts/Attic/createuser,v 1.2 1999/12/05 20:52:54 momjian Exp $
#
# Note - this should NOT be setuid.
#
#-------------------------------------------------------------------------
CMDNAME=`basename $0`
NewUser=
SysID=
CanAddUser=
CanCreateDb=
PwPrompt=
Password=
PSQLOPT=
# Check for echo -n vs echo \c
if echo '\c' | grep -s c >/dev/null 2>&1
then
ECHO_N="echo -n"
ECHO_C=""
else
ECHO_N="echo"
ECHO_C='\c'
fi
while [ $# -gt 0 ]
do
case "$1" in
--help|-\?)
usage=t
break
;;
# options passed on to psql
--host|-h)
PSQLOPT="$PSQLOPT -h $2"
shift;;
--port|-p)
PSQLOPT="$PSQLOPT -p $2"
shift;;
# Uncomment these lines if you need the -U and -W options.
# They are confusing in this context, however.
# --user|--username|-U)
# PSQLOPT="$PSQLOPT -U $2"
# shift;;
# --password|-W)
# PSQLOPT="$PSQLOPT -W"
# ;;
--echo|-e)
PSQLOPT="$PSQLOPT -e"
;;
--quiet|-q)
PSQLOPT="$PSQLOPT -o /dev/null"
;;
# options converted into SQL command
--createdb|-d)
CanCreateDb=t
;;
--no-createdb|-D)
CanCreateDb=f
;;
--adduser|-a)
CanAddUser=t
;;
--no-adduser|-A)
CanAddUser=f
;;
--pwprompt|--pw|-P)
PwPrompt=t
;;
-*)
echo "$CMDNAME: Unrecognized option: $1. Try -? for help."
exit 1
;;
*)
NewUser=$1
;;
esac
shift;
done
# Help
if [ "$usage" ]; then
echo "Usage: $CMDNAME [-h <server>] [-p <port>] [-d|-D] [-a|-A] [-P] [username]"
exit 0
fi
# Get missing user attributes
if [ -z "$NewUser" ]; then
$ECHO_N "Enter name of user to add: "$ECHO_C
read NewUser
[ $? -ne 0 ] && exit 1
fi
if [ "$PwPrompt" ]; then
$ECHO_N "Enter password for user $NewUser: "$ECHO_C
read Password
fi
if [ -z "$CanCreateDb" ]; then
$ECHO_N "Is the new user allowed to create databases? (y/n) "$ECHO_C
read REPLY
[ $? -ne 0 ] && exit 1
if [ $REPLY = "y" -o $REPLY = "Y" ]; then
CanCreateDb=t
else
CanCreateDb=f
fi
fi
if [ -z "$CanAddUser" ]; then
$ECHO_N "Shall the new user be allowed to create more new users? (y/n) "$ECHO_C
read REPLY
[ $? -ne 0 ] && exit 1
if [ $REPLY = "y" -o $REPLY = "Y" ]; then
CanAddUser=t
else
CanAddUser=f
fi
fi
#
# build SQL command
#
QUERY="CREATE USER \"$NewUser\""
[ "$Password" ] && QUERY="$QUERY WITH PASSWORD \"$Password\""
[ "$CanCreateDb" = t ] && QUERY="$QUERY CREATEDB"
[ "$CanCreateDb" = f ] && QUERY="$QUERY NOCREATEDB"
[ "$CanAddUser" = t ] && QUERY="$QUERY CREATEUSER"
[ "$CanAddUser" = f ] && QUERY="$QUERY NOCREATEUSER"
psql $PSQLOPT -d template1 -c "$QUERY"
if [ $? -ne 0 ]; then
echo "$CMDNAME: Creation of user \"$NewUser\" failed."
exit 1
fi
exit 0
|