mirror of
https://github.com/postgres/postgres.git
synced 2026-04-15 22:10:45 -04:00
145 lines
2.7 KiB
Text
145 lines
2.7 KiB
Text
|
|
#!/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.1 1999/12/04 04:53:21 momjian Exp $
|
||
|
|
#
|
||
|
|
# Note - this should NOT be setuid.
|
||
|
|
#
|
||
|
|
#-------------------------------------------------------------------------
|
||
|
|
|
||
|
|
CMDNAME=`basename $0`
|
||
|
|
|
||
|
|
NewUser=
|
||
|
|
SysID=
|
||
|
|
CanAddUser=
|
||
|
|
CanCreateDb=
|
||
|
|
PwPrompt=
|
||
|
|
Password=
|
||
|
|
PSQLOPT=
|
||
|
|
|
||
|
|
|
||
|
|
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: "
|
||
|
|
read -r NewUser
|
||
|
|
[ $? -ne 0 ] && exit 1
|
||
|
|
fi
|
||
|
|
|
||
|
|
if [ "$PwPrompt" ]; then
|
||
|
|
echo -n "Enter password for user $NewUser: "
|
||
|
|
read -r Password
|
||
|
|
fi
|
||
|
|
|
||
|
|
if [ -z "$CanCreateDb" ]; then
|
||
|
|
echo -n "Is the new user allowed to create databases? (y/n) "
|
||
|
|
read -r
|
||
|
|
[ $? -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) "
|
||
|
|
read -r
|
||
|
|
[ $? -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
|