dropuser 3.36 KB
Newer Older
1 2 3 4
#!/bin/sh
#-------------------------------------------------------------------------
#
# dropuser--
5
#    Utility for removing a user from the PostgreSQL database.
6
#
7 8
# Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
# Portions Copyright (c) 1994, Regents of the University of California
9 10 11
#
#
# IDENTIFICATION
12
#    $Header: /cvsroot/pgsql/src/bin/scripts/Attic/dropuser,v 1.13 2001/09/22 04:28:12 momjian Exp $
13 14 15 16 17
#
# Note - this should NOT be setuid.
#
#-------------------------------------------------------------------------

18
CMDNAME=`basename "$0"`
19 20
PATHNAME=`echo $0 | sed "s,$CMDNAME\$,,"`

21 22
PSQLOPT=
forcedel=t
23
DelUser=
24

25 26 27 28 29 30 31 32 33 34 35
# 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

36

37 38 39 40 41
while [ $# -gt 0 ]
do
    case "$1" in
	--help|-\?)
		usage=t
42
                break
43 44 45 46 47
		;;
# options passed on to psql
	--host|-h)
		PSQLOPT="$PSQLOPT -h $2"
		shift;;
48 49 50 51 52 53
        -h*)
                PSQLOPT="$PSQLOPT $1"
                ;;
        --host=*)
                PSQLOPT="$PSQLOPT -h "`echo $1 | sed 's/^--host=//'`
                ;;
54 55 56
	--port|-p)
		PSQLOPT="$PSQLOPT -p $2"
		shift;;
57 58 59 60 61 62 63 64
        -p*)
                PSQLOPT="$PSQLOPT $1"
                ;;
        --port=*)
                PSQLOPT="$PSQLOPT -p "`echo $1 | sed 's/^--port=//'`
                ;;
# Note: These two specify the user to connect as (like in psql),
#       not the user you're dropping.
65 66
	--username|-U)
		PSQLOPT="$PSQLOPT -U $2"
67 68 69 70 71 72 73 74 75 76
		shift;;
        -U*)
                PSQLOPT="$PSQLOPT $1"
                ;;
        --username=*)
                PSQLOPT="$PSQLOPT -U "`echo $1 | sed 's/^--username=//'`
                ;;
	--password|-W)
		PSQLOPT="$PSQLOPT -W"
		;;
77 78 79 80 81 82 83 84 85 86 87
	--echo|-e)
		PSQLOPT="$PSQLOPT -e"
		;;
	--quiet|-q)
		PSQLOPT="$PSQLOPT -o /dev/null"
		;;
# other options
	--interactive|-i)
		forcedel=f
		;;
	-*)
88
		echo "$CMDNAME: invalid option: $1" 1>&2
89
                echo "Try '$CMDNAME --help' for more information." 1>&2
90 91 92 93 94 95 96 97 98 99 100
		exit 1
		;;
         *)
		DelUser="$1"
		;;
    esac
    shift;
done


if [ "$usage" ]; then
101 102 103 104 105 106
	echo "$CMDNAME removes a PostgreSQL user."
        echo
	echo "Usage:"
        echo "  $CMDNAME [options] [username]"
        echo
	echo "Options:"
107 108 109 110 111
	echo "  -h, --host=HOSTNAME             Database server host"
	echo "  -p, --port=PORT                 Database server port"
	echo "  -U, --username=USERNAME         Username to connect as (not the one to drop)"
	echo "  -W, --password                  Prompt for password to connect"
	echo "  -i, --interactive               Prompt before deleting anything"
112 113 114 115
        echo "  -e, --echo                      Show the query being sent to the backend"
        echo "  -q, --quiet                     Don't write any messages"
	echo
	echo "Report bugs to <pgsql-bugs@postgresql.org>."
116
	exit 0
117 118 119 120 121
fi

# Prompt for username if missing

if [ -z "$DelUser" ]; then
122
	$ECHO_N "Enter name of user to delete: "$ECHO_C
123
	read DelUser
124 125 126 127 128
	[ $? -ne 0 ] && exit 1
fi


if [ "$forcedel" = f ]; then
129
	echo "User \"$DelUser\" will be permanently deleted."
130 131
	$ECHO_N "Are you sure? (y/n) "$ECHO_C
	read REPLY
132 133 134 135 136 137

	[ $? -eq 1 ] && exit 1
	[ "$REPLY" != "y" -a "$REPLY" != "Y" ] && exit 0
fi


138
DelUser=`echo "$DelUser" | sed 's/\"/\\\"/g'`
139

140
${PATHNAME}psql $PSQLOPT -d template1 -c "DROP USER \"$DelUser\""
141 142

if [ $? -ne 0 ]; then
143
	echo "$CMDNAME: deletion of user \"$DelUser\" failed" 1>&2
144 145 146 147
	exit 1
fi

exit 0