PostgreSQL Source Code git master
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
createdb.c
Go to the documentation of this file.
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"
18
19
20static void help(const char *progname);
21
22
23int
24main(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 const char *dbname = NULL;
54 const char *maintenance_db = NULL;
55 char *comment = NULL;
56 char *host = NULL;
57 char *port = NULL;
58 char *username = NULL;
59 enum trivalue prompt_password = TRI_DEFAULT;
60 ConnParams cparams;
61 bool echo = false;
62 char *owner = NULL;
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;
69 char *locale = NULL;
70 char *locale_provider = NULL;
71 char *builtin_locale = NULL;
72 char *icu_locale = NULL;
73 char *icu_rules = NULL;
74
76
77 PGconn *conn;
78 PGresult *result;
79
80 pg_logging_init(argv[0]);
81 progname = get_progname(argv[0]);
82 set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pgscripts"));
83
84 handle_help_version_opts(argc, argv, "createdb", help);
85
86 while ((c = getopt_long(argc, argv, "D:eE:h:l:O:p:S:T:U:wW", long_options, &optindex)) != -1)
87 {
88 switch (c)
89 {
90 case 'D':
92 break;
93 case 'e':
94 echo = true;
95 break;
96 case 'E':
98 break;
99 case 'h':
100 host = pg_strdup(optarg);
101 break;
102 case 'l':
104 break;
105 case 'O':
106 owner = pg_strdup(optarg);
107 break;
108 case 'p':
110 break;
111 case 'S':
112 strategy = pg_strdup(optarg);
113 break;
114 case 'T':
115 template = pg_strdup(optarg);
116 break;
117 case 'U':
119 break;
120 case 'w':
121 prompt_password = TRI_NO;
122 break;
123 case 'W':
124 prompt_password = TRI_YES;
125 break;
126 case 1:
128 break;
129 case 2:
131 break;
132 case 3:
133 maintenance_db = pg_strdup(optarg);
134 break;
135 case 4:
137 break;
138 case 5:
139 builtin_locale = pg_strdup(optarg);
140 break;
141 case 6:
142 icu_locale = pg_strdup(optarg);
143 break;
144 case 7:
146 break;
147 default:
148 /* getopt_long already emitted a complaint */
149 pg_log_error_hint("Try \"%s --help\" for more information.", progname);
150 exit(1);
151 }
152 }
153
154 switch (argc - optind)
155 {
156 case 0:
157 break;
158 case 1:
159 dbname = argv[optind];
160 break;
161 case 2:
162 dbname = argv[optind];
163 comment = argv[optind + 1];
164 break;
165 default:
166 pg_log_error("too many command-line arguments (first is \"%s\")",
167 argv[optind + 2]);
168 pg_log_error_hint("Try \"%s --help\" for more information.", progname);
169 exit(1);
170 }
171
172 if (encoding)
173 {
175 pg_fatal("\"%s\" is not a valid encoding name", encoding);
176 }
177
178 if (dbname == NULL)
179 {
180 if (getenv("PGDATABASE"))
181 dbname = getenv("PGDATABASE");
182 else if (getenv("PGUSER"))
183 dbname = getenv("PGUSER");
184 else
186 }
187
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";
191
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;
198
199 conn = connectMaintenanceDatabase(&cparams, progname, echo);
200
201 initPQExpBuffer(&sql);
202
203 appendPQExpBuffer(&sql, "CREATE DATABASE %s",
204 fmtId(dbname));
205
206 if (owner)
207 appendPQExpBuffer(&sql, " OWNER %s", fmtId(owner));
208 if (tablespace)
209 appendPQExpBuffer(&sql, " TABLESPACE %s", fmtId(tablespace));
210 if (encoding)
211 {
212 appendPQExpBufferStr(&sql, " ENCODING ");
214 }
215 if (strategy)
216 appendPQExpBuffer(&sql, " STRATEGY %s", fmtId(strategy));
217 if (template)
218 appendPQExpBuffer(&sql, " TEMPLATE %s", fmtId(template));
219 if (locale)
220 {
221 appendPQExpBufferStr(&sql, " LOCALE ");
223 }
224 if (builtin_locale)
225 {
226 appendPQExpBufferStr(&sql, " BUILTIN_LOCALE ");
227 appendStringLiteralConn(&sql, builtin_locale, conn);
228 }
229 if (lc_collate)
230 {
231 appendPQExpBufferStr(&sql, " LC_COLLATE ");
233 }
234 if (lc_ctype)
235 {
236 appendPQExpBufferStr(&sql, " LC_CTYPE ");
238 }
239 if (locale_provider)
240 appendPQExpBuffer(&sql, " LOCALE_PROVIDER %s", fmtId(locale_provider));
241 if (icu_locale)
242 {
243 appendPQExpBufferStr(&sql, " ICU_LOCALE ");
244 appendStringLiteralConn(&sql, icu_locale, conn);
245 }
246 if (icu_rules)
247 {
248 appendPQExpBufferStr(&sql, " ICU_RULES ");
250 }
251
252 appendPQExpBufferChar(&sql, ';');
253
254 if (echo)
255 printf("%s\n", sql.data);
256 result = PQexec(conn, sql.data);
257
258 if (PQresultStatus(result) != PGRES_COMMAND_OK)
259 {
260 pg_log_error("database creation failed: %s", PQerrorMessage(conn));
261 PQfinish(conn);
262 exit(1);
263 }
264
265 PQclear(result);
266
267 if (comment)
268 {
269 printfPQExpBuffer(&sql, "COMMENT ON DATABASE %s IS ", fmtId(dbname));
271 appendPQExpBufferChar(&sql, ';');
272
273 if (echo)
274 printf("%s\n", sql.data);
275 result = PQexec(conn, sql.data);
276
277 if (PQresultStatus(result) != PGRES_COMMAND_OK)
278 {
279 pg_log_error("comment creation failed (database was created): %s",
281 PQfinish(conn);
282 exit(1);
283 }
284
285 PQclear(result);
286 }
287
288 PQfinish(conn);
289
290 exit(0);
291}
292
293
294static void
295help(const char *progname)
296{
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);
327}
#define PG_TEXTDOMAIN(domain)
Definition: c.h:1171
void set_pglocale_pgservice(const char *argv0, const char *app)
Definition: exec.c:429
PGconn * connectMaintenanceDatabase(ConnParams *cparams, const char *progname, bool echo)
int main(int argc, char *argv[])
Definition: createdb.c:24
static void help(const char *progname)
Definition: createdb.c:295
#define _(x)
Definition: elog.c:90
void PQfinish(PGconn *conn)
Definition: fe-connect.c:4939
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:7268
ExecStatusType PQresultStatus(const PGresult *res)
Definition: fe-exec.c:3411
PGresult * PQexec(PGconn *conn, const char *query)
Definition: fe-exec.c:2262
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
int getopt_long(int argc, char *const argv[], const char *optstring, const struct option *longopts, int *longindex)
Definition: getopt_long.c:60
#define no_argument
Definition: getopt_long.h:25
#define required_argument
Definition: getopt_long.h:26
#define comment
Definition: indent_codes.h:49
static char * lc_collate
Definition: initdb.c:141
static char * lc_ctype
Definition: initdb.c:142
static char locale_provider
Definition: initdb.c:147
static char * icu_rules
Definition: initdb.c:151
static char * username
Definition: initdb.c:153
static char * locale
Definition: initdb.c:140
@ PGRES_COMMAND_OK
Definition: libpq-fe.h:120
exit(1)
void pg_logging_init(const char *argv0)
Definition: logging.c:83
#define pg_log_error(...)
Definition: logging.h:106
#define pg_log_error_hint(...)
Definition: logging.h:112
const char * progname
Definition: main.c:44
void handle_help_version_opts(int argc, char *argv[], const char *fixed_progname, help_handler hlp)
Definition: option_utils.c:24
#define pg_fatal(...)
int32 encoding
Definition: pg_database.h:41
PGDLLIMPORT int optind
Definition: getopt.c:51
PGDLLIMPORT char * optarg
Definition: getopt.c:53
static int port
Definition: pg_regress.c:115
#define pg_char_to_encoding
Definition: pg_wchar.h:629
static char * tablespace
Definition: pgbench.c:216
const char * get_progname(const char *argv0)
Definition: path.c:575
#define printf(...)
Definition: port.h:244
void printfPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:235
void initPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:90
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:265
void appendPQExpBufferChar(PQExpBuffer str, char ch)
Definition: pqexpbuffer.c:378
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:367
char * c
char * dbname
Definition: streamutil.c:50
PGconn * conn
Definition: streamutil.c:53
const char * fmtId(const char *rawid)
Definition: string_utils.c:64
void appendStringLiteralConn(PQExpBuffer buf, const char *str, PGconn *conn)
Definition: string_utils.c:293
const char * pguser
Definition: connect_utils.h:31
char * override_dbname
Definition: pg_backup.h:92
char * pgport
Definition: pg_backup.h:86
char * pghost
Definition: pg_backup.h:87
char * dbname
Definition: pg_backup.h:85
enum trivalue prompt_password
Definition: connect_utils.h:32
const char * get_user_name_or_exit(const char *progname)
Definition: username.c:74
trivalue
Definition: vacuumlo.c:35
@ TRI_YES
Definition: vacuumlo.c:38
@ TRI_DEFAULT
Definition: vacuumlo.c:36
@ TRI_NO
Definition: vacuumlo.c:37