Age Owner Branch data TLA Line data Source code
1 : : /*-------------------------------------------------------------------------
2 : : *
3 : : * createdb
4 : : *
5 : : * Portions Copyright (c) 1996-2025, 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 "common/logging.h"
16 : : #include "fe_utils/option_utils.h"
17 : : #include "fe_utils/string_utils.h"
18 : :
19 : :
20 : : static void help(const char *progname);
21 : :
22 : :
23 : : int
8208 peter_e@gmx.net 24 :CBC 52 : main(int argc, char *argv[])
25 : : {
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},
46 : : {NULL, 0, NULL, 0}
47 : : };
48 : :
49 : : const char *progname;
50 : : int optindex;
51 : : int c;
52 : :
53 : 52 : const char *dbname = NULL;
5023 rhaas@postgresql.org 54 : 52 : const char *maintenance_db = NULL;
8208 peter_e@gmx.net 55 : 52 : char *comment = NULL;
56 : 52 : char *host = NULL;
57 : 52 : char *port = NULL;
58 : 52 : char *username = NULL;
6036 59 : 52 : enum trivalue prompt_password = TRI_DEFAULT;
60 : : ConnParams cparams;
8208 61 : 52 : bool echo = false;
62 : 52 : char *owner = NULL;
7750 tgl@sss.pgh.pa.us 63 : 52 : char *tablespace = NULL;
8208 peter_e@gmx.net 64 : 52 : char *template = NULL;
65 : 52 : char *encoding = NULL;
1257 rhaas@postgresql.org 66 : 52 : char *strategy = NULL;
6192 heikki.linnakangas@i 67 : 52 : char *lc_collate = NULL;
68 : 52 : char *lc_ctype = NULL;
6144 alvherre@alvh.no-ip. 69 : 52 : char *locale = NULL;
1269 peter@eisentraut.org 70 : 52 : char *locale_provider = NULL;
542 jdavis@postgresql.or 71 : 52 : char *builtin_locale = NULL;
1269 peter@eisentraut.org 72 : 52 : char *icu_locale = NULL;
913 73 : 52 : char *icu_rules = NULL;
74 : :
75 : : PQExpBufferData sql;
76 : :
77 : : PGconn *conn;
78 : : PGresult *result;
79 : :
2350 80 : 52 : pg_logging_init(argv[0]);
8208 peter_e@gmx.net 81 : 52 : progname = get_progname(argv[0]);
6113 82 : 52 : set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pgscripts"));
83 : :
8208 84 : 52 : handle_help_version_opts(argc, argv, "createdb", help);
85 : :
999 peter@eisentraut.org 86 [ + + ]: 137 : while ((c = getopt_long(argc, argv, "D:eE:h:l:O:p:S:T:U:wW", long_options, &optindex)) != -1)
87 : : {
8208 peter_e@gmx.net 88 [ - - + - : 88 : switch (c)
+ + - + +
+ - - + +
- + + + +
+ ]
89 : : {
999 peter@eisentraut.org 90 :UBC 0 : case 'D':
91 : 0 : tablespace = pg_strdup(optarg);
92 : 0 : break;
93 : 0 : case 'e':
94 : 0 : echo = true;
95 : 0 : break;
999 peter@eisentraut.org 96 :CBC 8 : case 'E':
97 : 8 : encoding = pg_strdup(optarg);
98 : 8 : break;
8208 peter_e@gmx.net 99 :UBC 0 : case 'h':
4712 bruce@momjian.us 100 : 0 : host = pg_strdup(optarg);
8208 peter_e@gmx.net 101 : 0 : break;
999 peter@eisentraut.org 102 :CBC 10 : case 'l':
103 : 10 : locale = pg_strdup(optarg);
104 : 10 : break;
105 : 1 : case 'O':
106 : 1 : owner = pg_strdup(optarg);
107 : 1 : break;
8208 peter_e@gmx.net 108 :UBC 0 : case 'p':
4712 bruce@momjian.us 109 : 0 : port = pg_strdup(optarg);
8208 peter_e@gmx.net 110 : 0 : break;
999 peter@eisentraut.org 111 :CBC 5 : case 'S':
112 : 5 : strategy = pg_strdup(optarg);
113 : 5 : break;
114 : 24 : case 'T':
115 : 24 : template = pg_strdup(optarg);
116 : 24 : break;
8208 peter_e@gmx.net 117 : 6 : case 'U':
4712 bruce@momjian.us 118 : 6 : username = pg_strdup(optarg);
8208 peter_e@gmx.net 119 : 6 : break;
6036 peter_e@gmx.net 120 :UBC 0 : case 'w':
121 : 0 : prompt_password = TRI_NO;
122 : 0 : break;
8208 123 : 0 : case 'W':
6036 124 : 0 : prompt_password = TRI_YES;
8208 125 : 0 : break;
6192 heikki.linnakangas@i 126 :CBC 5 : case 1:
4712 bruce@momjian.us 127 : 5 : lc_collate = pg_strdup(optarg);
6192 heikki.linnakangas@i 128 : 5 : break;
129 : 5 : case 2:
4712 bruce@momjian.us 130 : 5 : lc_ctype = pg_strdup(optarg);
6192 heikki.linnakangas@i 131 : 5 : break;
5023 rhaas@postgresql.org 132 :UBC 0 : case 3:
4712 bruce@momjian.us 133 : 0 : maintenance_db = pg_strdup(optarg);
5023 rhaas@postgresql.org 134 : 0 : break;
1269 peter@eisentraut.org 135 :CBC 16 : case 4:
136 : 16 : locale_provider = pg_strdup(optarg);
137 : 16 : break;
138 : 2 : case 5:
542 jdavis@postgresql.or 139 : 2 : builtin_locale = pg_strdup(optarg);
1269 peter@eisentraut.org 140 : 2 : break;
913 141 : 4 : case 6:
542 jdavis@postgresql.or 142 : 4 : icu_locale = pg_strdup(optarg);
143 : 4 : break;
144 : 1 : case 7:
913 peter@eisentraut.org 145 : 1 : icu_rules = pg_strdup(optarg);
146 : 1 : break;
8208 peter_e@gmx.net 147 : 1 : default:
148 : : /* getopt_long already emitted a complaint */
1247 tgl@sss.pgh.pa.us 149 : 1 : pg_log_error_hint("Try \"%s --help\" for more information.", progname);
8208 peter_e@gmx.net 150 : 1 : exit(1);
151 : : }
152 : : }
153 : :
154 [ - + - - ]: 49 : switch (argc - optind)
155 : : {
8208 peter_e@gmx.net 156 :UBC 0 : case 0:
157 : 0 : break;
8208 peter_e@gmx.net 158 :CBC 49 : case 1:
159 : 49 : dbname = argv[optind];
160 : 49 : break;
8208 peter_e@gmx.net 161 :UBC 0 : case 2:
162 : 0 : dbname = argv[optind];
163 : 0 : comment = argv[optind + 1];
164 : 0 : break;
165 : 0 : default:
2350 peter@eisentraut.org 166 : 0 : pg_log_error("too many command-line arguments (first is \"%s\")",
167 : : argv[optind + 2]);
1247 tgl@sss.pgh.pa.us 168 : 0 : pg_log_error_hint("Try \"%s --help\" for more information.", progname);
8208 peter_e@gmx.net 169 : 0 : exit(1);
170 : : }
171 : :
8208 peter_e@gmx.net 172 [ + + ]:CBC 49 : if (encoding)
173 : : {
174 [ + + ]: 8 : if (pg_char_to_encoding(encoding) < 0)
1247 tgl@sss.pgh.pa.us 175 : 1 : pg_fatal("\"%s\" is not a valid encoding name", encoding);
176 : : }
177 : :
8208 peter_e@gmx.net 178 [ - + ]: 48 : if (dbname == NULL)
179 : : {
8208 peter_e@gmx.net 180 [ # # ]:UBC 0 : if (getenv("PGDATABASE"))
181 : 0 : dbname = getenv("PGDATABASE");
182 [ # # ]: 0 : else if (getenv("PGUSER"))
183 : 0 : dbname = getenv("PGUSER");
184 : : else
4280 bruce@momjian.us 185 : 0 : dbname = get_user_name_or_exit(progname);
186 : : }
187 : :
188 : : /* No point in trying to use postgres db when creating postgres db. */
2018 michael@paquier.xyz 189 [ + - - + ]:CBC 48 : if (maintenance_db == NULL && strcmp(dbname, "postgres") == 0)
2018 michael@paquier.xyz 190 :UBC 0 : maintenance_db = "template1";
191 : :
1783 tgl@sss.pgh.pa.us 192 :CBC 48 : cparams.dbname = maintenance_db;
193 : 48 : cparams.pghost = host;
194 : 48 : cparams.pgport = port;
195 : 48 : cparams.pguser = username;
196 : 48 : cparams.prompt_password = prompt_password;
197 : 48 : cparams.override_dbname = NULL;
198 : :
199 : 48 : conn = connectMaintenanceDatabase(&cparams, progname, echo);
200 : :
208 andres@anarazel.de 201 : 48 : setFmtEncoding(PQclientEncoding(conn));
202 : :
8208 peter_e@gmx.net 203 : 48 : initPQExpBuffer(&sql);
204 : :
8151 tgl@sss.pgh.pa.us 205 : 48 : appendPQExpBuffer(&sql, "CREATE DATABASE %s",
206 : : fmtId(dbname));
207 : :
8208 peter_e@gmx.net 208 [ + + ]: 48 : if (owner)
209 : 1 : appendPQExpBuffer(&sql, " OWNER %s", fmtId(owner));
7750 tgl@sss.pgh.pa.us 210 [ - + ]: 48 : if (tablespace)
7750 tgl@sss.pgh.pa.us 211 :UBC 0 : appendPQExpBuffer(&sql, " TABLESPACE %s", fmtId(tablespace));
8208 peter_e@gmx.net 212 [ + + ]:CBC 48 : if (encoding)
213 : : {
2018 michael@paquier.xyz 214 : 7 : appendPQExpBufferStr(&sql, " ENCODING ");
215 : 7 : appendStringLiteralConn(&sql, encoding, conn);
216 : : }
1257 rhaas@postgresql.org 217 [ + + ]: 48 : if (strategy)
218 : 5 : appendPQExpBuffer(&sql, " STRATEGY %s", fmtId(strategy));
8208 peter_e@gmx.net 219 [ + + ]: 48 : if (template)
220 : 24 : appendPQExpBuffer(&sql, " TEMPLATE %s", fmtId(template));
813 jdavis@postgresql.or 221 [ + + ]: 48 : if (locale)
222 : : {
223 : 10 : appendPQExpBufferStr(&sql, " LOCALE ");
224 : 10 : appendStringLiteralConn(&sql, locale, conn);
225 : : }
542 226 [ + + ]: 48 : if (builtin_locale)
227 : : {
228 : 2 : appendPQExpBufferStr(&sql, " BUILTIN_LOCALE ");
229 : 2 : appendStringLiteralConn(&sql, builtin_locale, conn);
230 : : }
6192 heikki.linnakangas@i 231 [ + + ]: 48 : if (lc_collate)
232 : : {
2018 michael@paquier.xyz 233 : 5 : appendPQExpBufferStr(&sql, " LC_COLLATE ");
234 : 5 : appendStringLiteralConn(&sql, lc_collate, conn);
235 : : }
6192 heikki.linnakangas@i 236 [ + + ]: 48 : if (lc_ctype)
237 : : {
2018 michael@paquier.xyz 238 : 5 : appendPQExpBufferStr(&sql, " LC_CTYPE ");
239 : 5 : appendStringLiteralConn(&sql, lc_ctype, conn);
240 : : }
1269 peter@eisentraut.org 241 [ + + ]: 48 : if (locale_provider)
503 tomas.vondra@postgre 242 : 16 : appendPQExpBuffer(&sql, " LOCALE_PROVIDER %s", fmtId(locale_provider));
1269 peter@eisentraut.org 243 [ + + ]: 48 : if (icu_locale)
244 : : {
245 : 4 : appendPQExpBufferStr(&sql, " ICU_LOCALE ");
246 : 4 : appendStringLiteralConn(&sql, icu_locale, conn);
247 : : }
913 248 [ + + ]: 48 : if (icu_rules)
249 : : {
250 : 1 : appendPQExpBufferStr(&sql, " ICU_RULES ");
251 : 1 : appendStringLiteralConn(&sql, icu_rules, conn);
252 : : }
253 : :
3719 heikki.linnakangas@i 254 : 48 : appendPQExpBufferChar(&sql, ';');
255 : :
8208 peter_e@gmx.net 256 [ - + ]: 48 : if (echo)
4226 peter_e@gmx.net 257 :UBC 0 : printf("%s\n", sql.data);
8208 peter_e@gmx.net 258 :CBC 48 : result = PQexec(conn, sql.data);
259 : :
260 [ + + ]: 48 : if (PQresultStatus(result) != PGRES_COMMAND_OK)
261 : : {
2350 peter@eisentraut.org 262 : 13 : pg_log_error("database creation failed: %s", PQerrorMessage(conn));
8208 peter_e@gmx.net 263 : 13 : PQfinish(conn);
264 : 13 : exit(1);
265 : : }
266 : :
267 : 35 : PQclear(result);
268 : :
269 [ - + ]: 35 : if (comment)
270 : : {
8151 tgl@sss.pgh.pa.us 271 :UBC 0 : printfPQExpBuffer(&sql, "COMMENT ON DATABASE %s IS ", fmtId(dbname));
7041 272 : 0 : appendStringLiteralConn(&sql, comment, conn);
3719 heikki.linnakangas@i 273 : 0 : appendPQExpBufferChar(&sql, ';');
274 : :
8208 peter_e@gmx.net 275 [ # # ]: 0 : if (echo)
4226 276 : 0 : printf("%s\n", sql.data);
8208 277 : 0 : result = PQexec(conn, sql.data);
278 : :
279 [ # # ]: 0 : if (PQresultStatus(result) != PGRES_COMMAND_OK)
280 : : {
2350 peter@eisentraut.org 281 : 0 : pg_log_error("comment creation failed (database was created): %s",
282 : : PQerrorMessage(conn));
8208 peter_e@gmx.net 283 : 0 : PQfinish(conn);
284 : 0 : exit(1);
285 : : }
286 : :
7040 bruce@momjian.us 287 : 0 : PQclear(result);
288 : : }
289 : :
5233 bruce@momjian.us 290 :CBC 35 : PQfinish(conn);
291 : :
8208 peter_e@gmx.net 292 : 35 : exit(0);
293 : : }
294 : :
295 : :
296 : : static void
297 : 1 : help(const char *progname)
298 : : {
299 : 1 : printf(_("%s creates a PostgreSQL database.\n\n"), progname);
300 : 1 : printf(_("Usage:\n"));
301 : 1 : printf(_(" %s [OPTION]... [DBNAME] [DESCRIPTION]\n"), progname);
302 : 1 : printf(_("\nOptions:\n"));
7750 tgl@sss.pgh.pa.us 303 : 1 : printf(_(" -D, --tablespace=TABLESPACE default tablespace for the database\n"));
6037 peter_e@gmx.net 304 : 1 : printf(_(" -e, --echo show the commands being sent to the server\n"));
7750 tgl@sss.pgh.pa.us 305 : 1 : printf(_(" -E, --encoding=ENCODING encoding for the database\n"));
6144 alvherre@alvh.no-ip. 306 : 1 : printf(_(" -l, --locale=LOCALE locale settings for the database\n"));
6037 peter_e@gmx.net 307 : 1 : printf(_(" --lc-collate=LOCALE LC_COLLATE setting for the database\n"));
308 : 1 : printf(_(" --lc-ctype=LOCALE LC_CTYPE setting for the database\n"));
542 jdavis@postgresql.or 309 : 1 : printf(_(" --builtin-locale=LOCALE builtin locale setting for the database\n"));
1269 peter@eisentraut.org 310 : 1 : printf(_(" --icu-locale=LOCALE ICU locale setting for the database\n"));
913 311 : 1 : printf(_(" --icu-rules=RULES ICU rules setting for the database\n"));
542 jdavis@postgresql.or 312 : 1 : printf(_(" --locale-provider={builtin|libc|icu}\n"
313 : : " locale provider for the database's default collation\n"));
7750 tgl@sss.pgh.pa.us 314 : 1 : printf(_(" -O, --owner=OWNER database user to own the new database\n"));
1257 rhaas@postgresql.org 315 : 1 : printf(_(" -S, --strategy=STRATEGY database creation strategy wal_log or file_copy\n"));
7750 tgl@sss.pgh.pa.us 316 : 1 : printf(_(" -T, --template=TEMPLATE template database to copy\n"));
4828 peter_e@gmx.net 317 : 1 : printf(_(" -V, --version output version information, then exit\n"));
318 : 1 : printf(_(" -?, --help show this help, then exit\n"));
8208 319 : 1 : printf(_("\nConnection options:\n"));
7750 tgl@sss.pgh.pa.us 320 : 1 : printf(_(" -h, --host=HOSTNAME database server host or socket directory\n"));
321 : 1 : printf(_(" -p, --port=PORT database server port\n"));
322 : 1 : printf(_(" -U, --username=USERNAME user name to connect as\n"));
6036 peter_e@gmx.net 323 : 1 : printf(_(" -w, --no-password never prompt for password\n"));
6479 tgl@sss.pgh.pa.us 324 : 1 : printf(_(" -W, --password force password prompt\n"));
5023 rhaas@postgresql.org 325 : 1 : printf(_(" --maintenance-db=DBNAME alternate maintenance database\n"));
8208 peter_e@gmx.net 326 : 1 : printf(_("\nBy default, a database with the same name as the current user is created.\n"));
2017 peter@eisentraut.org 327 : 1 : printf(_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
328 : 1 : printf(_("%s home page: <%s>\n"), PACKAGE_NAME, PACKAGE_URL);
8208 peter_e@gmx.net 329 : 1 : }
|