1 /*-------------------------------------------------------------------------
5 * Portions Copyright (c) 1996-2021, 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 {"lc-collate", required_argument
, NULL
, 1},
38 {"lc-ctype", required_argument
, NULL
, 2},
39 {"locale", required_argument
, NULL
, 'l'},
40 {"maintenance-db", required_argument
, NULL
, 3},
48 const char *dbname
= NULL
;
49 const char *maintenance_db
= NULL
;
53 char *username
= NULL
;
54 enum trivalue prompt_password
= TRI_DEFAULT
;
58 char *tablespace
= NULL
;
59 char *template = NULL
;
60 char *encoding
= NULL
;
61 char *lc_collate
= NULL
;
62 char *lc_ctype
= NULL
;
70 pg_logging_init(argv
[0]);
71 progname
= get_progname(argv
[0]);
72 set_pglocale_pgservice(argv
[0], PG_TEXTDOMAIN("pgscripts"));
74 handle_help_version_opts(argc
, argv
, "createdb", help
);
76 while ((c
= getopt_long(argc
, argv
, "h:p:U:wWeO:D:T:E:l:", long_options
, &optindex
)) != -1)
81 host
= pg_strdup(optarg
);
84 port
= pg_strdup(optarg
);
87 username
= pg_strdup(optarg
);
90 prompt_password
= TRI_NO
;
93 prompt_password
= TRI_YES
;
99 owner
= pg_strdup(optarg
);
102 tablespace
= pg_strdup(optarg
);
105 template = pg_strdup(optarg
);
108 encoding
= pg_strdup(optarg
);
111 lc_collate
= pg_strdup(optarg
);
114 lc_ctype
= pg_strdup(optarg
);
117 locale
= pg_strdup(optarg
);
120 maintenance_db
= pg_strdup(optarg
);
123 fprintf(stderr
, _("Try \"%s --help\" for more information.\n"), progname
);
128 switch (argc
- optind
)
133 dbname
= argv
[optind
];
136 dbname
= argv
[optind
];
137 comment
= argv
[optind
+ 1];
140 pg_log_error("too many command-line arguments (first is \"%s\")",
142 fprintf(stderr
, _("Try \"%s --help\" for more information.\n"), progname
);
150 pg_log_error("only one of --locale and --lc-ctype can be specified");
155 pg_log_error("only one of --locale and --lc-collate can be specified");
164 if (pg_char_to_encoding(encoding
) < 0)
166 pg_log_error("\"%s\" is not a valid encoding name", encoding
);
173 if (getenv("PGDATABASE"))
174 dbname
= getenv("PGDATABASE");
175 else if (getenv("PGUSER"))
176 dbname
= getenv("PGUSER");
178 dbname
= get_user_name_or_exit(progname
);
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";
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
;
192 conn
= connectMaintenanceDatabase(&cparams
, progname
, echo
);
194 initPQExpBuffer(&sql
);
196 appendPQExpBuffer(&sql
, "CREATE DATABASE %s",
200 appendPQExpBuffer(&sql
, " OWNER %s", fmtId(owner
));
202 appendPQExpBuffer(&sql
, " TABLESPACE %s", fmtId(tablespace
));
205 appendPQExpBufferStr(&sql
, " ENCODING ");
206 appendStringLiteralConn(&sql
, encoding
, conn
);
209 appendPQExpBuffer(&sql
, " TEMPLATE %s", fmtId(template));
212 appendPQExpBufferStr(&sql
, " LC_COLLATE ");
213 appendStringLiteralConn(&sql
, lc_collate
, conn
);
217 appendPQExpBufferStr(&sql
, " LC_CTYPE ");
218 appendStringLiteralConn(&sql
, lc_ctype
, conn
);
221 appendPQExpBufferChar(&sql
, ';');
224 printf("%s\n", sql
.data
);
225 result
= PQexec(conn
, sql
.data
);
227 if (PQresultStatus(result
) != PGRES_COMMAND_OK
)
229 pg_log_error("database creation failed: %s", PQerrorMessage(conn
));
238 printfPQExpBuffer(&sql
, "COMMENT ON DATABASE %s IS ", fmtId(dbname
));
239 appendStringLiteralConn(&sql
, comment
, conn
);
240 appendPQExpBufferChar(&sql
, ';');
243 printf("%s\n", sql
.data
);
244 result
= PQexec(conn
, sql
.data
);
246 if (PQresultStatus(result
) != PGRES_COMMAND_OK
)
248 pg_log_error("comment creation failed (database was created): %s",
249 PQerrorMessage(conn
));
264 help(const char *progname
)
266 printf(_("%s creates a PostgreSQL database.\n\n"), progname
);
267 printf(_("Usage:\n"));
268 printf(_(" %s [OPTION]... [DBNAME] [DESCRIPTION]\n"), progname
);
269 printf(_("\nOptions:\n"));
270 printf(_(" -D, --tablespace=TABLESPACE default tablespace for the database\n"));
271 printf(_(" -e, --echo show the commands being sent to the server\n"));
272 printf(_(" -E, --encoding=ENCODING encoding for the database\n"));
273 printf(_(" -l, --locale=LOCALE locale settings for the database\n"));
274 printf(_(" --lc-collate=LOCALE LC_COLLATE setting for the database\n"));
275 printf(_(" --lc-ctype=LOCALE LC_CTYPE setting for the database\n"));
276 printf(_(" -O, --owner=OWNER database user to own the new database\n"));
277 printf(_(" -T, --template=TEMPLATE template database to copy\n"));
278 printf(_(" -V, --version output version information, then exit\n"));
279 printf(_(" -?, --help show this help, then exit\n"));
280 printf(_("\nConnection options:\n"));
281 printf(_(" -h, --host=HOSTNAME database server host or socket directory\n"));
282 printf(_(" -p, --port=PORT database server port\n"));
283 printf(_(" -U, --username=USERNAME user name to connect as\n"));
284 printf(_(" -w, --no-password never prompt for password\n"));
285 printf(_(" -W, --password force password prompt\n"));
286 printf(_(" --maintenance-db=DBNAME alternate maintenance database\n"));
287 printf(_("\nBy default, a database with the same name as the current user is created.\n"));
288 printf(_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT
);
289 printf(_("%s home page: <%s>\n"), PACKAGE_NAME
, PACKAGE_URL
);