PostgreSQL Source Code: src/bin/scripts/createdb.c Source File (original) (raw)
1
2
3
4
5
6
7
8
9
10
11
13
18
19
21
22
23int
24main(int argc, char *argv[])
25{
26 static struct option long_options[] = {
46 {NULL, 0, NULL, 0}
47 };
48
50 int optindex;
51 int c;
52
53 const char *dbname = NULL;
54 const char *maintenance_db = NULL;
56 char *host = NULL;
57 char *port = NULL;
61 bool echo = false;
62 char *owner = NULL;
64 char *template = NULL;
66 char *strategy = NULL;
71 char *builtin_locale = NULL;
72 char *icu_locale = NULL;
74
76
79
83
85
86 while ((c = getopt_long(argc, argv, "D:eE:h:l:O:p:S:T:U:wW", long_options, &optindex)) != -1)
87 {
88 switch (c)
89 {
90 case 'D':
92 break;
93 case 'e':
94 echo = true;
95 break;
96 case 'E':
98 break;
99 case 'h':
101 break;
102 case 'l':
104 break;
105 case 'O':
107 break;
108 case 'p':
110 break;
111 case 'S':
113 break;
114 case 'T':
116 break;
117 case 'U':
119 break;
120 case 'w':
121 prompt_password = TRI_NO;
122 break;
123 case 'W':
124 prompt_password = TRI_YES;
125 break;
126 case 1:
128 break;
129 case 2:
131 break;
132 case 3:
134 break;
135 case 4:
137 break;
138 case 5:
140 break;
141 case 6:
143 break;
144 case 7:
146 break;
147 default:
148
150 exit(1);
151 }
152 }
153
154 switch (argc - optind)
155 {
156 case 0:
157 break;
158 case 1:
160 break;
161 case 2:
164 break;
165 default:
166 pg_log_error("too many command-line arguments (first is \"%s\")",
169 exit(1);
170 }
171
173 {
176 }
177
179 {
180 if (getenv("PGDATABASE"))
181 dbname = getenv("PGDATABASE");
182 else if (getenv("PGUSER"))
183 dbname = getenv("PGUSER");
184 else
186 }
187
188
189 if (maintenance_db == NULL && strcmp(dbname, "postgres") == 0)
190 maintenance_db = "template1";
191
192 cparams.dbname = maintenance_db;
193 cparams.pghost = host;
198
200
202
204
207
208 if (owner)
213 {
216 }
217 if (strategy)
219 if (template)
222 {
225 }
226 if (builtin_locale)
227 {
230 }
232 {
235 }
237 {
240 }
243 if (icu_locale)
244 {
247 }
249 {
252 }
253
255
256 if (echo)
259
261 {
264 exit(1);
265 }
266
268
270 {
274
275 if (echo)
278
280 {
281 pg_log_error("comment creation failed (database was created): %s",
284 exit(1);
285 }
286
288 }
289
291
292 exit(0);
293}
294
295
296static void
298{
299 printf(_("%s creates a PostgreSQL database.\n\n"), progname);
301 printf(_(" %s [OPTION]... [DBNAME] [DESCRIPTION]\n"), progname);
302 printf(_("\nOptions:\n"));
303 printf(_(" -D, --tablespace=TABLESPACE default tablespace for the database\n"));
304 printf(_(" -e, --echo show the commands being sent to the server\n"));
305 printf(_(" -E, --encoding=ENCODING encoding for the database\n"));
306 printf(_(" -l, --locale=LOCALE locale settings for the database\n"));
307 printf(_(" --lc-collate=LOCALE LC_COLLATE setting for the database\n"));
308 printf(_(" --lc-ctype=LOCALE LC_CTYPE setting for the database\n"));
309 printf(_(" --builtin-locale=LOCALE builtin locale setting for the database\n"));
310 printf(_(" --icu-locale=LOCALE ICU locale setting for the database\n"));
311 printf(_(" --icu-rules=RULES ICU rules setting for the database\n"));
312 printf(_(" --locale-provider={builtin|libc|icu}\n"
313 " locale provider for the database's default collation\n"));
314 printf(_(" -O, --owner=OWNER database user to own the new database\n"));
315 printf(_(" -S, --strategy=STRATEGY database creation strategy wal_log or file_copy\n"));
316 printf(_(" -T, --template=TEMPLATE template database to copy\n"));
317 printf(_(" -V, --version output version information, then exit\n"));
318 printf(_(" -?, --help show this help, then exit\n"));
319 printf(_("\nConnection options:\n"));
320 printf(_(" -h, --host=HOSTNAME database server host or socket directory\n"));
321 printf(_(" -p, --port=PORT database server port\n"));
322 printf(_(" -U, --username=USERNAME user name to connect as\n"));
323 printf(_(" -w, --no-password never prompt for password\n"));
324 printf(_(" -W, --password force password prompt\n"));
325 printf(_(" --maintenance-db=DBNAME alternate maintenance database\n"));
326 printf(_("\nBy default, a database with the same name as the current user is created.\n"));
327 printf(_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
328 printf(_("%s home page: <%s>\n"), PACKAGE_NAME, PACKAGE_URL);
329}
#define PG_TEXTDOMAIN(domain)
void set_pglocale_pgservice(const char *argv0, const char *app)
PGconn * connectMaintenanceDatabase(ConnParams *cparams, const char *progname, bool echo)
int main(int argc, char *argv[])
static void help(const char *progname)
int PQclientEncoding(const PGconn *conn)
void PQfinish(PGconn *conn)
char * PQerrorMessage(const PGconn *conn)
ExecStatusType PQresultStatus(const PGresult *res)
void PQclear(PGresult *res)
PGresult * PQexec(PGconn *conn, const char *query)
char * pg_strdup(const char *in)
int getopt_long(int argc, char *const argv[], const char *optstring, const struct option *longopts, int *longindex)
#define required_argument
static char locale_provider
void pg_logging_init(const char *argv0)
#define pg_log_error(...)
#define pg_log_error_hint(...)
void handle_help_version_opts(int argc, char *argv[], const char *fixed_progname, help_handler hlp)
PGDLLIMPORT char * optarg
#define pg_char_to_encoding
const char * get_progname(const char *argv0)
void printfPQExpBuffer(PQExpBuffer str, const char *fmt,...)
void initPQExpBuffer(PQExpBuffer str)
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
void appendPQExpBufferChar(PQExpBuffer str, char ch)
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
const char * fmtId(const char *rawid)
void setFmtEncoding(int encoding)
void appendStringLiteralConn(PQExpBuffer buf, const char *str, PGconn *conn)
enum trivalue prompt_password
const char * get_user_name_or_exit(const char *progname)