1
0
mirror of https://github.com/postgres/postgres.git synced 2025-07-28 23:42:10 +03:00

New scripts for create/drop user/db from Peter Eisentraut

This commit is contained in:
Bruce Momjian
1999-12-04 04:53:22 +00:00
parent 21b69148dc
commit 240e4c98f5
33 changed files with 1286 additions and 2081 deletions

View File

@ -7,16 +7,15 @@
#
#
# IDENTIFICATION
# $Header: /cvsroot/pgsql/src/bin/Makefile,v 1.18 1999/05/20 16:49:59 wieck Exp $
# $Header: /cvsroot/pgsql/src/bin/Makefile,v 1.19 1999/12/04 04:53:16 momjian Exp $
#
#-------------------------------------------------------------------------
SRCDIR= ..
include ../Makefile.global
DIRS = pg_id pg_version psql pg_dump pg_passwd cleardbdir createdb \
createlang createuser destroydb destroylang destroyuser initdb \
vacuumdb initlocation ipcclean
DIRS = pg_id pg_version psql pg_dump pg_passwd cleardbdir \
createlang destroylang initdb initlocation ipcclean
ifdef MULTIBYTE
DIRS += pg_encoding

View File

@ -1,28 +0,0 @@
#-------------------------------------------------------------------------
#
# Makefile.inc--
# Makefile for bin/createdb
#
# Copyright (c) 1994, Regents of the University of California
#
#
# IDENTIFICATION
# $Header: /cvsroot/pgsql/src/bin/createdb/Attic/Makefile,v 1.10 1998/07/26 04:31:12 scrappy Exp $
#
#-------------------------------------------------------------------------
SRCDIR= ../..
include ../../Makefile.global
all: createdb
createdb: createdb.sh
sed 's/__MULTIBYTE__/$(MULTIBYTE)/' createdb.sh > createdb
install: createdb
$(INSTALL) $(INSTL_EXE_OPTS) $< $(BINDIR)/$<
clean:
rm -f createdb
dep depend:

View File

@ -1,121 +0,0 @@
#!/bin/sh
#-------------------------------------------------------------------------
#
# createdb.sh--
# create a postgres database
#
# this program runs the monitor with the "-c" option to create
# the requested database.
#
# Copyright (c) 1994, Regents of the University of California
#
#
# IDENTIFICATION
# $Header: /cvsroot/pgsql/src/bin/createdb/Attic/createdb.sh,v 1.12 1999/11/18 21:47:37 momjian Exp $
#
#-------------------------------------------------------------------------
CMDNAME=`basename $0`
MBENABLED=__MULTIBYTE__
MB=
if [ -z "$USER" ]; then
if [ -z "$LOGNAME" ]; then
if [ -z "`whoami`" ]; then
echo "$CMDNAME: cannot determine user name"
exit 1
fi
else
USER=$LOGNAME
export USER
fi
fi
dbname=$USER
PASSWDOPT="";
while test -n "$1"
do
case $1 in
--help) usage=1;;
-a) AUTHSYS=$2; shift;;
-h) PGHOST=$2; shift;;
-p) PGPORT=$2; shift;;
-u) PASSWDOPT=$1;;
-D) dbpath=$2; shift;;
-E)
if [ -z "$MBENABLED" ];then
echo "$CMDNAME: you need to turn on MB compile time option"
exit 1
fi
MB=$2
MBID=`pg_encoding $MB`
if [ -z "$MBID" ];then
echo "$CMDNAME: $MB is not a valid encoding name"
exit 1
fi
shift;;
-*) echo "$CMDNAME: unrecognized parameter $1"; usage=1;;
*) dbname=$1;;
esac
shift;
done
if [ "$usage" ]; then
if [ -z "$MBENABLED" ];then
echo "Usage: $CMDNAME -a <authtype> -h <server> -p <portnumber> -D <location> [dbname]"
exit 1
else
echo "Usage: $CMDNAME -a <authtype> -h <server> -p <portnumber> -D <location> -E <encoding> [dbname]"
exit 1
fi
fi
if [ -z "$AUTHSYS" ]; then
AUTHOPT=""
else
AUTHOPT="-a $AUTHSYS"
fi
if [ -z "$PGHOST" ]; then
PGHOSTOPT=""
else
PGHOSTOPT="-h $PGHOST"
fi
if [ -z "$PGPORT" ]; then
PGPORTOPT=""
else
PGPORTOPT="-p $PGPORT"
fi
if [ -z "$dbpath" ]; then
location=""
else
# if [ ! -d "$dbpath"/base ]; then
# echo "$CMDNAME: database creation failed on $dbname."
# echo "directory $dbpath/base not found."
# exit 1
# fi
location="with location = '$dbpath'"
fi
if [ -z "$MBENABLED" -o -z "$MB" ]; then
encoding=""
else
encoding="encoding = '$MB'"
if [ -z "$location" ];then
encoding="with $encoding"
fi
fi
psql $PASSWDOPT -tq $AUTHOPT $PGHOSTOPT $PGPORTOPT -c "create database \"$dbname\" $location $encoding" template1
if [ $? -ne 0 ]; then
echo "$CMDNAME: database creation failed on $dbname."
exit 1
fi
exit 0

View File

@ -1,32 +0,0 @@
#-------------------------------------------------------------------------
#
# Makefile.inc--
# Makefile for bin/createuser
#
# Copyright (c) 1994, Regents of the University of California
#
#
# IDENTIFICATION
# $Header: /cvsroot/pgsql/src/bin/createuser/Attic/Makefile,v 1.9 1998/08/22 05:19:16 momjian Exp $
#
#-------------------------------------------------------------------------
SRCDIR= ../..
include ../../Makefile.global
SEDSCRIPT= \
-e "s^PG_OPT_DASH_N_PARAM^$(DASH_N)^g" \
-e "s^PG_OPT_BACKSLASH_C_PARAM^$(BACKSLASH_C)^g"
all: createuser
createuser: createuser.sh
sed $(SEDSCRIPT) <createuser.sh >createuser
install: createuser
$(INSTALL) $(INSTL_EXE_OPTS) $< $(BINDIR)/$<
clean:
rm -f createuser
dep depend:

View File

@ -1,252 +0,0 @@
#!/bin/sh
#-------------------------------------------------------------------------
#
# createuser.sh--
# utility for creating a user in the POSTGRES database
#
# Copyright (c) 1994, Regents of the University of California
#
#
# IDENTIFICATION
# $Header: /cvsroot/pgsql/src/bin/createuser/Attic/createuser.sh,v 1.13 1999/09/27 16:44:56 momjian Exp $
#
# Note - this should NOT be setuid.
#
#-------------------------------------------------------------------------
CMDNAME=`basename $0`
SYSID=
CANADDUSER=
CANCREATE=
if [ -z "$USER" ]; then
if [ -z "$LOGNAME" ]; then
if [ -z "`whoami`" ]; then
echo "$CMDNAME: cannot determine user name"
exit 1
fi
else
USER=$LOGNAME
export USER
fi
fi
while [ -n "$1" ]
do
case $1 in
-a) AUTHSYS=$2; shift;;
-h) PGHOST=$2; shift;;
-p) PGPORT=$2; shift;;
-d) CANCREATE=t;;
-D) CANCREATE=f;;
-u) CANADDUSER=t;;
-U) CANADDUSER=f;;
-i) SYSID=$2; shift;;
*) NEWUSER=$1;;
esac
shift;
done
if [ -z "$AUTHSYS" ]; then
AUTHOPT=""
else
AUTHOPT="-a $AUTHSYS"
fi
if [ -z "$PGHOST" ]; then
PGHOSTOPT=""
else
PGHOSTOPT="-h $PGHOST"
fi
if [ -z "$PGPORT" ]; then
PGPORTOPT=""
else
PGPORTOPT="-p $PGPORT"
fi
PARGS="-tq $AUTHOPT $PGHOSTOPT $PGPORTOPT"
#
# generate the first part of the actual monitor command
#
PSQL="psql $PARGS"
#
# see if user $USER is a superuser
#
QUERY="select usesuper from pg_user where usename = '$USER' "
#echo $QUERY
ADDUSER=`$PSQL -c "$QUERY" template1`
if [ $? -ne 0 ]
then
echo "$CMDNAME: database access failed." 1>&2
exit 1
fi
if [ -n "$ADDUSER" ]
then
if [ $ADDUSER != "t" ]
then
echo "$CMDNAME: $USER cannot create users." 1>&2
exit 1
fi
fi
#
# get the user name of the new user. Make sure it doesn't already exist.
#
if [ -z "$NEWUSER" ]
then
echo PG_OPT_DASH_N_PARAM "Enter name of user to add ---> PG_OPT_BACKSLASH_C_PARAM"
read NEWUSER
fi
QUERY="select usesysid from pg_user where usename = '$NEWUSER' "
RES=`$PSQL -c "$QUERY" template1`
if [ $? -ne 0 ]
then
echo "$CMDNAME: database access failed." 1>&2
exit 1
fi
if [ -n "$RES" ]
then
echo "$CMDNAME: user "\"$NEWUSER\"" already exists" 1>&2
exit 1
fi
done=0
#
# get the system id of the new user. Make sure it is unique.
#
while [ $done -ne 1 ]
do
DEFSYSID=`pg_id $NEWUSER 2>/dev/null`
if [ $? -eq 0 ]; then
DEFMSG=" or RETURN to use unix user ID: $DEFSYSID"
else
DEFMSG=
DEFSYSID=
fi
while [ -z "$SYSID" ]
do
echo PG_OPT_DASH_N_PARAM "Enter user's postgres ID$DEFMSG -> PG_OPT_BACKSLASH_C_PARAM"
read SYSID
[ -z "$SYSID" ] && SYSID=$DEFSYSID;
SYSIDISNUM=`echo $SYSID | egrep '^[0-9]+$'`
if [ -z "$SYSIDISNUM" ]
then
echo "$CMDNAME: the postgres ID must be a number"
SYSID=
fi
done
QUERY="select usename from pg_user where usesysid = '$SYSID'::int4"
RES=`$PSQL -c "$QUERY" template1`
if [ $? -ne 0 ]
then
echo "$CMDNAME: database access failed."
exit 1
fi
if [ -n "$RES" ]
then
echo
echo "$CMDNAME: $SYSID already belongs to $RES, pick another"
DEFMSG= DEFSYSID= SYSID=
else
done=1
fi
done
#
# get the rest of the user info...
#
#
# can the user create databases?
#
if [ -z "$CANCREATE" ]
then
yn=f
while [ "$yn" != y -a "$yn" != n ]
do
echo PG_OPT_DASH_N_PARAM "Is user \"$NEWUSER\" allowed to create databases (y/n) PG_OPT_BACKSLASH_C_PARAM"
read yn
done
if [ "$yn" = y ]
then
CANCREATE=t
else
CANCREATE=f
fi
fi
#
# can the user add users?
#
if [ -z "$CANADDUSER" ]
then
yn=f
while [ "$yn" != y -a "$yn" != n ]
do
echo PG_OPT_DASH_N_PARAM "Is user \"$NEWUSER\" a superuser? (y/n) PG_OPT_BACKSLASH_C_PARAM"
read yn
done
if (test "$yn" = y)
then
CANADDUSER=t
else
CANADDUSER=f
fi
fi
if [ "$CANCREATE" = "t" -o "$CANADDUSER" = "t" ]
then CANCATUPD="t"
else CANCATUPD="f"
fi
QUERY="insert into pg_shadow \
(usename, usesysid, usecreatedb, usetrace, usesuper, usecatupd) \
values \
('$NEWUSER', $SYSID, '$CANCREATE', 'f', '$CANADDUSER','$CANCATUPD')"
RES=`$PSQL -c "$QUERY" template1`
#
# Wrap things up. If the user was created successfully, AND the user was
# NOT allowed to create databases, remind the DBA to create one for the user.
#
if [ $? -ne 0 ]
then
echo "$CMDNAME: $NEWUSER was NOT added successfully"
else
echo "$CMDNAME: $NEWUSER was successfully added"
if [ "$CANCREATE" = f ]
then
echo PG_OPT_DASH_N_PARAM "Shall I create a database for \"$NEWUSER\" (y/n) PG_OPT_BACKSLASH_C_PARAM"
read yn
if [ "$yn" = y ]
then
createdb $NEWUSER
else
echo "don't forget to create a database for $NEWUSER"
fi
fi
fi

View File

@ -1,28 +0,0 @@
#-------------------------------------------------------------------------
#
# Makefile.inc--
# Makefile for bin/destroydb
#
# Copyright (c) 1994, Regents of the University of California
#
#
# IDENTIFICATION
# $Header: /cvsroot/pgsql/src/bin/destroydb/Attic/Makefile,v 1.8 1998/04/06 16:49:51 momjian Exp $
#
#-------------------------------------------------------------------------
SRCDIR= ../..
include ../../Makefile.global
all: destroydb
destroydb: destroydb.sh
cp destroydb.sh destroydb
install: destroydb
$(INSTALL) $(INSTL_EXE_OPTS) $< $(BINDIR)/$<
clean:
rm -f destroydb
dep depend:

View File

@ -1,85 +0,0 @@
#!/bin/sh
#-------------------------------------------------------------------------
#
# destroydb.sh--
# destroy a postgres database
#
# this program runs the monitor with the ? option to destroy
# the requested database.
#
# Copyright (c) 1994, Regents of the University of California
#
#
# IDENTIFICATION
# $Header: /cvsroot/pgsql/src/bin/destroydb/Attic/destroydb.sh,v 1.9 1999/11/18 21:47:37 momjian Exp $
#
#-------------------------------------------------------------------------
CMDNAME=`basename $0`
if [ -z "$USER" ]; then
if [ -z "$LOGNAME" ]; then
if [ -z "`whoami`" ]; then
echo "$CMDNAME: cannot determine user name"
exit 1
fi
else
USER=$LOGNAME
export USER
fi
fi
dbname=$USER
forcedel=t
while [ -n "$1" ]
do
case $1 in
-i) forcedel=f;;
-a) AUTHSYS=$2; shift;;
-h) PGHOST=$2; shift;;
-p) PGPORT=$2; shift;;
*) dbname=$1;;
esac
shift;
done
if [ -z "$AUTHSYS" ]; then
AUTHOPT=""
else
AUTHOPT="-a $AUTHSYS"
fi
if [ -z "$PGHOST" ]; then
PGHOSTOPT=""
else
PGHOSTOPT="-h $PGHOST"
fi
if [ -z "$PGPORT" ]; then
PGPORTOPT=""
else
PGPORTOPT="-p $PGPORT"
fi
answer=y
if [ "$forcedel" = f ]
then
answer=f
while [ "$answer" != y -a "$answer" != n ]
do
echo "Database '$dbname' will be permanently deleted."
echo -n "Are you sure? (y/n) "
read answer
done
fi
if [ "$answer" = y ]
then
psql -tq $AUTHOPT $PGHOSTOPT $PGPORTOPT -c "drop database \"$dbname\"" template1
if [ $? -ne 0 ]
then echo "$CMDNAME: database destroy failed on $dbname."
exit 1
fi
fi
exit 0

View File

@ -1,32 +0,0 @@
#-------------------------------------------------------------------------
#
# Makefile.inc--
# Makefile for bin/destroyuser
#
# Copyright (c) 1994, Regents of the University of California
#
#
# IDENTIFICATION
# $Header: /cvsroot/pgsql/src/bin/destroyuser/Attic/Makefile,v 1.9 1998/08/22 05:19:19 momjian Exp $
#
#-------------------------------------------------------------------------
SRCDIR= ../..
include ../../Makefile.global
SEDSCRIPT= \
-e "s^PG_OPT_DASH_N_PARAM^$(DASH_N)^g" \
-e "s^PG_OPT_BACKSLASH_C_PARAM^$(BACKSLASH_C)^g"
all: destroyuser
destroyuser: destroyuser.sh
sed $(SEDSCRIPT) <destroyuser.sh >destroyuser
install: destroyuser
$(INSTALL) $(INSTL_EXE_OPTS) $< $(BINDIR)/$<
clean:
rm -f destroyuser
dep depend:

View File

@ -1,196 +0,0 @@
#!/bin/sh
#-------------------------------------------------------------------------
#
# destroyuser.sh--
# utility for destroying a user from the POSTGRES database.
#
# Copyright (c) 1994, Regents of the University of California
#
#
# IDENTIFICATION
# $Header: /cvsroot/pgsql/src/bin/destroyuser/Attic/destroyuser.sh,v 1.11 1999/03/14 16:00:55 momjian Exp $
#
# Note - this should NOT be setuid.
#
#-------------------------------------------------------------------------
CMDNAME=`basename $0`
if [ -z "$USER" ]; then
if [ -z "$LOGNAME" ]; then
if [ -z "`whoami`" ]; then
echo "$CMDNAME: cannot determine user name"
exit 1
fi
else
USER=$LOGNAME
export USER
fi
fi
while (test -n "$1")
do
case $1 in
-a) AUTHSYS=$2; shift;;
-h) PGHOST=$2; shift;;
-p) PGPORT=$2; shift;;
*) DELUSER=$1;;
esac
shift;
done
if [ -z "$AUTHSYS" ]; then
AUTHOPT=""
else
AUTHOPT="-a $AUTHSYS"
fi
if [ -z "$PGHOST" ]; then
PGHOSTOPT=""
else
PGHOSTOPT="-h $PGHOST"
fi
if [ -z "$PGPORT" ]; then
PGPORTOPT=""
else
PGPORTOPT="-p $PGPORT"
fi
PARGS="-tq $AUTHOPT $PGHOSTOPT $PGPORTOPT"
#
# generate the first part of the actual monitor command
#
PSQL="psql $PARGS"
#
# see if user $USER is allowed to create new users. Only a user who can
# create users can delete them.
#
QUERY="select usesuper from pg_user where usename = '$USER'"
ADDUSER=`$PSQL -c "$QUERY" template1`
if [ $? -ne 0 ]
then
echo "$CMDNAME: database access failed."
exit 1
fi
if [ x$ADDUSER != xt ]
then
echo "$CMDNAME: $USER cannot delete users."
exit 1
fi
#
# get the user name of the user to delete. Make sure it exists.
#
if [ -z "$DELUSER" ]
then
echo PG_OPT_DASH_N_PARAM "Enter name of user to delete ---> PG_OPT_BACKSLASH_C_PARAM"
read DELUSER
fi
QUERY="select usesysid from pg_user where usename = '$DELUSER'"
RES=`$PSQL -c "$QUERY" template1`
if [ $? -ne 0 ]
then
echo "$CMDNAME: database access failed."
exit 1
fi
if [ ! -n "$RES" ]
then
echo "$CMDNAME: user "\"$DELUSER\"" does not exist."
exit 1
fi
SYSID=`echo $RES | sed 's/ //g'`
#
# destroy the databases owned by the deleted user. First, use this query
# to find out what they are.
#
QUERY="select datname from pg_database where datdba = '$SYSID'::oid"
ALLDBS=`$PSQL -c "$QUERY" template1`
if [ $? -ne 0 ]
then
echo "$CMDNAME: database access failed - exiting..."
exit 1
fi
#
# don't try to delete template1!
#
for i in $ALLDBS
do
if [ $i != "template1" ]
then
DBLIST="$DBLIST $i"
fi
done
if [ -n "$DBLIST" ]
then
echo "User $DELUSER owned the following databases:"
echo $DBLIST
echo
#
# Now we warn the DBA that deleting this user will destroy a bunch of databases
#
yn=f
while [ "$yn" != y -a "$yn" != n ]
do
echo PG_OPT_DASH_N_PARAM "Deleting user $DELUSER will destroy them. Continue (y/n)? PG_OPT_BACKSLASH_C_PARAM"
read yn
done
if [ $yn = n ]
then
echo "$CMDNAME: exiting"
exit 1
fi
#
# now actually destroy the databases
#
for i in $DBLIST
do
echo "destroying database $i"
QUERY="drop database $i"
$PSQL -c "$QUERY" template1
if [ $? -ne 0 ]
then
echo "$CMDNAME: drop database on $i failed - exiting"
exit 1
fi
done
fi
QUERY="delete from pg_shadow where usename = '$DELUSER'"
$PSQL -c "$QUERY" template1
if [ $? -ne 0 ]
then
echo "$CMDNAME: delete of user $DELUSER was UNSUCCESSFUL"
else
echo "$CMDNAME: delete of user $DELUSER was successful."
fi
exit 0

33
src/bin/scripts/Makefile Normal file
View File

@ -0,0 +1,33 @@
#-------------------------------------------------------------------------
#
# Makefile.inc--
# Makefile for bin/scripts
#
# Copyright (c) 1994, Regents of the University of California
#
#
# IDENTIFICATION
# $Header: /cvsroot/pgsql/src/bin/scripts/Makefile,v 1.1 1999/12/04 04:53:21 momjian Exp $
#
#-------------------------------------------------------------------------
.SUFFIXES:
SRCDIR=../..
include ../../Makefile.global
SCRIPTS=createdb dropdb createuser dropuser vacuumdb
all: $(SCRIPTS)
createdb:
dropdb:
createuser:
dropuser:
vacuumdb:
install: $(SCRIPTS)
$(INSTALL) $(INSTL_EXE_OPTS) $< $(BINDIR)/$(X)$<
clean:
dep depend:

112
src/bin/scripts/createdb Normal file
View File

@ -0,0 +1,112 @@
#!/bin/sh
#-------------------------------------------------------------------------
#
# createdb.sh--
# create a postgres database
#
# This program runs psql with the "-c" option to create
# the requested database.
#
# Copyright (c) 1994, Regents of the University of California
#
#
# IDENTIFICATION
# $Header: /cvsroot/pgsql/src/bin/scripts/Attic/createdb,v 1.1 1999/12/04 04:53:21 momjian Exp $
#
#-------------------------------------------------------------------------
CMDNAME=`basename $0`
MB=
PSQLOPT=
dbname=
dbcomment=
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;;
--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
--dbpath|-D)
dbpath="$2"
shift;;
--encoding|-E)
MB=$2
shift
if [ -z `pg_encoding $MB` ]; then
echo "$CMDNAME: $MB is not a valid encoding name"
exit 1
fi
;;
-*)
echo "$CMDNAME: Unrecognized option: $1. Try -? for help."
exit 1
;;
*)
if [ -z "$dbname" ]; then
dbname="$1"
else
dbcomment="$1"
fi
;;
esac
shift
done
if [ "$usage" ]; then
echo "Usage: $CMDNAME [-h <server>] [-p <port>] [-D <path>] \\"
echo " [-E <encoding>] [-U <username>] [-W] dbname [description]"
exit 0
fi
if [ -z "$dbname" ]; then
echo "$CMDNAME: Missing required argument database name. Try -? for help."
exit 1
fi
withstring=
[ "$dbpath" ] && withstring="$withstring LOCATION = '$dbpath'"
[ "$MB" ] && withstring="$withstring ENCODING = '$MB'"
[ "$withstring" ] && withstring=" WITH$withstring"
psql $PSQLOPT -d template1 -c "CREATE DATABASE \"$dbname\"$withstring"
if [ $? -ne 0 ]; then
echo "$CMDNAME: Database creation failed."
exit 1
fi
# Insert comment as well, if requested
[ -z "$dbcomment" ] && exit 0
psql $PSQLOPT -d template1 -c "COMMENT ON DATABASE \"$dbname\" IS \'$dbcomment\'"
if [ $? -ne 0 ]; then
echo "$CMDNAME: Comment creation failed."
exit 1
fi
exit 0

145
src/bin/scripts/createuser Normal file
View File

@ -0,0 +1,145 @@
#!/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

92
src/bin/scripts/dropdb Normal file
View File

@ -0,0 +1,92 @@
#!/bin/sh
#-------------------------------------------------------------------------
#
# dropdb--
# destroy a postgres database
#
# this program runs psql to drop the requested database.
#
# Copyright (c) 1994, Regents of the University of California
#
#
# IDENTIFICATION
# $Header: /cvsroot/pgsql/src/bin/scripts/Attic/dropdb,v 1.1 1999/12/04 04:53:21 momjian Exp $
#
#-------------------------------------------------------------------------
CMDNAME=`basename $0`
PSQLOPT=
dbname=
forcedel=t
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;;
--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"
;;
# other options
--interactive|-i)
forcedel=f
;;
-*)
echo "$CMDNAME: Unrecognized option: $1. Try -? for help."
exit 1
;;
*)
dbname="$1"
;;
esac
shift
done
if [ "$usage" ]; then
echo "Usage: $CMDNAME [-h <server>] [-p <port>] [-U <username>] [-W] [-i] dbname"
exit 0
fi
if [ -z "$dbname" ]; then
echo "$CMDNAME: Missing required argument database name. Try -? for help."
exit 1
fi
if [ "$forcedel" = f ]; then
echo "Database \"$dbname\" will be permanently deleted."
echo -n "Are you sure? (y/n) "
read -r
[ $? -eq 1 ] && exit 1
[ "$REPLY" != "y" -a "$REPLY" != "Y" ] && exit 0
fi
psql $PSQLOPT -d template1 -c "DROP DATABASE \"$dbname\""
if [ $? -ne 0 ]; then
echo "$CMDNAME: Database removal failed."
exit 1
fi
exit 0

98
src/bin/scripts/dropuser Normal file
View File

@ -0,0 +1,98 @@
#!/bin/sh
#-------------------------------------------------------------------------
#
# dropuser--
# Utility for remocing 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.1 1999/12/04 04:53:21 momjian Exp $
#
# Note - this should NOT be setuid.
#
#-------------------------------------------------------------------------
CMDNAME=`basename $0`
PSQLOPT=
forcedel=t
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"
;;
# other options
--interactive|-i)
forcedel=f
;;
-*)
echo "$CMDNAME: Unrecognized option: $1. Try -? for help."
exit 1
;;
*)
DelUser="$1"
;;
esac
shift;
done
# Help
if [ "$usage" ]; then
echo "Usage: $CMDNAME [-h <server>] [-p <port>] [-i] [username]"
exit 0
fi
# Prompt for username if missing
if [ -z "$DelUser" ]; then
echo -n "Enter name of user to delete: "
read -r NewUser
[ $? -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) "
read -r
[ $? -eq 1 ] && exit 1
[ "$REPLY" != "y" -a "$REPLY" != "Y" ] && exit 0
fi
psql $PSQLOPT -d template1 -c "DROP USER \"$DelUser\""
if [ $? -ne 0 ]; then
echo "$CMDNAME: Deletion of user \"$DelUser\" failed."
exit 1
fi
exit 0

96
src/bin/scripts/vacuumdb Normal file
View File

@ -0,0 +1,96 @@
#!/bin/sh
#-------------------------------------------------------------------------
#
# vacuumdb--
# vacuum a postgres database
#
# This script runs psql with the "-c" option to vacuum
# the requested database.
#
# Copyright (c) 1994, Regents of the University of California
#
#
# IDENTIFICATION
# $Header: /cvsroot/pgsql/src/bin/scripts/Attic/vacuumdb,v 1.1 1999/12/04 04:53:21 momjian Exp $
#
#-------------------------------------------------------------------------
CMDNAME=`basename $0`
PSQLOPT=
verbose=
analyze=
table=
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;;
--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"
;;
--dbname|--database|-d)
dbname="$2"
shift;;
# options converted into SQL command
--analyze|-z)
analyze="analyze"
;;
--table|-t)
table=$2
shift;;
--verbose|-v)
verbose="verbose"
;;
-*)
echo "$CMDNAME: Unrecognized option: $1. Try -? for help."
exit 1
;;
*)
dbname="$1"
;;
esac
shift
done
if [ "$usage" ]; then
echo "Usage: $CMDNAME [-h <server>] [-p <port>] [-U <username>] [-W] [-d <dbname>] \\"
echo " [-z|--analyze] [-v|--verbose] [-t|--table 'table[(columns)]'] [dbname]"
exit 0
fi
if [ -z "$dbname" ]; then
echo "$CMDNAME: Missing required argument database name. Try -? for help."
exit 1
fi
psql $PSQLOPT -d "$dbname" -c "VACUUM $verbose $analyze $table"
if [ $? -ne 0 ]; then
echo "$CMDNAME: Database vacuum failed."
exit 1
fi
exit 0

View File

@ -1,24 +0,0 @@
#-------------------------------------------------------------------------
#
# Makefile--
# Makefile for bin/vacuumdb
#
# Copyright (c) 1994, Regents of the University of California
#
#
# IDENTIFICATION
# $Header: /cvsroot/pgsql/src/bin/vacuumdb/Attic/Makefile,v 1.1 1998/11/14 01:58:14 thomas Exp $
#
#-------------------------------------------------------------------------
SRCDIR= ../..
include ../../Makefile.global
all: vacuumdb
install: vacuumdb
$(INSTALL) $(INSTL_EXE_OPTS) $< $(BINDIR)/$<
clean:
dep depend:

View File

@ -1,98 +0,0 @@
#!/bin/sh
#-------------------------------------------------------------------------
#
# vacuumdb--
# vacuum a postgres database
#
# this program runs the monitor with the "-c" option to vacuum
# the requested database.
#
# Copyright (c) 1994, Regents of the University of California
#
#
# IDENTIFICATION
# $Header: /cvsroot/pgsql/src/bin/vacuumdb/Attic/vacuumdb,v 1.1 1998/11/14 01:58:15 thomas Exp $
#
#-------------------------------------------------------------------------
CMDNAME=`basename $0`
if [ -z "$USER" ]; then
if [ -z "$LOGNAME" ]; then
if [ -z "`whoami`" ]; then
echo "$CMDNAME: cannot determine user name"
exit 1
fi
else
USER=$LOGNAME
export USER
fi
fi
dbname=$USER
PASSWDOPT="";
while test -n "$1"
do
case $1 in
--help) usage=1;;
--analyze) analyze="analyze";;
--table) table=$2; shift;;
--verbose) verbose="verbose";;
-a) AUTHSYS=$2; shift;;
-h) PGHOST=$2; shift;;
-p) PGPORT=$2; shift;;
-t) table=$2; shift;;
-u) PASSWDOPT=$1;;
-v) verbose="verbose";;
-z) analyze="analyze";;
-*) echo "$CMDNAME: unrecognized parameter $1"; usage=1;;
*) dbname=$1;;
esac
shift;
done
if [ "$usage" ]; then
echo "Usage: $CMDNAME -a <authtype> -h <server> -p <portnumber> --analyze --verbose [--table 'table[(cols)]'] [dbname]"
exit 1
fi
if [ -z "$AUTHSYS" ]; then
AUTHOPT=""
else
AUTHOPT="-a $AUTHSYS"
fi
if [ -z "$PGHOST" ]; then
PGHOSTOPT=""
else
PGHOSTOPT="-h $PGHOST"
fi
if [ -z "$PGPORT" ]; then
PGPORTOPT=""
else
PGPORTOPT="-p $PGPORT"
fi
if [ -z "$dbpath" ]; then
location=""
else
# if [ ! -d "$dbpath"/base ]; then
# echo "$CMDNAME: database creation failed on $dbname."
# echo "directory $dbpath/base not found."
# exit 1
# fi
location="with location = '$dbpath'"
fi
psql $PASSWDOPT -tq $AUTHOPT $PGHOSTOPT $PGPORTOPT -c "vacuum $verbose $analyze $table" $dbname
if [ $? -ne 0 ]; then
echo "$CMDNAME: database vacuum failed on $dbname."
exit 1
fi
exit 0