1 /*-------------------------------------------------------------------------
2 *
3 * createdb
4 *
5 * Portions Copyright (c) 1996-2016, PostgreSQL Global Development Group
6 * Portions Copyright (c) 1994, Regents of the University of California
7 *
8 * src/bin/scripts/createdb.c
9 *
10 *-------------------------------------------------------------------------
11 */
12 #include "postgres_fe.h"
13
14 #include "common.h"
15 #include "fe_utils/string_utils.h"
16
17
18 static void help(const char *progname);
19
20
21 int
main(int argc,char * argv[])22 main(int argc, char *argv[])
23 {
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'},
38 {"maintenance-db", required_argument, NULL, 3},
39 {NULL, 0, NULL, 0}
40 };
41
42 const char *progname;
43 int optindex;
44 int c;
45
46 const char *dbname = NULL;
47 const char *maintenance_db = NULL;
48 char *comment = NULL;
49 char *host = NULL;
50 char *port = NULL;
51 char *username = NULL;
52 enum trivalue prompt_password = TRI_DEFAULT;
53 ConnParams cparams;
54 bool echo = false;
55 char *owner = NULL;
56 char *tablespace = NULL;
57 char *template = NULL;
58 char *encoding = NULL;
59 char *lc_collate = NULL;
60 char *lc_ctype = NULL;
61 char *locale = NULL;
62
63 PQExpBufferData sql;
64
65 PGconn *conn;
66 PGresult *result;
67
68 progname = get_progname(argv[0]);
69 set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pgscripts"));
70
71 handle_help_version_opts(argc, argv, "createdb", help);
72
73 while ((c = getopt_long(argc, argv, "h:p:U:wWeO:D:T:E:l:", long_options, &optindex)) != -1)
74 {
75 switch (c)
76 {
77 case 'h':
78 host = pg_strdup(optarg);
79 break;
80 case 'p':
81 port = pg_strdup(optarg);
82 break;
83 case 'U':
84 username = pg_strdup(optarg);
85 break;
86 case 'w':
87 prompt_password = TRI_NO;
88 break;
89 case 'W':
90 prompt_password = TRI_YES;
91 break;
92 case 'e':
93 echo = true;
94 break;
95 case 'O':
96 owner = pg_strdup(optarg);
97 break;
98 case 'D':
99 tablespace = pg_strdup(optarg);
100 break;
101 case 'T':
102 template = pg_strdup(optarg);
103 break;
104 case 'E':
105 encoding = pg_strdup(optarg);
106 break;
107 case 1:
108 lc_collate = pg_strdup(optarg);
109 break;
110 case 2:
111 lc_ctype = pg_strdup(optarg);
112 break;
113 case 'l':
114 locale = pg_strdup(optarg);
115 break;
116 case 3:
117 maintenance_db = pg_strdup(optarg);
118 break;
119 default:
120 fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
121 exit(1);
122 }
123 }
124
125 switch (argc - optind)
126 {
127 case 0:
128 break;
129 case 1:
130 dbname = argv[optind];
131 break;
132 case 2:
133 dbname = argv[optind];
134 comment = argv[optind + 1];
135 break;
136 default:
137 fprintf(stderr, _("%s: too many command-line arguments (first is \"%s\")\n"),
138 progname, argv[optind + 2]);
139 fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
140 exit(1);
141 }
142
143 if (locale)
144 {
145 if (lc_ctype)
146 {
147 fprintf(stderr, _("%s: only one of --locale and --lc-ctype can be specified\n"),
148 progname);
149 exit(1);
150 }
151 if (lc_collate)
152 {
153 fprintf(stderr, _("%s: only one of --locale and --lc-collate can be specified\n"),
154 progname);
155 exit(1);
156 }
157 lc_ctype = locale;
158 lc_collate = locale;
159 }
160
161 if (encoding)
162 {
163 if (pg_char_to_encoding(encoding) < 0)
164 {
165 fprintf(stderr, _("%s: \"%s\" is not a valid encoding name\n"),
166 progname, encoding);
167 exit(1);
168 }
169 }
170
171 if (dbname == NULL)
172 {
173 if (getenv("PGDATABASE"))
174 dbname = getenv("PGDATABASE");
175 else if (getenv("PGUSER"))
176 dbname = getenv("PGUSER");
177 else
178 dbname = get_user_name_or_exit(progname);
179 }
180
181 /* No point in trying to use postgres db when creating postgres db. */
182 if (maintenance_db == NULL && strcmp(dbname, "postgres") == 0)
183 maintenance_db = "template1";
184
185 cparams.dbname = maintenance_db;
186 cparams.pghost = host;
187 cparams.pgport = port;
188 cparams.pguser = username;
189 cparams.prompt_password = prompt_password;
190 cparams.override_dbname = NULL;
191
192 conn = connectMaintenanceDatabase(&cparams, progname, echo);
193
194 initPQExpBuffer(&sql);
195
196 appendPQExpBuffer(&sql, "CREATE DATABASE %s",
197 fmtId(dbname));
198
199 if (owner)
200 appendPQExpBuffer(&sql, " OWNER %s", fmtId(owner));
201 if (tablespace)
202 appendPQExpBuffer(&sql, " TABLESPACE %s", fmtId(tablespace));
203 if (encoding)
204 {
205 appendPQExpBufferStr(&sql, " ENCODING ");
206 appendStringLiteralConn(&sql, encoding, conn);
207 }
208 if (template)
209 appendPQExpBuffer(&sql, " TEMPLATE %s", fmtId(template));
210 if (lc_collate)
211 {
212 appendPQExpBufferStr(&sql, " LC_COLLATE ");
213 appendStringLiteralConn(&sql, lc_collate, conn);
214 }
215 if (lc_ctype)
216 {
217 appendPQExpBufferStr(&sql, " LC_CTYPE ");
218 appendStringLiteralConn(&sql, lc_ctype, conn);
219 }
220
221 appendPQExpBufferChar(&sql, ';');
222
223 if (echo)
224 printf("%s\n", sql.data);
225 result = PQexec(conn, sql.data);
226
227 if (PQresultStatus(result) != PGRES_COMMAND_OK)
228 {
229 fprintf(stderr, _("%s: database creation failed: %s"),
230 progname, PQerrorMessage(conn));
231 PQfinish(conn);
232 exit(1);
233 }
234
235 PQclear(result);
236
237 if (comment)
238 {
239 printfPQExpBuffer(&sql, "COMMENT ON DATABASE %s IS ", fmtId(dbname));
240 appendStringLiteralConn(&sql, comment, conn);
241 appendPQExpBufferChar(&sql, ';');
242
243 if (echo)
244 printf("%s\n", sql.data);
245 result = PQexec(conn, sql.data);
246
247 if (PQresultStatus(result) != PGRES_COMMAND_OK)
248 {
249 fprintf(stderr, _("%s: comment creation failed (database was created): %s"),
250 progname, PQerrorMessage(conn));
251 PQfinish(conn);
252 exit(1);
253 }
254
255 PQclear(result);
256 }
257
258 PQfinish(conn);
259
260 exit(0);
261 }
262
263
264 static void
help(const char * progname)265 help(const char *progname)
266 {
267 printf(_("%s creates a PostgreSQL database.\n\n"), progname);
268 printf(_("Usage:\n"));
269 printf(_(" %s [OPTION]... [DBNAME] [DESCRIPTION]\n"), progname);
270 printf(_("\nOptions:\n"));
271 printf(_(" -D, --tablespace=TABLESPACE default tablespace for the database\n"));
272 printf(_(" -e, --echo show the commands being sent to the server\n"));
273 printf(_(" -E, --encoding=ENCODING encoding for the database\n"));
274 printf(_(" -l, --locale=LOCALE locale settings for the database\n"));
275 printf(_(" --lc-collate=LOCALE LC_COLLATE setting for the database\n"));
276 printf(_(" --lc-ctype=LOCALE LC_CTYPE setting for the database\n"));
277 printf(_(" -O, --owner=OWNER database user to own the new database\n"));
278 printf(_(" -T, --template=TEMPLATE template database to copy\n"));
279 printf(_(" -V, --version output version information, then exit\n"));
280 printf(_(" -?, --help show this help, then exit\n"));
281 printf(_("\nConnection options:\n"));
282 printf(_(" -h, --host=HOSTNAME database server host or socket directory\n"));
283 printf(_(" -p, --port=PORT database server port\n"));
284 printf(_(" -U, --username=USERNAME user name to connect as\n"));
285 printf(_(" -w, --no-password never prompt for password\n"));
286 printf(_(" -W, --password force password prompt\n"));
287 printf(_(" --maintenance-db=DBNAME alternate maintenance database\n"));
288 printf(_("\nBy default, a database with the same name as the current user is created.\n"));
289 printf(_("\nReport bugs to <pgsql-bugs@postgresql.org>.\n"));
290 }
291