aboutsummaryrefslogtreecommitdiff
path: root/src/bin/createdb/createdb.sh
blob: 2217f19714a32708ab3bc941626f2ec5b4d5c8ac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#!/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.9 1998/07/24 03:32:06 scrappy Exp $
#
#-------------------------------------------------------------------------

CMDNAME=`basename $0`

MBENABLED=__MB__
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]"
	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