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