1 /*-------------------------------------------------------------------------
5 * Portions Copyright (c) 1996-2009, PostgreSQL Global Development Group
6 * Portions Copyright (c) 1994, Regents of the University of California
10 *-------------------------------------------------------------------------
12 #include "postgres_fe.h"
15 #include "dumputils.h"
18 static void help(const char *progname
);
22 main(int argc
, char *argv
[])
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'},
45 const char *dbname
= NULL
;
49 char *username
= NULL
;
50 enum trivalue prompt_password
= TRI_DEFAULT
;
53 char *tablespace
= NULL
;
54 char *template = NULL
;
55 char *encoding
= NULL
;
56 char *lc_collate
= NULL
;
57 char *lc_ctype
= NULL
;
65 progname
= get_progname(argv
[0]);
66 set_pglocale_pgservice(argv
[0], PG_TEXTDOMAIN("pgscripts"));
68 handle_help_version_opts(argc
, argv
, "createdb", help
);
70 while ((c
= getopt_long(argc
, argv
, "h:p:U:wWeO:D:T:E:l:", long_options
, &optindex
)) != -1)
84 prompt_password
= TRI_NO
;
87 prompt_password
= TRI_YES
;
114 fprintf(stderr
, _("Try \"%s --help\" for more information.\n"), progname
);
119 switch (argc
- optind
)
124 dbname
= argv
[optind
];
127 dbname
= argv
[optind
];
128 comment
= argv
[optind
+ 1];
131 fprintf(stderr
, _("%s: too many command-line arguments (first is \"%s\")\n"),
132 progname
, argv
[optind
+ 2]);
133 fprintf(stderr
, _("Try \"%s --help\" for more information.\n"), progname
);
141 fprintf(stderr
, _("%s: only one of --locale and --lc-ctype can be specified\n"),
147 fprintf(stderr
, _("%s: only one of --locale and --lc-collate can be specified\n"),
157 if (pg_char_to_encoding(encoding
) < 0)
159 fprintf(stderr
, _("%s: \"%s\" is not a valid encoding name\n"),
167 if (getenv("PGDATABASE"))
168 dbname
= getenv("PGDATABASE");
169 else if (getenv("PGUSER"))
170 dbname
= getenv("PGUSER");
172 dbname
= get_user_name(progname
);
175 initPQExpBuffer(&sql
);
177 appendPQExpBuffer(&sql
, "CREATE DATABASE %s",
181 appendPQExpBuffer(&sql
, " OWNER %s", fmtId(owner
));
183 appendPQExpBuffer(&sql
, " TABLESPACE %s", fmtId(tablespace
));
185 appendPQExpBuffer(&sql
, " ENCODING '%s'", encoding
);
187 appendPQExpBuffer(&sql
, " TEMPLATE %s", fmtId(template));
189 appendPQExpBuffer(&sql
, " LC_COLLATE '%s'", lc_collate
);
191 appendPQExpBuffer(&sql
, " LC_CTYPE '%s'", lc_ctype
);
193 appendPQExpBuffer(&sql
, ";\n");
195 conn
= connectDatabase(strcmp(dbname
, "postgres") == 0 ? "template1" : "postgres",
196 host
, port
, username
, prompt_password
, progname
);
199 printf("%s", sql
.data
);
200 result
= PQexec(conn
, sql
.data
);
202 if (PQresultStatus(result
) != PGRES_COMMAND_OK
)
204 fprintf(stderr
, _("%s: database creation failed: %s"),
205 progname
, PQerrorMessage(conn
));
215 conn
= connectDatabase(dbname
, host
, port
, username
, prompt_password
, progname
);
217 printfPQExpBuffer(&sql
, "COMMENT ON DATABASE %s IS ", fmtId(dbname
));
218 appendStringLiteralConn(&sql
, comment
, conn
);
219 appendPQExpBuffer(&sql
, ";\n");
222 printf("%s", sql
.data
);
223 result
= PQexec(conn
, sql
.data
);
225 if (PQresultStatus(result
) != PGRES_COMMAND_OK
)
227 fprintf(stderr
, _("%s: comment creation failed (database was created): %s"),
228 progname
, PQerrorMessage(conn
));
242 help(const char *progname
)
244 printf(_("%s creates a PostgreSQL database.\n\n"), progname
);
245 printf(_("Usage:\n"));
246 printf(_(" %s [OPTION]... [DBNAME] [DESCRIPTION]\n"), progname
);
247 printf(_("\nOptions:\n"));
248 printf(_(" -D, --tablespace=TABLESPACE default tablespace for the database\n"));
249 printf(_(" -e, --echo show the commands being sent to the server\n"));
250 printf(_(" -E, --encoding=ENCODING encoding for the database\n"));
251 printf(_(" -l, --locale=LOCALE locale settings for the database\n"));
252 printf(_(" --lc-collate=LOCALE LC_COLLATE setting for the database\n"));
253 printf(_(" --lc-ctype=LOCALE LC_CTYPE setting for the database\n"));
254 printf(_(" -O, --owner=OWNER database user to own the new database\n"));
255 printf(_(" -T, --template=TEMPLATE template database to copy\n"));
256 printf(_(" --help show this help, then exit\n"));
257 printf(_(" --version output version information, then exit\n"));
258 printf(_("\nConnection options:\n"));
259 printf(_(" -h, --host=HOSTNAME database server host or socket directory\n"));
260 printf(_(" -p, --port=PORT database server port\n"));
261 printf(_(" -U, --username=USERNAME user name to connect as\n"));
262 printf(_(" -w, --no-password never prompt for password\n"));
263 printf(_(" -W, --password force password prompt\n"));
264 printf(_("\nBy default, a database with the same name as the current user is created.\n"));
265 printf(_("\nReport bugs to <pgsql-bugs@postgresql.org>.\n"));