You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 
postgres/src/bin/scripts/dropuser

141 lines
3.1 KiB

#!/bin/sh
#-------------------------------------------------------------------------
#
# dropuser--
# Utility for removing a user from the PostgreSQL database.
#
# Copyright (c) 1994, Regents of the University of California
#
#
# IDENTIFICATION
# $Header: /cvsroot/pgsql/src/bin/scripts/Attic/dropuser,v 1.5 2000/01/12 19:36:36 petere Exp $
#
# Note - this should NOT be setuid.
#
#-------------------------------------------------------------------------
CMDNAME=`basename $0`
PSQLOPT=
forcedel=t
DelUser=
# 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;;
-h*)
PSQLOPT="$PSQLOPT $1"
;;
--host=*)
PSQLOPT="$PSQLOPT -h "`echo $1 | sed 's/^--host=//'`
;;
--port|-p)
PSQLOPT="$PSQLOPT -p $2"
shift;;
-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.
--user|--username|-U)
PSQLOPT="$PSQLOPT -U '$2'"
shift;;
-U*)
PSQLOPT="$PSQLOPT $1"
;;
--user=*)
PSQLOPT="$PSQLOPT -U "`echo $1 | sed 's/^--user=//'`
;;
--username=*)
PSQLOPT="$PSQLOPT -U "`echo $1 | sed 's/^--username=//'`
;;
--password|-W)
PSQLOPT="$PSQLOPT -W"
;;
--echo|-e)
PSQLOPT="$PSQLOPT -e"
;;
--quiet|-q)
PSQLOPT="$PSQLOPT -o /dev/null"
;;
# other options
--interactive|-i)
forcedel=f
;;
-*)
echo "$CMDNAME: Unrecognized option: $1. Try -? for help."
exit 1
;;
*)
DelUser="$1"
;;
esac
shift;
done
if [ "$usage" ]; then
echo ""
echo "Usage: $CMDNAME [options] [username]"
echo ""
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"
#??? echo " -e, --echo "
echo " -q, --quiet Don't write any messages"
exit 0
fi
# Prompt for username if missing
if [ -z "$DelUser" ]; then
$ECHO_N "Enter name of user to delete: "$ECHO_C
read DelUser
[ $? -ne 0 ] && exit 1
fi
if [ "$forcedel" = f ]; then
echo "User \"$DelUser\" and any owned databases will be permanently deleted."
$ECHO_N "Are you sure? (y/n) "$ECHO_C
read REPLY
[ $? -eq 1 ] && exit 1
[ "$REPLY" != "y" -a "$REPLY" != "Y" ] && exit 0
fi
DelUser=`echo $DelUser | sed 's/\"/\\\"/g'`
psql $PSQLOPT -d template1 -c "DROP USER \"$DelUser\""
if [ $? -ne 0 ]; then
echo "$CMDNAME: deletion of user \"$DelUser\" failed"
exit 1
fi
exit 0