Sort the output of --help mostly alphabetical, make it align better, make
[PostgreSQL.git] / src / bin / scripts / createdb.c
blob180dc5a6c4a1fd4e0771a271b58ab3bf4b7ba2f5
1 /*-------------------------------------------------------------------------
3 * createdb
5 * Portions Copyright (c) 1996-2009, PostgreSQL Global Development Group
6 * Portions Copyright (c) 1994, Regents of the University of California
8 * $PostgreSQL$
10 *-------------------------------------------------------------------------
12 #include "postgres_fe.h"
14 #include "common.h"
15 #include "dumputils.h"
18 static void help(const char *progname);
21 int
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 {"password", no_argument, NULL, 'W'},
29 {"echo", no_argument, NULL, 'e'},
30 {"quiet", no_argument, NULL, 'q'},
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'},
38 {NULL, 0, NULL, 0}
41 const char *progname;
42 int optindex;
43 int c;
45 const char *dbname = NULL;
46 char *comment = NULL;
47 char *host = NULL;
48 char *port = NULL;
49 char *username = NULL;
50 bool password = false;
51 bool echo = false;
52 char *owner = NULL;
53 char *tablespace = NULL;
54 char *template = NULL;
55 char *encoding = NULL;
56 char *lc_collate = NULL;
57 char *lc_ctype = NULL;
58 char *locale = NULL;
60 PQExpBufferData sql;
62 PGconn *conn;
63 PGresult *result;
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:WeqO:D:T:E:l:", long_options, &optindex)) != -1)
72 switch (c)
74 case 'h':
75 host = optarg;
76 break;
77 case 'p':
78 port = optarg;
79 break;
80 case 'U':
81 username = optarg;
82 break;
83 case 'W':
84 password = true;
85 break;
86 case 'e':
87 echo = true;
88 break;
89 case 'q':
90 /* obsolete; remove in 8.4 */
91 break;
92 case 'O':
93 owner = optarg;
94 break;
95 case 'D':
96 tablespace = optarg;
97 break;
98 case 'T':
99 template = optarg;
100 break;
101 case 'E':
102 encoding = optarg;
103 break;
104 case 1:
105 lc_collate = optarg;
106 break;
107 case 2:
108 lc_ctype = optarg;
109 break;
110 case 'l':
111 locale = optarg;
112 break;
113 default:
114 fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
115 exit(1);
119 switch (argc - optind)
121 case 0:
122 break;
123 case 1:
124 dbname = argv[optind];
125 break;
126 case 2:
127 dbname = argv[optind];
128 comment = argv[optind + 1];
129 break;
130 default:
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);
134 exit(1);
137 if (locale)
139 if (lc_ctype)
141 fprintf(stderr, _("%s: only one of --locale and --lc-ctype can be specified\n"),
142 progname);
143 exit(1);
145 if (lc_collate)
147 fprintf(stderr, _("%s: only one of --locale and --lc-collate can be specified\n"),
148 progname);
149 exit(1);
151 lc_ctype = locale;
152 lc_collate = locale;
155 if (encoding)
157 if (pg_char_to_encoding(encoding) < 0)
159 fprintf(stderr, _("%s: \"%s\" is not a valid encoding name\n"),
160 progname, encoding);
161 exit(1);
165 if (dbname == NULL)
167 if (getenv("PGDATABASE"))
168 dbname = getenv("PGDATABASE");
169 else if (getenv("PGUSER"))
170 dbname = getenv("PGUSER");
171 else
172 dbname = get_user_name(progname);
175 initPQExpBuffer(&sql);
177 appendPQExpBuffer(&sql, "CREATE DATABASE %s",
178 fmtId(dbname));
180 if (owner)
181 appendPQExpBuffer(&sql, " OWNER %s", fmtId(owner));
182 if (tablespace)
183 appendPQExpBuffer(&sql, " TABLESPACE %s", fmtId(tablespace));
184 if (encoding)
185 appendPQExpBuffer(&sql, " ENCODING '%s'", encoding);
186 if (template)
187 appendPQExpBuffer(&sql, " TEMPLATE %s", fmtId(template));
188 if (lc_collate)
189 appendPQExpBuffer(&sql, " COLLATE '%s'", lc_collate);
190 if (lc_ctype)
191 appendPQExpBuffer(&sql, " CTYPE '%s'", lc_ctype);
193 appendPQExpBuffer(&sql, ";\n");
195 conn = connectDatabase(strcmp(dbname, "postgres") == 0 ? "template1" : "postgres",
196 host, port, username, password, progname);
198 if (echo)
199 printf("%s", sql.data);
200 result = PQexec(conn, sql.data);
202 if (PQresultStatus(result) != PGRES_COMMAND_OK)
204 fprintf(stderr, _("%s: database creation failed: %s"),
205 progname, PQerrorMessage(conn));
206 PQfinish(conn);
207 exit(1);
210 PQclear(result);
211 PQfinish(conn);
213 if (comment)
215 conn = connectDatabase(dbname, host, port, username, password, progname);
217 printfPQExpBuffer(&sql, "COMMENT ON DATABASE %s IS ", fmtId(dbname));
218 appendStringLiteralConn(&sql, comment, conn);
219 appendPQExpBuffer(&sql, ";\n");
221 if (echo)
222 printf("%s", sql.data);
223 result = PQexec(conn, sql.data);
225 if (PQresultStatus(result) != PGRES_COMMAND_OK)
227 fprintf(stderr, _("%s: comment creation failed (database was created): %s"),
228 progname, PQerrorMessage(conn));
229 PQfinish(conn);
230 exit(1);
233 PQclear(result);
234 PQfinish(conn);
237 exit(0);
241 static void
242 help(const char *progname)
244 printf(_("%s creates a PostgreSQL database.\n\n"), progname);
245 printf(_("Usage:\n"));
246 printf(_(" %s [OPTION]... [DBNAME] [DESCRIPTION]\n"), progname);
247 printf(_("\nOptions:\n"));
248 printf(_(" -D, --tablespace=TABLESPACE default tablespace for the database\n"));
249 printf(_(" -e, --echo show the commands being sent to the server\n"));
250 printf(_(" -E, --encoding=ENCODING encoding for the database\n"));
251 printf(_(" -l, --locale=LOCALE locale settings for the database\n"));
252 printf(_(" --lc-collate=LOCALE LC_COLLATE setting for the database\n"));
253 printf(_(" --lc-ctype=LOCALE LC_CTYPE setting for the database\n"));
254 printf(_(" -O, --owner=OWNER database user to own the new database\n"));
255 printf(_(" -T, --template=TEMPLATE template database to copy\n"));
256 printf(_(" --help show this help, then exit\n"));
257 printf(_(" --version output version information, then exit\n"));
258 printf(_("\nConnection options:\n"));
259 printf(_(" -h, --host=HOSTNAME database server host or socket directory\n"));
260 printf(_(" -p, --port=PORT database server port\n"));
261 printf(_(" -U, --username=USERNAME user name to connect as\n"));
262 printf(_(" -W, --password force password prompt\n"));
263 printf(_("\nBy default, a database with the same name as the current user is created.\n"));
264 printf(_("\nReport bugs to <pgsql-bugs@postgresql.org>.\n"));