1
/*-------------------------------------------------------------------------
5
* Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
6
* Portions Copyright (c) 1994, Regents of the University of California
8
* $PostgreSQL: pgsql/src/bin/scripts/createdb.c,v 1.14 2004-12-31 22:03:17 pgsql Exp $
10
*-------------------------------------------------------------------------
13
#include "postgres_fe.h"
15
#include "dumputils.h"
17
#include "mb/pg_wchar.h"
20
static void help(const char *progname);
24
main(int argc, char *argv[])
26
static struct option long_options[] = {
27
{"host", required_argument, NULL, 'h'},
28
{"port", required_argument, NULL, 'p'},
29
{"username", required_argument, NULL, 'U'},
30
{"password", no_argument, NULL, 'W'},
31
{"echo", no_argument, NULL, 'e'},
32
{"quiet", no_argument, NULL, 'q'},
33
{"owner", required_argument, NULL, 'O'},
34
{"tablespace", required_argument, NULL, 'D'},
35
{"template", required_argument, NULL, 'T'},
36
{"encoding", required_argument, NULL, 'E'},
44
const char *dbname = NULL;
48
char *username = NULL;
49
bool password = false;
53
char *tablespace = NULL;
54
char *template = NULL;
55
char *encoding = NULL;
62
progname = get_progname(argv[0]);
63
set_pglocale_pgservice(argv[0], "pgscripts");
65
handle_help_version_opts(argc, argv, "createdb", help);
67
while ((c = getopt_long(argc, argv, "h:p:U:WeqO:D:T:E:", long_options, &optindex)) != -1)
102
fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
107
switch (argc - optind)
112
dbname = argv[optind];
115
dbname = argv[optind];
116
comment = argv[optind + 1];
119
fprintf(stderr, _("%s: too many command-line arguments (first is \"%s\")\n"),
120
progname, argv[optind + 2]);
121
fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
127
if (pg_char_to_encoding(encoding) < 0)
129
fprintf(stderr, _("%s: \"%s\" is not a valid encoding name\n"),
137
if (getenv("PGDATABASE"))
138
dbname = getenv("PGDATABASE");
139
else if (getenv("PGUSER"))
140
dbname = getenv("PGUSER");
142
dbname = get_user_name(progname);
145
initPQExpBuffer(&sql);
147
appendPQExpBuffer(&sql, "CREATE DATABASE %s",
151
appendPQExpBuffer(&sql, " OWNER %s", fmtId(owner));
153
appendPQExpBuffer(&sql, " TABLESPACE %s", fmtId(tablespace));
155
appendPQExpBuffer(&sql, " ENCODING '%s'", encoding);
157
appendPQExpBuffer(&sql, " TEMPLATE %s", fmtId(template));
158
appendPQExpBuffer(&sql, ";\n");
160
conn = connectDatabase("template1", host, port, username, password, progname);
163
printf("%s", sql.data);
164
result = PQexec(conn, sql.data);
166
if (PQresultStatus(result) != PGRES_COMMAND_OK)
168
fprintf(stderr, _("%s: database creation failed: %s"),
169
progname, PQerrorMessage(conn));
179
puts("CREATE DATABASE");
185
printfPQExpBuffer(&sql, "COMMENT ON DATABASE %s IS ", fmtId(dbname));
186
appendStringLiteral(&sql, comment, false);
187
appendPQExpBuffer(&sql, ";\n");
189
conn = connectDatabase(dbname, host, port, username, password, progname);
191
printf("%s", sql.data);
192
result = PQexec(conn, sql.data);
194
if (PQresultStatus(result) != PGRES_COMMAND_OK)
196
fprintf(stderr, _("%s: comment creation failed (database was created): %s"),
197
progname, PQerrorMessage(conn));
215
help(const char *progname)
217
printf(_("%s creates a PostgreSQL database.\n\n"), progname);
218
printf(_("Usage:\n"));
219
printf(_(" %s [OPTION]... [DBNAME] [DESCRIPTION]\n"), progname);
220
printf(_("\nOptions:\n"));
221
printf(_(" -D, --tablespace=TABLESPACE default tablespace for the database\n"));
222
printf(_(" -E, --encoding=ENCODING encoding for the database\n"));
223
printf(_(" -O, --owner=OWNER database user to own the new database\n"));
224
printf(_(" -T, --template=TEMPLATE template database to copy\n"));
225
printf(_(" -e, --echo show the commands being sent to the server\n"));
226
printf(_(" -q, --quiet don't write any messages\n"));
227
printf(_(" --help show this help, then exit\n"));
228
printf(_(" --version output version information, then exit\n"));
229
printf(_("\nConnection options:\n"));
230
printf(_(" -h, --host=HOSTNAME database server host or socket directory\n"));
231
printf(_(" -p, --port=PORT database server port\n"));
232
printf(_(" -U, --username=USERNAME user name to connect as\n"));
233
printf(_(" -W, --password prompt for password\n"));
234
printf(_("\nBy default, a database with the same name as the current user is created.\n"));
235
printf(_("\nReport bugs to <pgsql-bugs@postgresql.org>.\n"));