2 #-------------------------------------------------------------------------
5 # Utility for creating a user in the PostgreSQL database
7 # Copyright (c) 1994, Regents of the University of California
11 # $Header: /cvsroot/pgsql/src/bin/scripts/Attic/createuser,v 1.16 2001/01/21 05:16:45 momjian Exp $
13 # Note - this should NOT be setuid.
15 #-------------------------------------------------------------------------
18 PATHNAME=`echo $0 | sed "s,$CMDNAME\$,,"`
28 # Check for echo -n vs echo \c
30 if echo '\c' | grep -s c >/dev/null 2>&1
47 # options passed on to psql
49 PSQLOPT="$PSQLOPT -h $2"
55 PSQLOPT="$PSQLOPT -h "`echo $1 | sed 's/^--host=//'`
58 PSQLOPT="$PSQLOPT -p $2"
64 PSQLOPT="$PSQLOPT -p "`echo $1 | sed 's/^--port=//'`
66 # Note: These two specify the user to connect as (like in psql),
67 # not the user you're creating.
69 PSQLOPT="$PSQLOPT -U $2"
75 PSQLOPT="$PSQLOPT -U "`echo $1 | sed 's/^--username=//'`
84 PSQLOPT="$PSQLOPT -o /dev/null"
86 # options converted into SQL command
103 SysID=`echo "$1" | sed 's/^--sysid=//'`
106 SysID=`echo "$1" | sed 's/^-i//'`
112 echo "$CMDNAME: invalid option: $1" 1>&2
113 echo "Try '$CMDNAME --help' for more information." 1>&2
123 if [ "$usage" ]; then
124 echo "$CMDNAME creates a new PostgreSQL user."
127 echo " $CMDNAME [options] [username]"
130 echo " -d, --createdb User can create new databases"
131 echo " -D, --no-createdb User cannot create databases"
132 echo " -a, --adduser User can add new users"
133 echo " -A, --no-adduser User cannot add new users"
134 echo " -i, --sysid=SYSID Select sysid for new user"
135 echo " -P, --pwprompt Assign a password to new user"
136 echo " -h, --host=HOSTNAME Database server host"
137 echo " -p, --port=PORT Database server port"
138 echo " -U, --username=USERNAME Username to connect as (not the one to create)"
139 echo " -W, --password Prompt for password to connect"
140 echo " -e, --echo Show the query being sent to the backend"
141 echo " -q, --quiet Don't write any messages"
143 echo "If one of -d, -D, -a, -A, and 'username' is not specified, you will"
144 echo "be prompted interactively."
146 echo "Report bugs to <pgsql-bugs@postgresql.org>."
150 if [ "$SysID" ]; then
151 if [ "$SysID" != "`echo $SysID | sed 's/[^0-9]//g'`" ]; then
152 echo "$CMDNAME: user sysid must be a positive number" 1>&2
157 # Don't want to leave the user blind if he breaks
158 # during password entry.
160 trap 'stty echo >/dev/null 2>&1' 1 2 3 15
162 # Get missing user attributes
164 if [ -z "$NewUser" ]; then
165 $ECHO_N "Enter name of user to add: "$ECHO_C
167 [ $? -ne 0 ] && exit 1
170 if [ "$PwPrompt" ]; then
171 $ECHO_N "Enter password for user \"$NewUser\": "$ECHO_C
172 stty -echo >/dev/null 2>&1
174 stty echo >/dev/null 2>&1
176 $ECHO_N "Enter it again: "$ECHO_C
177 stty -echo >/dev/null 2>&1
179 stty echo >/dev/null 2>&1
181 if [ "$FirstPw" != "$SecondPw" ]; then
182 echo "Passwords didn't match." 1>&2
188 if [ -z "$CanCreateDb" ]; then
189 $ECHO_N "Shall the new user be allowed to create databases? (y/n) "$ECHO_C
191 [ $? -ne 0 ] && exit 1
192 if [ "$REPLY" = "y" -o "$REPLY" = "Y" ]; then
199 if [ -z "$CanAddUser" ]; then
200 $ECHO_N "Shall the new user be allowed to create more new users? (y/n) "$ECHO_C
202 [ $? -ne 0 ] && exit 1
203 if [ "$REPLY" = "y" -o "$REPLY" = "Y" ]; then
214 NewUser=`echo "$NewUser" | sed 's/\"/\\\"/g'`
215 Password=`echo "$Password" | sed 's/\"/\\\"/g'`
217 QUERY="CREATE USER \"$NewUser\""
220 [ "$SysID" ] && SUBQUERY="$SUBQUERY SYSID $SysID"
221 [ "$Password" ] && SUBQUERY="$SUBQUERY PASSWORD '$Password'"
222 [ "$SUBQUERY" ] && QUERY="$QUERY WITH $SUBQUERY"
224 [ "$CanCreateDb" = t ] && QUERY="$QUERY CREATEDB"
225 [ "$CanCreateDb" = f ] && QUERY="$QUERY NOCREATEDB"
226 [ "$CanAddUser" = t ] && QUERY="$QUERY CREATEUSER"
227 [ "$CanAddUser" = f ] && QUERY="$QUERY NOCREATEUSER"
229 ${PATHNAME}psql -c "$QUERY" -d template1 $PSQLOPT
230 if [ $? -ne 0 ]; then
231 echo "$CMDNAME: creation of user \"$NewUser\" failed" 1>&2