OSDN Git Service

9b72eac79b7c3dc8123b718bdb1faae00d1a54b0
[pg-rex/syncrep.git] / src / bin / scripts / createdb.c
1 /*-------------------------------------------------------------------------
2  *
3  * createdb
4  *
5  * Portions Copyright (c) 1996-2011, 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 "dumputils.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                 {NULL, 0, NULL, 0}
39         };
40
41         const char *progname;
42         int                     optindex;
43         int                     c;
44
45         const char *dbname = NULL;
46         char       *comment = NULL;
47         char       *host = NULL;
48         char       *port = NULL;
49         char       *username = NULL;
50         enum trivalue prompt_password = TRI_DEFAULT;
51         bool            echo = false;
52         char       *owner = NULL;
53         char       *tablespace = NULL;
54         char       *template = NULL;
55         char       *encoding = NULL;
56         char       *lc_collate = NULL;
57         char       *lc_ctype = NULL;
58         char       *locale = NULL;
59
60         PQExpBufferData sql;
61
62         PGconn     *conn;
63         PGresult   *result;
64
65         progname = get_progname(argv[0]);
66         set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pgscripts"));
67
68         handle_help_version_opts(argc, argv, "createdb", help);
69
70         while ((c = getopt_long(argc, argv, "h:p:U:wWeO:D:T:E:l:", long_options, &optindex)) != -1)
71         {
72                 switch (c)
73                 {
74                         case 'h':
75                                 host = optarg;
76                                 break;
77                         case 'p':
78                                 port = optarg;
79                                 break;
80                         case 'U':
81                                 username = optarg;
82                                 break;
83                         case 'w':
84                                 prompt_password = TRI_NO;
85                                 break;
86                         case 'W':
87                                 prompt_password = TRI_YES;
88                                 break;
89                         case 'e':
90                                 echo = true;
91                                 break;
92                         case 'O':
93                                 owner = optarg;
94                                 break;
95                         case 'D':
96                                 tablespace = optarg;
97                                 break;
98                         case 'T':
99                                 template = optarg;
100                                 break;
101                         case 'E':
102                                 encoding = optarg;
103                                 break;
104                         case 1:
105                                 lc_collate = optarg;
106                                 break;
107                         case 2:
108                                 lc_ctype = optarg;
109                                 break;
110                         case 'l':
111                                 locale = optarg;
112                                 break;
113                         default:
114                                 fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
115                                 exit(1);
116                 }
117         }
118
119         switch (argc - optind)
120         {
121                 case 0:
122                         break;
123                 case 1:
124                         dbname = argv[optind];
125                         break;
126                 case 2:
127                         dbname = argv[optind];
128                         comment = argv[optind + 1];
129                         break;
130                 default:
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);
134                         exit(1);
135         }
136
137         if (locale)
138         {
139                 if (lc_ctype)
140                 {
141                         fprintf(stderr, _("%s: only one of --locale and --lc-ctype can be specified\n"),
142                                         progname);
143                         exit(1);
144                 }
145                 if (lc_collate)
146                 {
147                         fprintf(stderr, _("%s: only one of --locale and --lc-collate can be specified\n"),
148                                         progname);
149                         exit(1);
150                 }
151                 lc_ctype = locale;
152                 lc_collate = locale;
153         }
154
155         if (encoding)
156         {
157                 if (pg_char_to_encoding(encoding) < 0)
158                 {
159                         fprintf(stderr, _("%s: \"%s\" is not a valid encoding name\n"),
160                                         progname, encoding);
161                         exit(1);
162                 }
163         }
164
165         if (dbname == NULL)
166         {
167                 if (getenv("PGDATABASE"))
168                         dbname = getenv("PGDATABASE");
169                 else if (getenv("PGUSER"))
170                         dbname = getenv("PGUSER");
171                 else
172                         dbname = get_user_name(progname);
173         }
174
175         initPQExpBuffer(&sql);
176
177         appendPQExpBuffer(&sql, "CREATE DATABASE %s",
178                                           fmtId(dbname));
179
180         if (owner)
181                 appendPQExpBuffer(&sql, " OWNER %s", fmtId(owner));
182         if (tablespace)
183                 appendPQExpBuffer(&sql, " TABLESPACE %s", fmtId(tablespace));
184         if (encoding)
185                 appendPQExpBuffer(&sql, " ENCODING '%s'", encoding);
186         if (template)
187                 appendPQExpBuffer(&sql, " TEMPLATE %s", fmtId(template));
188         if (lc_collate)
189                 appendPQExpBuffer(&sql, " LC_COLLATE '%s'", lc_collate);
190         if (lc_ctype)
191                 appendPQExpBuffer(&sql, " LC_CTYPE '%s'", lc_ctype);
192
193         appendPQExpBuffer(&sql, ";\n");
194
195         conn = connectDatabase(strcmp(dbname, "postgres") == 0 ? "template1" : "postgres",
196                                                    host, port, username, prompt_password, progname);
197
198         if (echo)
199                 printf("%s", sql.data);
200         result = PQexec(conn, sql.data);
201
202         if (PQresultStatus(result) != PGRES_COMMAND_OK)
203         {
204                 fprintf(stderr, _("%s: database creation failed: %s"),
205                                 progname, PQerrorMessage(conn));
206                 PQfinish(conn);
207                 exit(1);
208         }
209
210         PQclear(result);
211         PQfinish(conn);
212
213         if (comment)
214         {
215                 conn = connectDatabase(dbname, host, port, username, prompt_password, progname);
216
217                 printfPQExpBuffer(&sql, "COMMENT ON DATABASE %s IS ", fmtId(dbname));
218                 appendStringLiteralConn(&sql, comment, conn);
219                 appendPQExpBuffer(&sql, ";\n");
220
221                 if (echo)
222                         printf("%s", sql.data);
223                 result = PQexec(conn, sql.data);
224
225                 if (PQresultStatus(result) != PGRES_COMMAND_OK)
226                 {
227                         fprintf(stderr, _("%s: comment creation failed (database was created): %s"),
228                                         progname, PQerrorMessage(conn));
229                         PQfinish(conn);
230                         exit(1);
231                 }
232
233                 PQclear(result);
234                 PQfinish(conn);
235         }
236
237         exit(0);
238 }
239
240
241 static void
242 help(const char *progname)
243 {
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"));
266 }