]> granicus.if.org Git - postgresql/blob - src/bin/scripts/createdb.c
Remove init_nls() functions, call set_pglocale() directly.
[postgresql] / src / bin / scripts / createdb.c
1 /*-------------------------------------------------------------------------
2  *
3  * createdb
4  *
5  * Portions Copyright (c) 1996-2003, PostgreSQL Global Development Group
6  * Portions Copyright (c) 1994, Regents of the University of California
7  *
8  * $PostgreSQL: pgsql/src/bin/scripts/createdb.c,v 1.10 2004/06/01 02:54:09 momjian Exp $
9  *
10  *-------------------------------------------------------------------------
11  */
12
13 #include "postgres_fe.h"
14 #include "common.h"
15 #include "dumputils.h"
16
17 #include "mb/pg_wchar.h"
18
19
20 static void help(const char *progname);
21
22
23 int
24 main(int argc, char *argv[])
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                 {"password", no_argument, NULL, 'W'},
31                 {"echo", no_argument, NULL, 'e'},
32                 {"quiet", no_argument, NULL, 'q'},
33                 {"owner", required_argument, NULL, 'O'},
34                 {"location", required_argument, NULL, 'D'},
35                 {"template", required_argument, NULL, 'T'},
36                 {"encoding", required_argument, NULL, 'E'},
37                 {NULL, 0, NULL, 0}
38         };
39
40         const char *progname;
41         int                     optindex;
42         int                     c;
43
44         const char *dbname = NULL;
45         char       *comment = NULL;
46         char       *host = NULL;
47         char       *port = NULL;
48         char       *username = NULL;
49         bool            password = false;
50         bool            echo = false;
51         bool            quiet = false;
52         char       *owner = NULL;
53         char       *location = NULL;
54         char       *template = NULL;
55         char       *encoding = NULL;
56
57         PQExpBufferData sql;
58
59         PGconn     *conn;
60         PGresult   *result;
61
62         progname = get_progname(argv[0]);
63         set_pglocale(argv[0], "pgscripts");
64
65         handle_help_version_opts(argc, argv, "createdb", help);
66
67         while ((c = getopt_long(argc, argv, "h:p:U:WeqO:D:T:E:", long_options, &optindex)) != -1)
68         {
69                 switch (c)
70                 {
71                         case 'h':
72                                 host = optarg;
73                                 break;
74                         case 'p':
75                                 port = optarg;
76                                 break;
77                         case 'U':
78                                 username = optarg;
79                                 break;
80                         case 'W':
81                                 password = true;
82                                 break;
83                         case 'e':
84                                 echo = true;
85                                 break;
86                         case 'q':
87                                 quiet = true;
88                                 break;
89                         case 'O':
90                                 owner = optarg;
91                                 break;
92                         case 'D':
93                                 location = optarg;
94                                 break;
95                         case 'T':
96                                 template = optarg;
97                                 break;
98                         case 'E':
99                                 encoding = optarg;
100                                 break;
101                         default:
102                                 fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
103                                 exit(1);
104                 }
105         }
106
107         switch (argc - optind)
108         {
109                 case 0:
110                         break;
111                 case 1:
112                         dbname = argv[optind];
113                         break;
114                 case 2:
115                         dbname = argv[optind];
116                         comment = argv[optind + 1];
117                         break;
118                 default:
119                         fprintf(stderr, _("%s: too many command-line arguments (first is \"%s\")\n"),
120                                         progname, argv[optind + 2]);
121                         fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
122                         exit(1);
123         }
124
125         if (encoding)
126         {
127                 if (pg_char_to_encoding(encoding) < 0)
128                 {
129                         fprintf(stderr, _("%s: \"%s\" is not a valid encoding name\n"),
130                                         progname, encoding);
131                         exit(1);
132                 }
133         }
134
135         if (dbname == NULL)
136         {
137                 if (getenv("PGDATABASE"))
138                         dbname = getenv("PGDATABASE");
139                 else if (getenv("PGUSER"))
140                         dbname = getenv("PGUSER");
141                 else
142                         dbname = get_user_name(progname);
143         }
144
145         initPQExpBuffer(&sql);
146
147         appendPQExpBuffer(&sql, "CREATE DATABASE %s",
148                                           fmtId(dbname));
149
150         if (owner)
151                 appendPQExpBuffer(&sql, " OWNER %s", fmtId(owner));
152         if (location)
153         {
154                 appendPQExpBuffer(&sql, " LOCATION ");
155                 appendStringLiteral(&sql, location, false);
156         }
157         if (encoding)
158                 appendPQExpBuffer(&sql, " ENCODING '%s'", encoding);
159         if (template)
160                 appendPQExpBuffer(&sql, " TEMPLATE %s", fmtId(template));
161         appendPQExpBuffer(&sql, ";\n");
162
163         conn = connectDatabase("template1", host, port, username, password, progname);
164
165         if (echo)
166                 printf("%s", sql.data);
167         result = PQexec(conn, sql.data);
168
169         if (PQresultStatus(result) != PGRES_COMMAND_OK)
170         {
171                 fprintf(stderr, _("%s: database creation failed: %s"),
172                                 progname, PQerrorMessage(conn));
173                 PQfinish(conn);
174                 exit(1);
175         }
176
177         PQclear(result);
178         PQfinish(conn);
179
180         if (!quiet)
181         {
182                 puts("CREATE DATABASE");
183                 fflush(stdout);
184         }
185
186         if (comment)
187         {
188                 printfPQExpBuffer(&sql, "COMMENT ON DATABASE %s IS ", fmtId(dbname));
189                 appendStringLiteral(&sql, comment, false);
190                 appendPQExpBuffer(&sql, ";\n");
191
192                 conn = connectDatabase(dbname, host, port, username, password, progname);
193                 if (echo)
194                         printf("%s", sql.data);
195                 result = PQexec(conn, sql.data);
196
197                 if (PQresultStatus(result) != PGRES_COMMAND_OK)
198                 {
199                         fprintf(stderr, _("%s: comment creation failed (database was created): %s"),
200                                         progname, PQerrorMessage(conn));
201                         PQfinish(conn);
202                         exit(1);
203                 }
204
205                 PQfinish(conn);
206                 if (!quiet)
207                 {
208                         puts("COMMENT");
209                         fflush(stdout);
210                 }
211         }
212
213         exit(0);
214 }
215
216
217 static void
218 help(const char *progname)
219 {
220         printf(_("%s creates a PostgreSQL database.\n\n"), progname);
221         printf(_("Usage:\n"));
222         printf(_("  %s [OPTION]... [DBNAME] [DESCRIPTION]\n"), progname);
223         printf(_("\nOptions:\n"));
224         printf(_("  -D, --location=PATH       alternative place to store the database\n"));
225         printf(_("  -E, --encoding=ENCODING   encoding for the database\n"));
226         printf(_("  -O, --owner=OWNER         database user to own the new database\n"));
227         printf(_("  -T, --template=TEMPLATE   template database to copy\n"));
228         printf(_("  -e, --echo                show the commands being sent to the server\n"));
229         printf(_("  -q, --quiet               don't write any messages\n"));
230         printf(_("  --help                    show this help, then exit\n"));
231         printf(_("  --version                 output version information, then exit\n"));
232         printf(_("\nConnection options:\n"));
233         printf(_("  -h, --host=HOSTNAME       database server host or socket directory\n"));
234         printf(_("  -p, --port=PORT           database server port\n"));
235         printf(_("  -U, --username=USERNAME   user name to connect as\n"));
236         printf(_("  -W, --password            prompt for password\n"));
237         printf(_("\nBy default, a database with the same name as the current user is created.\n"));
238         printf(_("\nReport bugs to <pgsql-bugs@postgresql.org>.\n"));
239 }