aboutsummaryrefslogtreecommitdiff
path: root/src/bin/scripts/createdb
blob: 3601811b24910b956a9037938fa5a33c97237d0c (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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#!/bin/sh
#-------------------------------------------------------------------------
#
# createdb--
#    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.9 2000/11/11 22:59:48 petere Exp $
#
#-------------------------------------------------------------------------

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

MB=
PSQLOPT=
dbname=
dbcomment=
dbpath=

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=//'`
                ;;
	--username|-U)
		PSQLOPT="$PSQLOPT -U $2"
		shift;;
        -U*)
                PSQLOPT="$PSQLOPT $1"
                ;;
        --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"
		;;
# options converted into SQL command
	--location|-D)
		dbpath="$2"
		shift;;
        -D*)
                dbpath=`echo $1 | sed 's/^-D//'`
                ;;
        --location=*)
                dbpath=`echo $1 | sed 's/^--location=//'`
                ;;
	--encoding|-E)
		MB=$2
		shift;;
        -E*)
                MB=`echo $1 | sed 's/^-E//'`
                ;;
        --encoding=*)
                MB=`echo $1 | sed 's/^--encoding=//'`
                ;;
	-*)
		echo "$CMDNAME: invalid option: $1" 1>&2
                echo "Try '$CMDNAME -?' for help." 1>&2
		exit 1
		;;
	*)
		if [ -z "$dbname" ]; then
			dbname="$1"
		else
			dbcomment="$1"
		fi
		;;
    esac
    shift
done

if [ "$usage" ]; then
        echo "$CMDNAME creates a PostgreSQL database."
        echo
	echo "Usage:"
        echo "  $CMDNAME [options] dbname [description]"
        echo
	echo "Options:"
	echo "  -D, --location=PATH             Alternative place to store the database"
	echo "  -E, --encoding=ENCODING         Multibyte encoding for the database"
	echo "  -h, --host=HOSTNAME             Database server host"
	echo "  -p, --port=PORT                 Database server port"
	echo "  -U, --username=USERNAME         Username to connect as"
	echo "  -W, --password                  Prompt for password"
	echo "  -e, --echo                      Show the query being sent to the backend"
        echo "  -q, --quiet                     Don't write any messages"
        echo
        echo "By default, a database with the same name as the current user is created."
	echo
	echo "Report bugs to <pgsql-bugs@postgresql.org>."
	exit 0
fi


if [ -n "$MB" ]
then
        mbcode=`${PATHNAME}pg_encoding "$MB"`
        if [ -z "$mbcode" ]
	then
		echo "$CMDNAME: \"$MB\" is not a valid encoding name" 1>&2
		exit 1
	fi
fi

if [ -z "$dbname" ]; then
        if [ "$PGUSER" ]; then
                dbname=$PGUSER
        else
                dbname=`${PATHNAME}pg_id -u -n`
        fi
        [ $? -ne 0 ] && exit 1
fi


# escape the quotes
dbpath=`echo $dbpath | sed "s/'/\\\\\'/g"`
dbname=`echo $dbname | sed 's/\"/\\\"/g'`

withstring=
[ "$dbpath" ] &&     withstring="$withstring LOCATION = '$dbpath'"
[ "$MB" ] &&         withstring="$withstring ENCODING = '$MB'"
[ "$withstring" ] && withstring=" WITH$withstring"

${PATHNAME}psql $PSQLOPT -d template1 -c "CREATE DATABASE \"$dbname\"$withstring"
if [ $? -ne 0 ]; then
	echo "$CMDNAME: database creation failed" 1>&2
	exit 1
fi

# Insert comment as well, if requested
[ -z "$dbcomment" ] && exit 0

dbcomment=`echo $dbcomment | sed "s/'/\\\\\'/g"`

${PATHNAME}psql $PSQLOPT -d template1 -c "COMMENT ON DATABASE \"$dbname\" IS '$dbcomment'"
if [ $? -ne 0 ]; then
	echo "$CMDNAME: comment creation failed (database was created)" 1>&2
	exit 1
fi

exit 0