PostgreSQL Source Code  git master
createdb.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * createdb
4  *
5  * Portions Copyright (c) 1996-2020, 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/string_utils.h"
17 
18 
19 static void help(const char *progname);
20 
21 
22 int
23 main(int argc, char *argv[])
24 {
25  static struct option long_options[] = {
26  {"host", required_argument, NULL, 'h'},
27  {"port", required_argument, NULL, 'p'},
28  {"username", required_argument, NULL, 'U'},
29  {"no-password", no_argument, NULL, 'w'},
30  {"password", no_argument, NULL, 'W'},
31  {"echo", no_argument, NULL, 'e'},
32  {"owner", required_argument, NULL, 'O'},
33  {"tablespace", required_argument, NULL, 'D'},
34  {"template", required_argument, NULL, 'T'},
35  {"encoding", required_argument, NULL, 'E'},
36  {"lc-collate", required_argument, NULL, 1},
37  {"lc-ctype", required_argument, NULL, 2},
38  {"locale", required_argument, NULL, 'l'},
39  {"maintenance-db", required_argument, NULL, 3},
40  {NULL, 0, NULL, 0}
41  };
42 
43  const char *progname;
44  int optindex;
45  int c;
46 
47  const char *dbname = NULL;
48  const char *maintenance_db = NULL;
49  char *comment = NULL;
50  char *host = NULL;
51  char *port = NULL;
52  char *username = NULL;
53  enum trivalue prompt_password = TRI_DEFAULT;
54  bool echo = false;
55  char *owner = NULL;
56  char *tablespace = NULL;
57  char *template = NULL;
58  char *encoding = NULL;
59  char *lc_collate = NULL;
60  char *lc_ctype = NULL;
61  char *locale = NULL;
62 
63  PQExpBufferData sql;
64 
65  PGconn *conn;
66  PGresult *result;
67 
68  pg_logging_init(argv[0]);
69  progname = get_progname(argv[0]);
70  set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pgscripts"));
71 
72  handle_help_version_opts(argc, argv, "createdb", help);
73 
74  while ((c = getopt_long(argc, argv, "h:p:U:wWeO:D:T:E:l:", long_options, &optindex)) != -1)
75  {
76  switch (c)
77  {
78  case 'h':
79  host = pg_strdup(optarg);
80  break;
81  case 'p':
82  port = pg_strdup(optarg);
83  break;
84  case 'U':
85  username = pg_strdup(optarg);
86  break;
87  case 'w':
88  prompt_password = TRI_NO;
89  break;
90  case 'W':
91  prompt_password = TRI_YES;
92  break;
93  case 'e':
94  echo = true;
95  break;
96  case 'O':
97  owner = pg_strdup(optarg);
98  break;
99  case 'D':
100  tablespace = pg_strdup(optarg);
101  break;
102  case 'T':
103  template = pg_strdup(optarg);
104  break;
105  case 'E':
106  encoding = pg_strdup(optarg);
107  break;
108  case 1:
109  lc_collate = pg_strdup(optarg);
110  break;
111  case 2:
112  lc_ctype = pg_strdup(optarg);
113  break;
114  case 'l':
115  locale = pg_strdup(optarg);
116  break;
117  case 3:
118  maintenance_db = pg_strdup(optarg);
119  break;
120  default:
121  fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
122  exit(1);
123  }
124  }
125 
126  switch (argc - optind)
127  {
128  case 0:
129  break;
130  case 1:
131  dbname = argv[optind];
132  break;
133  case 2:
134  dbname = argv[optind];
135  comment = argv[optind + 1];
136  break;
137  default:
138  pg_log_error("too many command-line arguments (first is \"%s\")",
139  argv[optind + 2]);
140  fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
141  exit(1);
142  }
143 
144  if (locale)
145  {
146  if (lc_ctype)
147  {
148  pg_log_error("only one of --locale and --lc-ctype can be specified");
149  exit(1);
150  }
151  if (lc_collate)
152  {
153  pg_log_error("only one of --locale and --lc-collate can be specified");
154  exit(1);
155  }
156  lc_ctype = locale;
157  lc_collate = locale;
158  }
159 
160  if (encoding)
161  {
162  if (pg_char_to_encoding(encoding) < 0)
163  {
164  pg_log_error("\"%s\" is not a valid encoding name", encoding);
165  exit(1);
166  }
167  }
168 
169  if (dbname == NULL)
170  {
171  if (getenv("PGDATABASE"))
172  dbname = getenv("PGDATABASE");
173  else if (getenv("PGUSER"))
174  dbname = getenv("PGUSER");
175  else
176  dbname = get_user_name_or_exit(progname);
177  }
178 
179  /* No point in trying to use postgres db when creating postgres db. */
180  if (maintenance_db == NULL && strcmp(dbname, "postgres") == 0)
181  maintenance_db = "template1";
182 
183  conn = connectMaintenanceDatabase(maintenance_db, host, port, username,
184  prompt_password, progname, echo);
185 
186  initPQExpBuffer(&sql);
187 
188  appendPQExpBuffer(&sql, "CREATE DATABASE %s",
189  fmtId(dbname));
190 
191  if (owner)
192  appendPQExpBuffer(&sql, " OWNER %s", fmtId(owner));
193  if (tablespace)
194  appendPQExpBuffer(&sql, " TABLESPACE %s", fmtId(tablespace));
195  if (encoding)
196  {
197  appendPQExpBufferStr(&sql, " ENCODING ");
198  appendStringLiteralConn(&sql, encoding, conn);
199  }
200  if (template)
201  appendPQExpBuffer(&sql, " TEMPLATE %s", fmtId(template));
202  if (lc_collate)
203  {
204  appendPQExpBufferStr(&sql, " LC_COLLATE ");
205  appendStringLiteralConn(&sql, lc_collate, conn);
206  }
207  if (lc_ctype)
208  {
209  appendPQExpBufferStr(&sql, " LC_CTYPE ");
210  appendStringLiteralConn(&sql, lc_ctype, conn);
211  }
212 
213  appendPQExpBufferChar(&sql, ';');
214 
215  if (echo)
216  printf("%s\n", sql.data);
217  result = PQexec(conn, sql.data);
218 
219  if (PQresultStatus(result) != PGRES_COMMAND_OK)
220  {
221  pg_log_error("database creation failed: %s", PQerrorMessage(conn));
222  PQfinish(conn);
223  exit(1);
224  }
225 
226  PQclear(result);
227 
228  if (comment)
229  {
230  printfPQExpBuffer(&sql, "COMMENT ON DATABASE %s IS ", fmtId(dbname));
231  appendStringLiteralConn(&sql, comment, conn);
232  appendPQExpBufferChar(&sql, ';');
233 
234  if (echo)
235  printf("%s\n", sql.data);
236  result = PQexec(conn, sql.data);
237 
238  if (PQresultStatus(result) != PGRES_COMMAND_OK)
239  {
240  pg_log_error("comment creation failed (database was created): %s",
241  PQerrorMessage(conn));
242  PQfinish(conn);
243  exit(1);
244  }
245 
246  PQclear(result);
247  }
248 
249  PQfinish(conn);
250 
251  exit(0);
252 }
253 
254 
255 static void
256 help(const char *progname)
257 {
258  printf(_("%s creates a PostgreSQL database.\n\n"), progname);
259  printf(_("Usage:\n"));
260  printf(_(" %s [OPTION]... [DBNAME] [DESCRIPTION]\n"), progname);
261  printf(_("\nOptions:\n"));
262  printf(_(" -D, --tablespace=TABLESPACE default tablespace for the database\n"));
263  printf(_(" -e, --echo show the commands being sent to the server\n"));
264  printf(_(" -E, --encoding=ENCODING encoding for the database\n"));
265  printf(_(" -l, --locale=LOCALE locale settings for the database\n"));
266  printf(_(" --lc-collate=LOCALE LC_COLLATE setting for the database\n"));
267  printf(_(" --lc-ctype=LOCALE LC_CTYPE setting for the database\n"));
268  printf(_(" -O, --owner=OWNER database user to own the new database\n"));
269  printf(_(" -T, --template=TEMPLATE template database to copy\n"));
270  printf(_(" -V, --version output version information, then exit\n"));
271  printf(_(" -?, --help show this help, then exit\n"));
272  printf(_("\nConnection options:\n"));
273  printf(_(" -h, --host=HOSTNAME database server host or socket directory\n"));
274  printf(_(" -p, --port=PORT database server port\n"));
275  printf(_(" -U, --username=USERNAME user name to connect as\n"));
276  printf(_(" -w, --no-password never prompt for password\n"));
277  printf(_(" -W, --password force password prompt\n"));
278  printf(_(" --maintenance-db=DBNAME alternate maintenance database\n"));
279  printf(_("\nBy default, a database with the same name as the current user is created.\n"));
280  printf(_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
281  printf(_("%s home page: <%s>\n"), PACKAGE_NAME, PACKAGE_URL);
282 }
PGconn * connectMaintenanceDatabase(const char *maintenance_db, const char *pghost, const char *pgport, const char *pguser, enum trivalue prompt_password, const char *progname, bool echo)
Definition: common.c:153
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:6687
void printfPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:237
int pg_char_to_encoding(const char *name)
Definition: encnames.c:550
static void help(const char *progname)
Definition: createdb.c:256
static char * lc_collate
Definition: initdb.c:126
const char * fmtId(const char *rawid)
Definition: string_utils.c:64
const char * get_progname(const char *argv0)
Definition: path.c:453
#define pg_log_error(...)
Definition: logging.h:79
int getopt_long(int argc, char *const argv[], const char *optstring, const struct option *longopts, int *longindex)
Definition: getopt_long.c:57
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:369
void pg_logging_init(const char *argv0)
Definition: logging.c:76
void PQfinish(PGconn *conn)
Definition: fe-connect.c:4185
#define printf(...)
Definition: port.h:199
const char * progname
Definition: pg_standby.c:36
#define fprintf
Definition: port.h:197
ExecStatusType PQresultStatus(const PGresult *res)
Definition: fe-exec.c:2692
#define required_argument
Definition: getopt_long.h:25
int optind
Definition: getopt.c:50
PGconn * conn
Definition: streamutil.c:54
static char * lc_ctype
Definition: initdb.c:127
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:267
char * c
char * tablespace
Definition: pgbench.c:188
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
static int port
Definition: pg_regress.c:90
void handle_help_version_opts(int argc, char *argv[], const char *fixed_progname, help_handler hlp)
Definition: common.c:33
trivalue
Definition: vacuumlo.c:33
#define no_argument
Definition: getopt_long.h:24
#define PG_TEXTDOMAIN(domain)
Definition: c.h:1178
void appendPQExpBufferChar(PQExpBuffer str, char ch)
Definition: pqexpbuffer.c:380
static char * username
Definition: initdb.c:133
void PQclear(PGresult *res)
Definition: fe-exec.c:694
char * dbname
Definition: streamutil.c:50
int32 encoding
Definition: pg_database.h:41
void appendStringLiteralConn(PQExpBuffer buf, const char *str, PGconn *conn)
Definition: string_utils.c:293
void set_pglocale_pgservice(const char *argv0, const char *app)
Definition: exec.c:434
char * optarg
Definition: getopt.c:52
static char * locale
Definition: initdb.c:125
PGresult * PQexec(PGconn *conn, const char *query)
Definition: fe-exec.c:1939
int main(int argc, char *argv[])
Definition: createdb.c:23
#define _(x)
Definition: elog.c:88
void initPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:92
const char * get_user_name_or_exit(const char *progname)
Definition: username.c:74