2 #-------------------------------------------------------------------------
5 # Utility for creating a user in the PostgreSQL database
7 # Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
8 # Portions Copyright (c) 1994, Regents of the University of California
12 # $Header: /cvsroot/pgsql/src/bin/scripts/Attic/createuser,v 1.17 2001/02/18 18:34:01 momjian Exp $
14 # Note - this should NOT be setuid.
16 #-------------------------------------------------------------------------
19 PATHNAME=`echo $0 | sed "s,$CMDNAME\$,,"`
29 # Check for echo -n vs echo \c
31 if echo '\c' | grep -s c >/dev/null 2>&1
48 # options passed on to psql
50 PSQLOPT="$PSQLOPT -h $2"
56 PSQLOPT="$PSQLOPT -h "`echo $1 | sed 's/^--host=//'`
59 PSQLOPT="$PSQLOPT -p $2"
65 PSQLOPT="$PSQLOPT -p "`echo $1 | sed 's/^--port=//'`
67 # Note: These two specify the user to connect as (like in psql),
68 # not the user you're creating.
70 PSQLOPT="$PSQLOPT -U $2"
76 PSQLOPT="$PSQLOPT -U "`echo $1 | sed 's/^--username=//'`
85 PSQLOPT="$PSQLOPT -o /dev/null"
87 # options converted into SQL command
104 SysID=`echo "$1" | sed 's/^--sysid=//'`
107 SysID=`echo "$1" | sed 's/^-i//'`
113 echo "$CMDNAME: invalid option: $1" 1>&2
114 echo "Try '$CMDNAME --help' for more information." 1>&2
124 if [ "$usage" ]; then
125 echo "$CMDNAME creates a new PostgreSQL user."
128 echo " $CMDNAME [options] [username]"
131 echo " -d, --createdb User can create new databases"
132 echo " -D, --no-createdb User cannot create databases"
133 echo " -a, --adduser User can add new users"
134 echo " -A, --no-adduser User cannot add new users"
135 echo " -i, --sysid=SYSID Select sysid for new user"
136 echo " -P, --pwprompt Assign a password to new user"
137 echo " -h, --host=HOSTNAME Database server host"
138 echo " -p, --port=PORT Database server port"
139 echo " -U, --username=USERNAME Username to connect as (not the one to create)"
140 echo " -W, --password Prompt for password to connect"
141 echo " -e, --echo Show the query being sent to the backend"
142 echo " -q, --quiet Don't write any messages"
144 echo "If one of -d, -D, -a, -A, and 'username' is not specified, you will"
145 echo "be prompted interactively."
147 echo "Report bugs to <pgsql-bugs@postgresql.org>."
151 if [ "$SysID" ]; then
152 if [ "$SysID" != "`echo $SysID | sed 's/[^0-9]//g'`" ]; then
153 echo "$CMDNAME: user sysid must be a positive number" 1>&2
158 # Don't want to leave the user blind if he breaks
159 # during password entry.
161 trap 'stty echo >/dev/null 2>&1' 1 2 3 15
163 # Get missing user attributes
165 if [ -z "$NewUser" ]; then
166 $ECHO_N "Enter name of user to add: "$ECHO_C
168 [ $? -ne 0 ] && exit 1
171 if [ "$PwPrompt" ]; then
172 $ECHO_N "Enter password for user \"$NewUser\": "$ECHO_C
173 stty -echo >/dev/null 2>&1
175 stty echo >/dev/null 2>&1
177 $ECHO_N "Enter it again: "$ECHO_C
178 stty -echo >/dev/null 2>&1
180 stty echo >/dev/null 2>&1
182 if [ "$FirstPw" != "$SecondPw" ]; then
183 echo "Passwords didn't match." 1>&2
189 if [ -z "$CanCreateDb" ]; then
190 $ECHO_N "Shall the new user be allowed to create databases? (y/n) "$ECHO_C
192 [ $? -ne 0 ] && exit 1
193 if [ "$REPLY" = "y" -o "$REPLY" = "Y" ]; then
200 if [ -z "$CanAddUser" ]; then
201 $ECHO_N "Shall the new user be allowed to create more new users? (y/n) "$ECHO_C
203 [ $? -ne 0 ] && exit 1
204 if [ "$REPLY" = "y" -o "$REPLY" = "Y" ]; then
215 NewUser=`echo "$NewUser" | sed 's/\"/\\\"/g'`
216 Password=`echo "$Password" | sed 's/\"/\\\"/g'`
218 QUERY="CREATE USER \"$NewUser\""
221 [ "$SysID" ] && SUBQUERY="$SUBQUERY SYSID $SysID"
222 [ "$Password" ] && SUBQUERY="$SUBQUERY PASSWORD '$Password'"
223 [ "$SUBQUERY" ] && QUERY="$QUERY WITH $SUBQUERY"
225 [ "$CanCreateDb" = t ] && QUERY="$QUERY CREATEDB"
226 [ "$CanCreateDb" = f ] && QUERY="$QUERY NOCREATEDB"
227 [ "$CanAddUser" = t ] && QUERY="$QUERY CREATEUSER"
228 [ "$CanAddUser" = f ] && QUERY="$QUERY NOCREATEUSER"
230 ${PATHNAME}psql -c "$QUERY" -d template1 $PSQLOPT
231 if [ $? -ne 0 ]; then
232 echo "$CMDNAME: creation of user \"$NewUser\" failed" 1>&2