1 /*-------------------------------------------------------------------------
5 * Portions Copyright (c) 1996-2024, 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 "common/logging.h"
16 #include "fe_utils/option_utils.h"
17 #include "fe_utils/string_utils.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 {"no-password", no_argument
, NULL
, 'w'},
31 {"password", no_argument
, NULL
, 'W'},
32 {"echo", no_argument
, NULL
, 'e'},
33 {"owner", required_argument
, NULL
, 'O'},
34 {"tablespace", required_argument
, NULL
, 'D'},
35 {"template", required_argument
, NULL
, 'T'},
36 {"encoding", required_argument
, NULL
, 'E'},
37 {"strategy", required_argument
, NULL
, 'S'},
38 {"lc-collate", required_argument
, NULL
, 1},
39 {"lc-ctype", required_argument
, NULL
, 2},
40 {"locale", required_argument
, NULL
, 'l'},
41 {"maintenance-db", required_argument
, NULL
, 3},
42 {"locale-provider", required_argument
, NULL
, 4},
43 {"builtin-locale", required_argument
, NULL
, 5},
44 {"icu-locale", required_argument
, NULL
, 6},
45 {"icu-rules", required_argument
, NULL
, 7},
53 const char *dbname
= NULL
;
54 const char *maintenance_db
= NULL
;
58 char *username
= NULL
;
59 enum trivalue prompt_password
= TRI_DEFAULT
;
63 char *tablespace
= NULL
;
64 char *template = NULL
;
65 char *encoding
= NULL
;
66 char *strategy
= NULL
;
67 char *lc_collate
= NULL
;
68 char *lc_ctype
= NULL
;
70 char *locale_provider
= NULL
;
71 char *builtin_locale
= NULL
;
72 char *icu_locale
= NULL
;
73 char *icu_rules
= NULL
;
80 pg_logging_init(argv
[0]);
81 progname
= get_progname(argv
[0]);
82 set_pglocale_pgservice(argv
[0], PG_TEXTDOMAIN("pgscripts"));
84 handle_help_version_opts(argc
, argv
, "createdb", help
);
86 while ((c
= getopt_long(argc
, argv
, "D:eE:h:l:O:p:S:T:U:wW", long_options
, &optindex
)) != -1)
91 tablespace
= pg_strdup(optarg
);
97 encoding
= pg_strdup(optarg
);
100 host
= pg_strdup(optarg
);
103 locale
= pg_strdup(optarg
);
106 owner
= pg_strdup(optarg
);
109 port
= pg_strdup(optarg
);
112 strategy
= pg_strdup(optarg
);
115 template = pg_strdup(optarg
);
118 username
= pg_strdup(optarg
);
121 prompt_password
= TRI_NO
;
124 prompt_password
= TRI_YES
;
127 lc_collate
= pg_strdup(optarg
);
130 lc_ctype
= pg_strdup(optarg
);
133 maintenance_db
= pg_strdup(optarg
);
136 locale_provider
= pg_strdup(optarg
);
139 builtin_locale
= pg_strdup(optarg
);
142 icu_locale
= pg_strdup(optarg
);
145 icu_rules
= pg_strdup(optarg
);
148 /* getopt_long already emitted a complaint */
149 pg_log_error_hint("Try \"%s --help\" for more information.", progname
);
154 switch (argc
- optind
)
159 dbname
= argv
[optind
];
162 dbname
= argv
[optind
];
163 comment
= argv
[optind
+ 1];
166 pg_log_error("too many command-line arguments (first is \"%s\")",
168 pg_log_error_hint("Try \"%s --help\" for more information.", progname
);
174 if (pg_char_to_encoding(encoding
) < 0)
175 pg_fatal("\"%s\" is not a valid encoding name", encoding
);
180 if (getenv("PGDATABASE"))
181 dbname
= getenv("PGDATABASE");
182 else if (getenv("PGUSER"))
183 dbname
= getenv("PGUSER");
185 dbname
= get_user_name_or_exit(progname
);
188 /* No point in trying to use postgres db when creating postgres db. */
189 if (maintenance_db
== NULL
&& strcmp(dbname
, "postgres") == 0)
190 maintenance_db
= "template1";
192 cparams
.dbname
= maintenance_db
;
193 cparams
.pghost
= host
;
194 cparams
.pgport
= port
;
195 cparams
.pguser
= username
;
196 cparams
.prompt_password
= prompt_password
;
197 cparams
.override_dbname
= NULL
;
199 conn
= connectMaintenanceDatabase(&cparams
, progname
, echo
);
201 initPQExpBuffer(&sql
);
203 appendPQExpBuffer(&sql
, "CREATE DATABASE %s",
207 appendPQExpBuffer(&sql
, " OWNER %s", fmtId(owner
));
209 appendPQExpBuffer(&sql
, " TABLESPACE %s", fmtId(tablespace
));
212 appendPQExpBufferStr(&sql
, " ENCODING ");
213 appendStringLiteralConn(&sql
, encoding
, conn
);
216 appendPQExpBuffer(&sql
, " STRATEGY %s", fmtId(strategy
));
218 appendPQExpBuffer(&sql
, " TEMPLATE %s", fmtId(template));
221 appendPQExpBufferStr(&sql
, " LOCALE ");
222 appendStringLiteralConn(&sql
, locale
, conn
);
226 appendPQExpBufferStr(&sql
, " BUILTIN_LOCALE ");
227 appendStringLiteralConn(&sql
, builtin_locale
, conn
);
231 appendPQExpBufferStr(&sql
, " LC_COLLATE ");
232 appendStringLiteralConn(&sql
, lc_collate
, conn
);
236 appendPQExpBufferStr(&sql
, " LC_CTYPE ");
237 appendStringLiteralConn(&sql
, lc_ctype
, conn
);
240 appendPQExpBuffer(&sql
, " LOCALE_PROVIDER %s", fmtId(locale_provider
));
243 appendPQExpBufferStr(&sql
, " ICU_LOCALE ");
244 appendStringLiteralConn(&sql
, icu_locale
, conn
);
248 appendPQExpBufferStr(&sql
, " ICU_RULES ");
249 appendStringLiteralConn(&sql
, icu_rules
, conn
);
252 appendPQExpBufferChar(&sql
, ';');
255 printf("%s\n", sql
.data
);
256 result
= PQexec(conn
, sql
.data
);
258 if (PQresultStatus(result
) != PGRES_COMMAND_OK
)
260 pg_log_error("database creation failed: %s", PQerrorMessage(conn
));
269 printfPQExpBuffer(&sql
, "COMMENT ON DATABASE %s IS ", fmtId(dbname
));
270 appendStringLiteralConn(&sql
, comment
, conn
);
271 appendPQExpBufferChar(&sql
, ';');
274 printf("%s\n", sql
.data
);
275 result
= PQexec(conn
, sql
.data
);
277 if (PQresultStatus(result
) != PGRES_COMMAND_OK
)
279 pg_log_error("comment creation failed (database was created): %s",
280 PQerrorMessage(conn
));
295 help(const char *progname
)
297 printf(_("%s creates a PostgreSQL database.\n\n"), progname
);
298 printf(_("Usage:\n"));
299 printf(_(" %s [OPTION]... [DBNAME] [DESCRIPTION]\n"), progname
);
300 printf(_("\nOptions:\n"));
301 printf(_(" -D, --tablespace=TABLESPACE default tablespace for the database\n"));
302 printf(_(" -e, --echo show the commands being sent to the server\n"));
303 printf(_(" -E, --encoding=ENCODING encoding for the database\n"));
304 printf(_(" -l, --locale=LOCALE locale settings for the database\n"));
305 printf(_(" --lc-collate=LOCALE LC_COLLATE setting for the database\n"));
306 printf(_(" --lc-ctype=LOCALE LC_CTYPE setting for the database\n"));
307 printf(_(" --builtin-locale=LOCALE builtin locale setting for the database\n"));
308 printf(_(" --icu-locale=LOCALE ICU locale setting for the database\n"));
309 printf(_(" --icu-rules=RULES ICU rules setting for the database\n"));
310 printf(_(" --locale-provider={builtin|libc|icu}\n"
311 " locale provider for the database's default collation\n"));
312 printf(_(" -O, --owner=OWNER database user to own the new database\n"));
313 printf(_(" -S, --strategy=STRATEGY database creation strategy wal_log or file_copy\n"));
314 printf(_(" -T, --template=TEMPLATE template database to copy\n"));
315 printf(_(" -V, --version output version information, then exit\n"));
316 printf(_(" -?, --help show this help, then exit\n"));
317 printf(_("\nConnection options:\n"));
318 printf(_(" -h, --host=HOSTNAME database server host or socket directory\n"));
319 printf(_(" -p, --port=PORT database server port\n"));
320 printf(_(" -U, --username=USERNAME user name to connect as\n"));
321 printf(_(" -w, --no-password never prompt for password\n"));
322 printf(_(" -W, --password force password prompt\n"));
323 printf(_(" --maintenance-db=DBNAME alternate maintenance database\n"));
324 printf(_("\nBy default, a database with the same name as the current user is created.\n"));
325 printf(_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT
);
326 printf(_("%s home page: <%s>\n"), PACKAGE_NAME
, PACKAGE_URL
);