1 /*-------------------------------------------------------------------------
5 * Portions Copyright (c) 1996-2003, 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.10 2004/06/01 02:54:09 momjian 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]);
63 set_pglocale(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));
154 appendPQExpBuffer(&sql, " LOCATION ");
155 appendStringLiteral(&sql, location, false);
158 appendPQExpBuffer(&sql, " ENCODING '%s'", encoding);
160 appendPQExpBuffer(&sql, " TEMPLATE %s", fmtId(template));
161 appendPQExpBuffer(&sql, ";\n");
163 conn = connectDatabase("template1", host, port, username, password, progname);
166 printf("%s", sql.data);
167 result = PQexec(conn, sql.data);
169 if (PQresultStatus(result) != PGRES_COMMAND_OK)
171 fprintf(stderr, _("%s: database creation failed: %s"),
172 progname, PQerrorMessage(conn));
182 puts("CREATE DATABASE");
188 printfPQExpBuffer(&sql, "COMMENT ON DATABASE %s IS ", fmtId(dbname));
189 appendStringLiteral(&sql, comment, false);
190 appendPQExpBuffer(&sql, ";\n");
192 conn = connectDatabase(dbname, host, port, username, password, progname);
194 printf("%s", sql.data);
195 result = PQexec(conn, sql.data);
197 if (PQresultStatus(result) != PGRES_COMMAND_OK)
199 fprintf(stderr, _("%s: comment creation failed (database was created): %s"),
200 progname, PQerrorMessage(conn));
218 help(const char *progname)
220 printf(_("%s creates a PostgreSQL database.\n\n"), progname);
221 printf(_("Usage:\n"));
222 printf(_(" %s [OPTION]... [DBNAME] [DESCRIPTION]\n"), progname);
223 printf(_("\nOptions:\n"));
224 printf(_(" -D, --location=PATH alternative place to store the database\n"));
225 printf(_(" -E, --encoding=ENCODING encoding for the database\n"));
226 printf(_(" -O, --owner=OWNER database user to own the new database\n"));
227 printf(_(" -T, --template=TEMPLATE template database to copy\n"));
228 printf(_(" -e, --echo show the commands being sent to the server\n"));
229 printf(_(" -q, --quiet don't write any messages\n"));
230 printf(_(" --help show this help, then exit\n"));
231 printf(_(" --version output version information, then exit\n"));
232 printf(_("\nConnection options:\n"));
233 printf(_(" -h, --host=HOSTNAME database server host or socket directory\n"));
234 printf(_(" -p, --port=PORT database server port\n"));
235 printf(_(" -U, --username=USERNAME user name to connect as\n"));
236 printf(_(" -W, --password prompt for password\n"));
237 printf(_("\nBy default, a database with the same name as the current user is created.\n"));
238 printf(_("\nReport bugs to <pgsql-bugs@postgresql.org>.\n"));