1 /*-------------------------------------------------------------------------
5 * Portions Copyright (c) 1996-2011, PostgreSQL Global Development Group
6 * Portions Copyright (c) 1994, Regents of the University of California
8 * src/bin/scripts/createdb.c
10 *-------------------------------------------------------------------------
12 #include "postgres_fe.h"
15 #include "dumputils.h"
18 static void help(const char *progname);
22 main(int argc, char *argv[])
24 static struct option long_options[] = {
25 {"host", required_argument, NULL, 'h'},
26 {"port", required_argument, NULL, 'p'},
27 {"username", required_argument, NULL, 'U'},
28 {"no-password", no_argument, NULL, 'w'},
29 {"password", no_argument, NULL, 'W'},
30 {"echo", no_argument, NULL, 'e'},
31 {"owner", required_argument, NULL, 'O'},
32 {"tablespace", required_argument, NULL, 'D'},
33 {"template", required_argument, NULL, 'T'},
34 {"encoding", required_argument, NULL, 'E'},
35 {"lc-collate", required_argument, NULL, 1},
36 {"lc-ctype", required_argument, NULL, 2},
37 {"locale", required_argument, NULL, 'l'},
45 const char *dbname = NULL;
49 char *username = NULL;
50 enum trivalue prompt_password = TRI_DEFAULT;
53 char *tablespace = NULL;
54 char *template = NULL;
55 char *encoding = NULL;
56 char *lc_collate = NULL;
57 char *lc_ctype = NULL;
65 progname = get_progname(argv[0]);
66 set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pgscripts"));
68 handle_help_version_opts(argc, argv, "createdb", help);
70 while ((c = getopt_long(argc, argv, "h:p:U:wWeO:D:T:E:l:", long_options, &optindex)) != -1)
84 prompt_password = TRI_NO;
87 prompt_password = TRI_YES;
114 fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
119 switch (argc - optind)
124 dbname = argv[optind];
127 dbname = argv[optind];
128 comment = argv[optind + 1];
131 fprintf(stderr, _("%s: too many command-line arguments (first is \"%s\")\n"),
132 progname, argv[optind + 2]);
133 fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
141 fprintf(stderr, _("%s: only one of --locale and --lc-ctype can be specified\n"),
147 fprintf(stderr, _("%s: only one of --locale and --lc-collate can be specified\n"),
157 if (pg_char_to_encoding(encoding) < 0)
159 fprintf(stderr, _("%s: \"%s\" is not a valid encoding name\n"),
167 if (getenv("PGDATABASE"))
168 dbname = getenv("PGDATABASE");
169 else if (getenv("PGUSER"))
170 dbname = getenv("PGUSER");
172 dbname = get_user_name(progname);
175 initPQExpBuffer(&sql);
177 appendPQExpBuffer(&sql, "CREATE DATABASE %s",
181 appendPQExpBuffer(&sql, " OWNER %s", fmtId(owner));
183 appendPQExpBuffer(&sql, " TABLESPACE %s", fmtId(tablespace));
185 appendPQExpBuffer(&sql, " ENCODING '%s'", encoding);
187 appendPQExpBuffer(&sql, " TEMPLATE %s", fmtId(template));
189 appendPQExpBuffer(&sql, " LC_COLLATE '%s'", lc_collate);
191 appendPQExpBuffer(&sql, " LC_CTYPE '%s'", lc_ctype);
193 appendPQExpBuffer(&sql, ";\n");
196 * Connect to the 'postgres' database by default, except have
197 * the 'postgres' user use 'template1' so he can create the
198 * 'postgres' database.
200 conn = connectDatabase(strcmp(dbname, "postgres") == 0 ? "template1" : "postgres",
201 host, port, username, prompt_password, progname);
204 printf("%s", sql.data);
205 result = PQexec(conn, sql.data);
207 if (PQresultStatus(result) != PGRES_COMMAND_OK)
209 fprintf(stderr, _("%s: database creation failed: %s"),
210 progname, PQerrorMessage(conn));
219 printfPQExpBuffer(&sql, "COMMENT ON DATABASE %s IS ", fmtId(dbname));
220 appendStringLiteralConn(&sql, comment, conn);
221 appendPQExpBuffer(&sql, ";\n");
224 printf("%s", sql.data);
225 result = PQexec(conn, sql.data);
227 if (PQresultStatus(result) != PGRES_COMMAND_OK)
229 fprintf(stderr, _("%s: comment creation failed (database was created): %s"),
230 progname, PQerrorMessage(conn));
245 help(const char *progname)
247 printf(_("%s creates a PostgreSQL database.\n\n"), progname);
248 printf(_("Usage:\n"));
249 printf(_(" %s [OPTION]... [DBNAME] [DESCRIPTION]\n"), progname);
250 printf(_("\nOptions:\n"));
251 printf(_(" -D, --tablespace=TABLESPACE default tablespace for the database\n"));
252 printf(_(" -e, --echo show the commands being sent to the server\n"));
253 printf(_(" -E, --encoding=ENCODING encoding for the database\n"));
254 printf(_(" -l, --locale=LOCALE locale settings for the database\n"));
255 printf(_(" --lc-collate=LOCALE LC_COLLATE setting for the database\n"));
256 printf(_(" --lc-ctype=LOCALE LC_CTYPE setting for the database\n"));
257 printf(_(" -O, --owner=OWNER database user to own the new database\n"));
258 printf(_(" -T, --template=TEMPLATE template database to copy\n"));
259 printf(_(" --help show this help, then exit\n"));
260 printf(_(" --version output version information, then exit\n"));
261 printf(_("\nConnection options:\n"));
262 printf(_(" -h, --host=HOSTNAME database server host or socket directory\n"));
263 printf(_(" -p, --port=PORT database server port\n"));
264 printf(_(" -U, --username=USERNAME user name to connect as\n"));
265 printf(_(" -w, --no-password never prompt for password\n"));
266 printf(_(" -W, --password force password prompt\n"));
267 printf(_("\nBy default, a database with the same name as the current user is created.\n"));
268 printf(_("\nReport bugs to <pgsql-bugs@postgresql.org>.\n"));