~vcs-imports/mammoth-replicator/trunk

« back to all changes in this revision

Viewing changes to src/bin/scripts/createdb.c

  • Committer: alvherre
  • Date: 2005-12-16 21:24:52 UTC
  • Revision ID: svn-v4:db760fc0-0f08-0410-9d63-cc6633f64896:trunk:1
Initial import of the REL8_0_3 sources from the Pgsql CVS repository.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*-------------------------------------------------------------------------
 
2
 *
 
3
 * createdb
 
4
 *
 
5
 * Portions Copyright (c) 1996-2005, 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.14 2004-12-31 22:03:17 pgsql 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
                {"tablespace", 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       *tablespace = 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_pgservice(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
                                tablespace = 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 (tablespace)
 
153
                appendPQExpBuffer(&sql, " TABLESPACE %s", fmtId(tablespace));
 
154
        if (encoding)
 
155
                appendPQExpBuffer(&sql, " ENCODING '%s'", encoding);
 
156
        if (template)
 
157
                appendPQExpBuffer(&sql, " TEMPLATE %s", fmtId(template));
 
158
        appendPQExpBuffer(&sql, ";\n");
 
159
 
 
160
        conn = connectDatabase("template1", host, port, username, password, progname);
 
161
 
 
162
        if (echo)
 
163
                printf("%s", sql.data);
 
164
        result = PQexec(conn, sql.data);
 
165
 
 
166
        if (PQresultStatus(result) != PGRES_COMMAND_OK)
 
167
        {
 
168
                fprintf(stderr, _("%s: database creation failed: %s"),
 
169
                                progname, PQerrorMessage(conn));
 
170
                PQfinish(conn);
 
171
                exit(1);
 
172
        }
 
173
 
 
174
        PQclear(result);
 
175
        PQfinish(conn);
 
176
 
 
177
        if (!quiet)
 
178
        {
 
179
                puts("CREATE DATABASE");
 
180
                fflush(stdout);
 
181
        }
 
182
 
 
183
        if (comment)
 
184
        {
 
185
                printfPQExpBuffer(&sql, "COMMENT ON DATABASE %s IS ", fmtId(dbname));
 
186
                appendStringLiteral(&sql, comment, false);
 
187
                appendPQExpBuffer(&sql, ";\n");
 
188
 
 
189
                conn = connectDatabase(dbname, host, port, username, password, progname);
 
190
                if (echo)
 
191
                        printf("%s", sql.data);
 
192
                result = PQexec(conn, sql.data);
 
193
 
 
194
                if (PQresultStatus(result) != PGRES_COMMAND_OK)
 
195
                {
 
196
                        fprintf(stderr, _("%s: comment creation failed (database was created): %s"),
 
197
                                        progname, PQerrorMessage(conn));
 
198
                        PQfinish(conn);
 
199
                        exit(1);
 
200
                }
 
201
 
 
202
                PQfinish(conn);
 
203
                if (!quiet)
 
204
                {
 
205
                        puts("COMMENT");
 
206
                        fflush(stdout);
 
207
                }
 
208
        }
 
209
 
 
210
        exit(0);
 
211
}
 
212
 
 
213
 
 
214
static void
 
215
help(const char *progname)
 
216
{
 
217
        printf(_("%s creates a PostgreSQL database.\n\n"), progname);
 
218
        printf(_("Usage:\n"));
 
219
        printf(_("  %s [OPTION]... [DBNAME] [DESCRIPTION]\n"), progname);
 
220
        printf(_("\nOptions:\n"));
 
221
        printf(_("  -D, --tablespace=TABLESPACE  default tablespace for the database\n"));
 
222
        printf(_("  -E, --encoding=ENCODING      encoding for the database\n"));
 
223
        printf(_("  -O, --owner=OWNER            database user to own the new database\n"));
 
224
        printf(_("  -T, --template=TEMPLATE      template database to copy\n"));
 
225
        printf(_("  -e, --echo                   show the commands being sent to the server\n"));
 
226
        printf(_("  -q, --quiet                  don't write any messages\n"));
 
227
        printf(_("  --help                       show this help, then exit\n"));
 
228
        printf(_("  --version                    output version information, then exit\n"));
 
229
        printf(_("\nConnection options:\n"));
 
230
        printf(_("  -h, --host=HOSTNAME          database server host or socket directory\n"));
 
231
        printf(_("  -p, --port=PORT              database server port\n"));
 
232
        printf(_("  -U, --username=USERNAME      user name to connect as\n"));
 
233
        printf(_("  -W, --password               prompt for password\n"));
 
234
        printf(_("\nBy default, a database with the same name as the current user is created.\n"));
 
235
        printf(_("\nReport bugs to <pgsql-bugs@postgresql.org>.\n"));
 
236
}