diff options
| author | Bruce Momjian | 1999-12-04 04:53:22 +0000 |
|---|---|---|
| committer | Bruce Momjian | 1999-12-04 04:53:22 +0000 |
| commit | 240e4c98f5f41d83d3c887d26e2dbfd9bd849d00 (patch) | |
| tree | 490d2b494601e7b36167dc997908ffeaf92567f3 /src/bin/scripts | |
| parent | 21b69148dce2a7d178d1c4cd060a1ed06fe7b3b4 (diff) | |
New scripts for create/drop user/db from Peter Eisentraut
Diffstat (limited to 'src/bin/scripts')
| -rw-r--r-- | src/bin/scripts/Makefile | 33 | ||||
| -rw-r--r-- | src/bin/scripts/createdb | 112 | ||||
| -rw-r--r-- | src/bin/scripts/createuser | 145 | ||||
| -rw-r--r-- | src/bin/scripts/dropdb | 92 | ||||
| -rw-r--r-- | src/bin/scripts/dropuser | 98 | ||||
| -rw-r--r-- | src/bin/scripts/vacuumdb | 96 |
6 files changed, 576 insertions, 0 deletions
diff --git a/src/bin/scripts/Makefile b/src/bin/scripts/Makefile new file mode 100644 index 00000000000..447a021640c --- /dev/null +++ b/src/bin/scripts/Makefile @@ -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: diff --git a/src/bin/scripts/createdb b/src/bin/scripts/createdb new file mode 100644 index 00000000000..43533a30ff4 --- /dev/null +++ b/src/bin/scripts/createdb @@ -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
\ No newline at end of file diff --git a/src/bin/scripts/createuser b/src/bin/scripts/createuser new file mode 100644 index 00000000000..70e095bf2a7 --- /dev/null +++ b/src/bin/scripts/createuser @@ -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
\ No newline at end of file diff --git a/src/bin/scripts/dropdb b/src/bin/scripts/dropdb new file mode 100644 index 00000000000..96a0a10cb62 --- /dev/null +++ b/src/bin/scripts/dropdb @@ -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
\ No newline at end of file diff --git a/src/bin/scripts/dropuser b/src/bin/scripts/dropuser new file mode 100644 index 00000000000..da2da3cf5a4 --- /dev/null +++ b/src/bin/scripts/dropuser @@ -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 diff --git a/src/bin/scripts/vacuumdb b/src/bin/scripts/vacuumdb new file mode 100644 index 00000000000..29f295c49cd --- /dev/null +++ b/src/bin/scripts/vacuumdb @@ -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 |
