]> granicus.if.org Git - postgresql/blob - src/bin/scripts/createdb.c
372ee922aa26108bda749ce4d0b80700739365da
[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  * $Header: /cvsroot/pgsql/src/bin/scripts/createdb.c,v 1.2 2003/05/14 03:26:03 tgl 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         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         init_nls();
64         handle_help_version_opts(argc, argv, "createdb", help);
65
66         while ((c = getopt_long(argc, argv, "h:p:U:WeqO:D:T:E:", long_options, &optindex)) != -1)
67         {
68                 switch (c)
69                 {
70                         case 'h':
71                                 host = optarg;
72                                 break;
73                         case 'p':
74                                 port = optarg;
75                                 break;
76                         case 'U':
77                                 username = optarg;
78                                 break;
79                         case 'W':
80                                 password = true;
81                                 break;
82                         case 'e':
83                                 echo = true;
84                                 break;
85                         case 'q':
86                                 quiet = true;
87                                 break;
88                         case 'O':
89                                 owner = optarg;
90                                 break;
91                         case 'D':
92                                 location = optarg;
93                                 break;
94                         case 'T':
95                                 template = optarg;
96                                 break;
97                         case 'E':
98                                 encoding = optarg;
99                                 break;
100                         default:
101                                 fprintf(stderr, _("Try '%s --help' for more information.\n"), progname);
102                                 exit(1);
103                 }
104         }
105
106         switch (argc - optind)
107         {
108                 case 0:
109                         break;
110                 case 1:
111                         dbname = argv[optind];
112                         break;
113                 case 2:
114                         dbname = argv[optind];
115                         comment = argv[optind + 1];
116                         break;
117                 default:
118                         fprintf(stderr, _("%s: too many command-line arguments (first is '%s')\n"),
119                                         progname, argv[optind + 2]);
120                         fprintf(stderr, _("Try '%s --help' for more information.\n"), progname);
121                         exit(1);
122         }
123
124         if (encoding)
125         {
126                 if (pg_char_to_encoding(encoding) < 0)
127                 {
128                         fprintf(stderr, _("%s: \"%s\" is not a valid encoding name\n"),
129                                         progname, encoding);
130                         exit(1);
131                 }
132         }
133
134         if (dbname == NULL)
135         {
136                 if (getenv("PGDATABASE"))
137                         dbname = getenv("PGDATABASE");
138                 else if (getenv("PGUSER"))
139                         dbname = getenv("PGUSER");
140                 else
141                         dbname = get_user_name(progname);
142         }
143
144         initPQExpBuffer(&sql);
145
146         appendPQExpBuffer(&sql, "CREATE DATABASE %s",
147                                           fmtId(dbname));
148
149         if (owner)
150                 appendPQExpBuffer(&sql, " OWNER %s", fmtId(owner));
151         if (location)
152         {
153                 appendPQExpBuffer(&sql, " LOCATION ");
154                 appendStringLiteral(&sql, location, false);
155         }
156         if (encoding)
157                 appendPQExpBuffer(&sql, " ENCODING '%s'", encoding);
158         if (template)
159                 appendPQExpBuffer(&sql, " TEMPLATE %s", fmtId(template));
160         appendPQExpBuffer(&sql, ";\n");
161
162         conn = connectDatabase("template1", host, port, username, password, progname);
163
164         if (echo)
165                 printf("%s", sql.data);
166         result = PQexec(conn, sql.data);
167
168         if (PQresultStatus(result) != PGRES_COMMAND_OK)
169         {
170                 fprintf(stderr, _("%s: database creation failed: %s"),
171                                 progname, PQerrorMessage(conn));
172                 PQfinish(conn);
173                 exit(1);
174         }
175
176         PQclear(result);
177         PQfinish(conn);
178
179         if (!quiet)
180                 puts("CREATE DATABASE");
181
182         if (comment)
183         {
184                 printfPQExpBuffer(&sql, "COMMENT ON DATABASE %s IS ", fmtId(dbname));
185                 appendStringLiteral(&sql, comment, false);
186                 appendPQExpBuffer(&sql, ";\n");
187
188                 conn = connectDatabase(dbname, host, port, username, password, progname);
189                 if (echo)
190                         printf("%s", sql.data);
191                 result = PQexec(conn, sql.data);
192
193                 if (PQresultStatus(result) != PGRES_COMMAND_OK)
194                 {
195                         fprintf(stderr, _("%s: comment creation failed (database was created): %s"),
196                                         progname, PQerrorMessage(conn));
197                         PQfinish(conn);
198                         exit(1);
199                 }
200
201                 PQfinish(conn);
202                 if (!quiet)
203                         puts("COMMENT");
204         }
205
206         exit(0);
207 }
208
209
210 static void
211 help(const char *progname)
212 {
213         printf(_("%s creates a PostgreSQL database.\n\n"), progname);
214         printf(_("Usage:\n"));
215         printf(_("  %s [OPTION]... [DBNAME] [DESCRIPTION]\n"), progname);
216         printf(_("\nOptions:\n"));
217         printf(_("  -D, --location=PATH       alternative place to store the database\n"));
218         printf(_("  -E, --encoding=ENCODING   encoding for the database\n"));
219         printf(_("  -O, --owner=OWNER         database user to own the new database\n"));
220         printf(_("  -T, --template=TEMPLATE   template database to copy\n"));
221         printf(_("  -e, --echo                show the commands being sent to the server\n"));
222         printf(_("  -q, --quiet               don't write any messages\n"));
223         printf(_("  --help                    show this help, then exit\n"));
224         printf(_("  --version                 output version information, then exit\n"));
225         printf(_("\nConnection options:\n"));
226         printf(_("  -h, --host=HOSTNAME       database server host\n"));
227         printf(_("  -p, --port=PORT           database server port\n"));
228         printf(_("  -U, --username=USERNAME   user name to connect as\n"));
229         printf(_("  -W, --password            prompt for password\n"));
230         printf(_("\nBy default, a database with the same name as the current user is created.\n"));
231         printf(_("\nReport bugs to <pgsql-bugs@postgresql.org>.\n"));
232 }