1 /*-------------------------------------------------------------------------
5 * Portions Copyright (c) 1996-2003, PostgreSQL Global Development Group
6 * Portions Copyright (c) 1994, Regents of the University of California
8 * $Header: /cvsroot/pgsql/src/bin/scripts/createdb.c,v 1.2 2003/05/14 03:26:03 tgl 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 {"location", 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 *location = NULL;
54 char *template = NULL;
55 char *encoding = NULL;
62 progname = get_progname(argv[0]);
64 handle_help_version_opts(argc, argv, "createdb", help);
66 while ((c = getopt_long(argc, argv, "h:p:U:WeqO:D:T:E:", long_options, &optindex)) != -1)
101 fprintf(stderr, _("Try '%s --help' for more information.\n"), progname);
106 switch (argc - optind)
111 dbname = argv[optind];
114 dbname = argv[optind];
115 comment = argv[optind + 1];
118 fprintf(stderr, _("%s: too many command-line arguments (first is '%s')\n"),
119 progname, argv[optind + 2]);
120 fprintf(stderr, _("Try '%s --help' for more information.\n"), progname);
126 if (pg_char_to_encoding(encoding) < 0)
128 fprintf(stderr, _("%s: \"%s\" is not a valid encoding name\n"),
136 if (getenv("PGDATABASE"))
137 dbname = getenv("PGDATABASE");
138 else if (getenv("PGUSER"))
139 dbname = getenv("PGUSER");
141 dbname = get_user_name(progname);
144 initPQExpBuffer(&sql);
146 appendPQExpBuffer(&sql, "CREATE DATABASE %s",
150 appendPQExpBuffer(&sql, " OWNER %s", fmtId(owner));
153 appendPQExpBuffer(&sql, " LOCATION ");
154 appendStringLiteral(&sql, location, false);
157 appendPQExpBuffer(&sql, " ENCODING '%s'", encoding);
159 appendPQExpBuffer(&sql, " TEMPLATE %s", fmtId(template));
160 appendPQExpBuffer(&sql, ";\n");
162 conn = connectDatabase("template1", host, port, username, password, progname);
165 printf("%s", sql.data);
166 result = PQexec(conn, sql.data);
168 if (PQresultStatus(result) != PGRES_COMMAND_OK)
170 fprintf(stderr, _("%s: database creation failed: %s"),
171 progname, PQerrorMessage(conn));
180 puts("CREATE DATABASE");
184 printfPQExpBuffer(&sql, "COMMENT ON DATABASE %s IS ", fmtId(dbname));
185 appendStringLiteral(&sql, comment, false);
186 appendPQExpBuffer(&sql, ";\n");
188 conn = connectDatabase(dbname, host, port, username, password, progname);
190 printf("%s", sql.data);
191 result = PQexec(conn, sql.data);
193 if (PQresultStatus(result) != PGRES_COMMAND_OK)
195 fprintf(stderr, _("%s: comment creation failed (database was created): %s"),
196 progname, PQerrorMessage(conn));
211 help(const char *progname)
213 printf(_("%s creates a PostgreSQL database.\n\n"), progname);
214 printf(_("Usage:\n"));
215 printf(_(" %s [OPTION]... [DBNAME] [DESCRIPTION]\n"), progname);
216 printf(_("\nOptions:\n"));
217 printf(_(" -D, --location=PATH alternative place to store the database\n"));
218 printf(_(" -E, --encoding=ENCODING encoding for the database\n"));
219 printf(_(" -O, --owner=OWNER database user to own the new database\n"));
220 printf(_(" -T, --template=TEMPLATE template database to copy\n"));
221 printf(_(" -e, --echo show the commands being sent to the server\n"));
222 printf(_(" -q, --quiet don't write any messages\n"));
223 printf(_(" --help show this help, then exit\n"));
224 printf(_(" --version output version information, then exit\n"));
225 printf(_("\nConnection options:\n"));
226 printf(_(" -h, --host=HOSTNAME database server host\n"));
227 printf(_(" -p, --port=PORT database server port\n"));
228 printf(_(" -U, --username=USERNAME user name to connect as\n"));
229 printf(_(" -W, --password prompt for password\n"));
230 printf(_("\nBy default, a database with the same name as the current user is created.\n"));
231 printf(_("\nReport bugs to <pgsql-bugs@postgresql.org>.\n"));