PostgreSQL Source Code  git master
createdb.c File Reference
#include "postgres_fe.h"
#include "common.h"
#include "common/logging.h"
#include "fe_utils/option_utils.h"
#include "fe_utils/string_utils.h"
Include dependency graph for createdb.c:

Go to the source code of this file.

Functions

static void help (const char *progname)
 
int main (int argc, char *argv[])
 

Function Documentation

◆ help()

static void help ( const char *  progname)
static

Definition at line 278 of file createdb.c.

279 {
280  printf(_("%s creates a PostgreSQL database.\n\n"), progname);
281  printf(_("Usage:\n"));
282  printf(_(" %s [OPTION]... [DBNAME] [DESCRIPTION]\n"), progname);
283  printf(_("\nOptions:\n"));
284  printf(_(" -D, --tablespace=TABLESPACE default tablespace for the database\n"));
285  printf(_(" -e, --echo show the commands being sent to the server\n"));
286  printf(_(" -E, --encoding=ENCODING encoding for the database\n"));
287  printf(_(" -l, --locale=LOCALE locale settings for the database\n"));
288  printf(_(" --lc-collate=LOCALE LC_COLLATE setting for the database\n"));
289  printf(_(" --lc-ctype=LOCALE LC_CTYPE setting for the database\n"));
290  printf(_(" --icu-locale=LOCALE ICU locale setting for the database\n"));
291  printf(_(" --locale-provider={libc|icu}\n"
292  " locale provider for the database's default collation\n"));
293  printf(_(" -O, --owner=OWNER database user to own the new database\n"));
294  printf(_(" -S, --strategy=STRATEGY database creation strategy wal_log or file_copy\n"));
295  printf(_(" -T, --template=TEMPLATE template database to copy\n"));
296  printf(_(" -V, --version output version information, then exit\n"));
297  printf(_(" -?, --help show this help, then exit\n"));
298  printf(_("\nConnection options:\n"));
299  printf(_(" -h, --host=HOSTNAME database server host or socket directory\n"));
300  printf(_(" -p, --port=PORT database server port\n"));
301  printf(_(" -U, --username=USERNAME user name to connect as\n"));
302  printf(_(" -w, --no-password never prompt for password\n"));
303  printf(_(" -W, --password force password prompt\n"));
304  printf(_(" --maintenance-db=DBNAME alternate maintenance database\n"));
305  printf(_("\nBy default, a database with the same name as the current user is created.\n"));
306  printf(_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
307  printf(_("%s home page: <%s>\n"), PACKAGE_NAME, PACKAGE_URL);
308 }
#define _(x)
Definition: elog.c:90
const char * progname
Definition: main.c:45
#define printf(...)
Definition: port.h:244

References _, printf, and progname.

Referenced by main().

◆ main()

int main ( int  argc,
char *  argv[] 
)

Definition at line 24 of file createdb.c.

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  {NULL, 0, NULL, 0}
45  };
46 
47  const char *progname;
48  int optindex;
49  int c;
50 
51  const char *dbname = NULL;
52  const char *maintenance_db = NULL;
53  char *comment = NULL;
54  char *host = NULL;
55  char *port = NULL;
56  char *username = NULL;
57  enum trivalue prompt_password = TRI_DEFAULT;
58  ConnParams cparams;
59  bool echo = false;
60  char *owner = NULL;
61  char *tablespace = NULL;
62  char *template = NULL;
63  char *encoding = NULL;
64  char *strategy = NULL;
65  char *lc_collate = NULL;
66  char *lc_ctype = NULL;
67  char *locale = NULL;
68  char *locale_provider = NULL;
69  char *icu_locale = NULL;
70 
71  PQExpBufferData sql;
72 
73  PGconn *conn;
74  PGresult *result;
75 
76  pg_logging_init(argv[0]);
77  progname = get_progname(argv[0]);
78  set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pgscripts"));
79 
80  handle_help_version_opts(argc, argv, "createdb", help);
81 
82  while ((c = getopt_long(argc, argv, "h:p:U:wWeO:D:T:E:l:S:", long_options, &optindex)) != -1)
83  {
84  switch (c)
85  {
86  case 'h':
87  host = pg_strdup(optarg);
88  break;
89  case 'p':
91  break;
92  case 'U':
94  break;
95  case 'w':
96  prompt_password = TRI_NO;
97  break;
98  case 'W':
99  prompt_password = TRI_YES;
100  break;
101  case 'e':
102  echo = true;
103  break;
104  case 'O':
105  owner = pg_strdup(optarg);
106  break;
107  case 'D':
109  break;
110  case 'T':
111  template = pg_strdup(optarg);
112  break;
113  case 'E':
115  break;
116  case 'S':
117  strategy = pg_strdup(optarg);
118  break;
119  case 1:
121  break;
122  case 2:
124  break;
125  case 'l':
127  break;
128  case 3:
129  maintenance_db = pg_strdup(optarg);
130  break;
131  case 4:
133  break;
134  case 5:
136  break;
137  default:
138  /* getopt_long already emitted a complaint */
139  pg_log_error_hint("Try \"%s --help\" for more information.", progname);
140  exit(1);
141  }
142  }
143 
144  switch (argc - optind)
145  {
146  case 0:
147  break;
148  case 1:
149  dbname = argv[optind];
150  break;
151  case 2:
152  dbname = argv[optind];
153  comment = argv[optind + 1];
154  break;
155  default:
156  pg_log_error("too many command-line arguments (first is \"%s\")",
157  argv[optind + 2]);
158  pg_log_error_hint("Try \"%s --help\" for more information.", progname);
159  exit(1);
160  }
161 
162  if (locale)
163  {
164  if (!lc_ctype)
165  lc_ctype = locale;
166  if (!lc_collate)
167  lc_collate = locale;
168  }
169 
170  if (encoding)
171  {
172  if (pg_char_to_encoding(encoding) < 0)
173  pg_fatal("\"%s\" is not a valid encoding name", encoding);
174  }
175 
176  if (dbname == NULL)
177  {
178  if (getenv("PGDATABASE"))
179  dbname = getenv("PGDATABASE");
180  else if (getenv("PGUSER"))
181  dbname = getenv("PGUSER");
182  else
184  }
185 
186  /* No point in trying to use postgres db when creating postgres db. */
187  if (maintenance_db == NULL && strcmp(dbname, "postgres") == 0)
188  maintenance_db = "template1";
189 
190  cparams.dbname = maintenance_db;
191  cparams.pghost = host;
192  cparams.pgport = port;
193  cparams.pguser = username;
194  cparams.prompt_password = prompt_password;
195  cparams.override_dbname = NULL;
196 
197  conn = connectMaintenanceDatabase(&cparams, progname, echo);
198 
199  initPQExpBuffer(&sql);
200 
201  appendPQExpBuffer(&sql, "CREATE DATABASE %s",
202  fmtId(dbname));
203 
204  if (owner)
205  appendPQExpBuffer(&sql, " OWNER %s", fmtId(owner));
206  if (tablespace)
207  appendPQExpBuffer(&sql, " TABLESPACE %s", fmtId(tablespace));
208  if (encoding)
209  {
210  appendPQExpBufferStr(&sql, " ENCODING ");
212  }
213  if (strategy)
214  appendPQExpBuffer(&sql, " STRATEGY %s", fmtId(strategy));
215  if (template)
216  appendPQExpBuffer(&sql, " TEMPLATE %s", fmtId(template));
217  if (lc_collate)
218  {
219  appendPQExpBufferStr(&sql, " LC_COLLATE ");
221  }
222  if (lc_ctype)
223  {
224  appendPQExpBufferStr(&sql, " LC_CTYPE ");
226  }
227  if (locale_provider)
228  appendPQExpBuffer(&sql, " LOCALE_PROVIDER %s", locale_provider);
229  if (icu_locale)
230  {
231  appendPQExpBufferStr(&sql, " ICU_LOCALE ");
233  }
234 
235  appendPQExpBufferChar(&sql, ';');
236 
237  if (echo)
238  printf("%s\n", sql.data);
239  result = PQexec(conn, sql.data);
240 
241  if (PQresultStatus(result) != PGRES_COMMAND_OK)
242  {
243  pg_log_error("database creation failed: %s", PQerrorMessage(conn));
244  PQfinish(conn);
245  exit(1);
246  }
247 
248  PQclear(result);
249 
250  if (comment)
251  {
252  printfPQExpBuffer(&sql, "COMMENT ON DATABASE %s IS ", fmtId(dbname));
253  appendStringLiteralConn(&sql, comment, conn);
254  appendPQExpBufferChar(&sql, ';');
255 
256  if (echo)
257  printf("%s\n", sql.data);
258  result = PQexec(conn, sql.data);
259 
260  if (PQresultStatus(result) != PGRES_COMMAND_OK)
261  {
262  pg_log_error("comment creation failed (database was created): %s",
264  PQfinish(conn);
265  exit(1);
266  }
267 
268  PQclear(result);
269  }
270 
271  PQfinish(conn);
272 
273  exit(0);
274 }
#define PG_TEXTDOMAIN(domain)
Definition: c.h:1153
void set_pglocale_pgservice(const char *argv0, const char *app)
Definition: exec.c:460
PGconn * connectMaintenanceDatabase(ConnParams *cparams, const char *progname, bool echo)
static void help(const char *progname)
Definition: createdb.c:278
int pg_char_to_encoding(const char *name)
Definition: encnames.c:550
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:6743
void PQfinish(PGconn *conn)
Definition: fe-connect.c:4130
ExecStatusType PQresultStatus(const PGresult *res)
Definition: fe-exec.c:3240
PGresult * PQexec(PGconn *conn, const char *query)
Definition: fe-exec.c:2225
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:57
#define no_argument
Definition: getopt_long.h:24
#define required_argument
Definition: getopt_long.h:25
static char * lc_collate
Definition: initdb.c:130
static char * lc_ctype
Definition: initdb.c:131
static char locale_provider
Definition: initdb.c:136
static char * locale
Definition: initdb.c:129
static char * icu_locale
Definition: initdb.c:137
@ 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
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:90
const char * username
Definition: pgbench.c:305
char * tablespace
Definition: pgbench.c:225
const char * get_progname(const char *argv0)
Definition: path.c:574
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:90
char * pgport
Definition: pg_backup.h:84
char * pghost
Definition: pg_backup.h:85
char * dbname
Definition: pg_backup.h:83
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

References appendPQExpBuffer(), appendPQExpBufferChar(), appendPQExpBufferStr(), appendStringLiteralConn(), conn, connectMaintenanceDatabase(), PQExpBufferData::data, dbname, _connParams::dbname, encoding, exit(), fmtId(), get_progname(), get_user_name_or_exit(), getopt_long(), handle_help_version_opts(), help(), icu_locale, initPQExpBuffer(), lc_collate, lc_ctype, locale, locale_provider, no_argument, optarg, optind, _connParams::override_dbname, pg_char_to_encoding(), pg_fatal, pg_log_error, pg_log_error_hint, pg_logging_init(), pg_strdup(), PG_TEXTDOMAIN, _connParams::pghost, _connParams::pgport, PGRES_COMMAND_OK, _connParams::pguser, port, PQclear(), PQerrorMessage(), PQexec(), PQfinish(), PQresultStatus(), printf, printfPQExpBuffer(), progname, _connParams::prompt_password, required_argument, set_pglocale_pgservice(), tablespace, TRI_DEFAULT, TRI_NO, TRI_YES, and username.