PostgreSQL Source Code  git master
createdb.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * createdb
4  *
5  * Portions Copyright (c) 1996-2023, 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
24 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  {"icu-locale", required_argument, NULL, 5},
44  {"icu-rules", required_argument, NULL, 6},
45  {NULL, 0, NULL, 0}
46  };
47 
48  const char *progname;
49  int optindex;
50  int c;
51 
52  const char *dbname = NULL;
53  const char *maintenance_db = NULL;
54  char *comment = NULL;
55  char *host = NULL;
56  char *port = NULL;
57  char *username = NULL;
58  enum trivalue prompt_password = TRI_DEFAULT;
59  ConnParams cparams;
60  bool echo = false;
61  char *owner = NULL;
62  char *tablespace = NULL;
63  char *template = NULL;
64  char *encoding = NULL;
65  char *strategy = NULL;
66  char *lc_collate = NULL;
67  char *lc_ctype = NULL;
68  char *locale = NULL;
69  char *locale_provider = NULL;
70  char *icu_locale = NULL;
71  char *icu_rules = NULL;
72 
73  PQExpBufferData sql;
74 
75  PGconn *conn;
76  PGresult *result;
77 
78  pg_logging_init(argv[0]);
79  progname = get_progname(argv[0]);
80  set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pgscripts"));
81 
82  handle_help_version_opts(argc, argv, "createdb", help);
83 
84  while ((c = getopt_long(argc, argv, "D:eE:h:l:O:p:S:T:U:wW", long_options, &optindex)) != -1)
85  {
86  switch (c)
87  {
88  case 'D':
90  break;
91  case 'e':
92  echo = true;
93  break;
94  case 'E':
96  break;
97  case 'h':
98  host = pg_strdup(optarg);
99  break;
100  case 'l':
102  break;
103  case 'O':
104  owner = pg_strdup(optarg);
105  break;
106  case 'p':
107  port = pg_strdup(optarg);
108  break;
109  case 'S':
110  strategy = pg_strdup(optarg);
111  break;
112  case 'T':
113  template = pg_strdup(optarg);
114  break;
115  case 'U':
117  break;
118  case 'w':
119  prompt_password = TRI_NO;
120  break;
121  case 'W':
122  prompt_password = TRI_YES;
123  break;
124  case 1:
126  break;
127  case 2:
129  break;
130  case 3:
131  maintenance_db = pg_strdup(optarg);
132  break;
133  case 4:
135  break;
136  case 5:
138  break;
139  case 6:
141  break;
142  default:
143  /* getopt_long already emitted a complaint */
144  pg_log_error_hint("Try \"%s --help\" for more information.", progname);
145  exit(1);
146  }
147  }
148 
149  switch (argc - optind)
150  {
151  case 0:
152  break;
153  case 1:
154  dbname = argv[optind];
155  break;
156  case 2:
157  dbname = argv[optind];
158  comment = argv[optind + 1];
159  break;
160  default:
161  pg_log_error("too many command-line arguments (first is \"%s\")",
162  argv[optind + 2]);
163  pg_log_error_hint("Try \"%s --help\" for more information.", progname);
164  exit(1);
165  }
166 
167  if (encoding)
168  {
169  if (pg_char_to_encoding(encoding) < 0)
170  pg_fatal("\"%s\" is not a valid encoding name", encoding);
171  }
172 
173  if (dbname == NULL)
174  {
175  if (getenv("PGDATABASE"))
176  dbname = getenv("PGDATABASE");
177  else if (getenv("PGUSER"))
178  dbname = getenv("PGUSER");
179  else
181  }
182 
183  /* No point in trying to use postgres db when creating postgres db. */
184  if (maintenance_db == NULL && strcmp(dbname, "postgres") == 0)
185  maintenance_db = "template1";
186 
187  cparams.dbname = maintenance_db;
188  cparams.pghost = host;
189  cparams.pgport = port;
190  cparams.pguser = username;
191  cparams.prompt_password = prompt_password;
192  cparams.override_dbname = NULL;
193 
194  conn = connectMaintenanceDatabase(&cparams, progname, echo);
195 
196  initPQExpBuffer(&sql);
197 
198  appendPQExpBuffer(&sql, "CREATE DATABASE %s",
199  fmtId(dbname));
200 
201  if (owner)
202  appendPQExpBuffer(&sql, " OWNER %s", fmtId(owner));
203  if (tablespace)
204  appendPQExpBuffer(&sql, " TABLESPACE %s", fmtId(tablespace));
205  if (encoding)
206  {
207  appendPQExpBufferStr(&sql, " ENCODING ");
209  }
210  if (strategy)
211  appendPQExpBuffer(&sql, " STRATEGY %s", fmtId(strategy));
212  if (template)
213  appendPQExpBuffer(&sql, " TEMPLATE %s", fmtId(template));
214  if (locale)
215  {
216  appendPQExpBufferStr(&sql, " LOCALE ");
218  }
219  if (lc_collate)
220  {
221  appendPQExpBufferStr(&sql, " LC_COLLATE ");
223  }
224  if (lc_ctype)
225  {
226  appendPQExpBufferStr(&sql, " LC_CTYPE ");
228  }
229  if (locale_provider)
230  appendPQExpBuffer(&sql, " LOCALE_PROVIDER %s", locale_provider);
231  if (icu_locale)
232  {
233  appendPQExpBufferStr(&sql, " ICU_LOCALE ");
235  }
236  if (icu_rules)
237  {
238  appendPQExpBufferStr(&sql, " ICU_RULES ");
240  }
241 
242  appendPQExpBufferChar(&sql, ';');
243 
244  if (echo)
245  printf("%s\n", sql.data);
246  result = PQexec(conn, sql.data);
247 
248  if (PQresultStatus(result) != PGRES_COMMAND_OK)
249  {
250  pg_log_error("database creation failed: %s", PQerrorMessage(conn));
251  PQfinish(conn);
252  exit(1);
253  }
254 
255  PQclear(result);
256 
257  if (comment)
258  {
259  printfPQExpBuffer(&sql, "COMMENT ON DATABASE %s IS ", fmtId(dbname));
261  appendPQExpBufferChar(&sql, ';');
262 
263  if (echo)
264  printf("%s\n", sql.data);
265  result = PQexec(conn, sql.data);
266 
267  if (PQresultStatus(result) != PGRES_COMMAND_OK)
268  {
269  pg_log_error("comment creation failed (database was created): %s",
271  PQfinish(conn);
272  exit(1);
273  }
274 
275  PQclear(result);
276  }
277 
278  PQfinish(conn);
279 
280  exit(0);
281 }
282 
283 
284 static void
285 help(const char *progname)
286 {
287  printf(_("%s creates a PostgreSQL database.\n\n"), progname);
288  printf(_("Usage:\n"));
289  printf(_(" %s [OPTION]... [DBNAME] [DESCRIPTION]\n"), progname);
290  printf(_("\nOptions:\n"));
291  printf(_(" -D, --tablespace=TABLESPACE default tablespace for the database\n"));
292  printf(_(" -e, --echo show the commands being sent to the server\n"));
293  printf(_(" -E, --encoding=ENCODING encoding for the database\n"));
294  printf(_(" -l, --locale=LOCALE locale settings for the database\n"));
295  printf(_(" --lc-collate=LOCALE LC_COLLATE setting for the database\n"));
296  printf(_(" --lc-ctype=LOCALE LC_CTYPE setting for the database\n"));
297  printf(_(" --icu-locale=LOCALE ICU locale setting for the database\n"));
298  printf(_(" --icu-rules=RULES ICU rules setting for the database\n"));
299  printf(_(" --locale-provider={libc|icu}\n"
300  " locale provider for the database's default collation\n"));
301  printf(_(" -O, --owner=OWNER database user to own the new database\n"));
302  printf(_(" -S, --strategy=STRATEGY database creation strategy wal_log or file_copy\n"));
303  printf(_(" -T, --template=TEMPLATE template database to copy\n"));
304  printf(_(" -V, --version output version information, then exit\n"));
305  printf(_(" -?, --help show this help, then exit\n"));
306  printf(_("\nConnection options:\n"));
307  printf(_(" -h, --host=HOSTNAME database server host or socket directory\n"));
308  printf(_(" -p, --port=PORT database server port\n"));
309  printf(_(" -U, --username=USERNAME user name to connect as\n"));
310  printf(_(" -w, --no-password never prompt for password\n"));
311  printf(_(" -W, --password force password prompt\n"));
312  printf(_(" --maintenance-db=DBNAME alternate maintenance database\n"));
313  printf(_("\nBy default, a database with the same name as the current user is created.\n"));
314  printf(_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
315  printf(_("%s home page: <%s>\n"), PACKAGE_NAME, PACKAGE_URL);
316 }
#define PG_TEXTDOMAIN(domain)
Definition: c.h:1227
void set_pglocale_pgservice(const char *argv0, const char *app)
Definition: exec.c:436
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:285
#define _(x)
Definition: elog.c:91
int pg_char_to_encoding(const char *name)
Definition: encnames.c:550
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:7248
void PQfinish(PGconn *conn)
Definition: fe-connect.c:4602
ExecStatusType PQresultStatus(const PGresult *res)
Definition: fe-exec.c:3325
PGresult * PQexec(PGconn *conn, const char *query)
Definition: fe-exec.c:2228
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:24
#define required_argument
Definition: getopt_long.h:25
#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:149
static char * locale
Definition: initdb.c:140
static char * icu_locale
Definition: initdb.c:148
@ PGRES_COMMAND_OK
Definition: libpq-fe.h:97
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:45
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:50
PGDLLIMPORT char * optarg
Definition: getopt.c:52
static int port
Definition: pg_regress.c:109
const char * username
Definition: pgbench.c:296
char * tablespace
Definition: pgbench.c:216
const char * get_progname(const char *argv0)
Definition: path.c:574
#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:51
PGconn * conn
Definition: streamutil.c:54
void appendStringLiteralConn(PQExpBuffer buf, const char *str, PGconn *conn)
Definition: string_utils.c:293
const char * fmtId(const char *rawid)
Definition: string_utils.c:64
const char * pguser
Definition: connect_utils.h:31
char * override_dbname
Definition: pg_backup.h:91
char * pgport
Definition: pg_backup.h:85
char * pghost
Definition: pg_backup.h:86
char * dbname
Definition: pg_backup.h:84
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