Header And Logo

PostgreSQL
| The world's most advanced open source database.

createdb.c

Go to the documentation of this file.
00001 /*-------------------------------------------------------------------------
00002  *
00003  * createdb
00004  *
00005  * Portions Copyright (c) 1996-2013, PostgreSQL Global Development Group
00006  * Portions Copyright (c) 1994, Regents of the University of California
00007  *
00008  * src/bin/scripts/createdb.c
00009  *
00010  *-------------------------------------------------------------------------
00011  */
00012 #include "postgres_fe.h"
00013 
00014 #include "common.h"
00015 #include "dumputils.h"
00016 
00017 
00018 static void help(const char *progname);
00019 
00020 
00021 int
00022 main(int argc, char *argv[])
00023 {
00024     static struct option long_options[] = {
00025         {"host", required_argument, NULL, 'h'},
00026         {"port", required_argument, NULL, 'p'},
00027         {"username", required_argument, NULL, 'U'},
00028         {"no-password", no_argument, NULL, 'w'},
00029         {"password", no_argument, NULL, 'W'},
00030         {"echo", no_argument, NULL, 'e'},
00031         {"owner", required_argument, NULL, 'O'},
00032         {"tablespace", required_argument, NULL, 'D'},
00033         {"template", required_argument, NULL, 'T'},
00034         {"encoding", required_argument, NULL, 'E'},
00035         {"lc-collate", required_argument, NULL, 1},
00036         {"lc-ctype", required_argument, NULL, 2},
00037         {"locale", required_argument, NULL, 'l'},
00038         {"maintenance-db", required_argument, NULL, 3},
00039         {NULL, 0, NULL, 0}
00040     };
00041 
00042     const char *progname;
00043     int         optindex;
00044     int         c;
00045 
00046     const char *dbname = NULL;
00047     const char *maintenance_db = NULL;
00048     char       *comment = NULL;
00049     char       *host = NULL;
00050     char       *port = NULL;
00051     char       *username = NULL;
00052     enum trivalue prompt_password = TRI_DEFAULT;
00053     bool        echo = false;
00054     char       *owner = NULL;
00055     char       *tablespace = NULL;
00056     char       *template = NULL;
00057     char       *encoding = NULL;
00058     char       *lc_collate = NULL;
00059     char       *lc_ctype = NULL;
00060     char       *locale = NULL;
00061 
00062     PQExpBufferData sql;
00063 
00064     PGconn     *conn;
00065     PGresult   *result;
00066 
00067     progname = get_progname(argv[0]);
00068     set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pgscripts"));
00069 
00070     handle_help_version_opts(argc, argv, "createdb", help);
00071 
00072     while ((c = getopt_long(argc, argv, "h:p:U:wWeO:D:T:E:l:", long_options, &optindex)) != -1)
00073     {
00074         switch (c)
00075         {
00076             case 'h':
00077                 host = pg_strdup(optarg);
00078                 break;
00079             case 'p':
00080                 port = pg_strdup(optarg);
00081                 break;
00082             case 'U':
00083                 username = pg_strdup(optarg);
00084                 break;
00085             case 'w':
00086                 prompt_password = TRI_NO;
00087                 break;
00088             case 'W':
00089                 prompt_password = TRI_YES;
00090                 break;
00091             case 'e':
00092                 echo = true;
00093                 break;
00094             case 'O':
00095                 owner = pg_strdup(optarg);
00096                 break;
00097             case 'D':
00098                 tablespace = pg_strdup(optarg);
00099                 break;
00100             case 'T':
00101                 template = pg_strdup(optarg);
00102                 break;
00103             case 'E':
00104                 encoding = pg_strdup(optarg);
00105                 break;
00106             case 1:
00107                 lc_collate = pg_strdup(optarg);
00108                 break;
00109             case 2:
00110                 lc_ctype = pg_strdup(optarg);
00111                 break;
00112             case 'l':
00113                 locale = pg_strdup(optarg);
00114                 break;
00115             case 3:
00116                 maintenance_db = pg_strdup(optarg);
00117                 break;
00118             default:
00119                 fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
00120                 exit(1);
00121         }
00122     }
00123 
00124     switch (argc - optind)
00125     {
00126         case 0:
00127             break;
00128         case 1:
00129             dbname = argv[optind];
00130             break;
00131         case 2:
00132             dbname = argv[optind];
00133             comment = argv[optind + 1];
00134             break;
00135         default:
00136             fprintf(stderr, _("%s: too many command-line arguments (first is \"%s\")\n"),
00137                     progname, argv[optind + 2]);
00138             fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
00139             exit(1);
00140     }
00141 
00142     if (locale)
00143     {
00144         if (lc_ctype)
00145         {
00146             fprintf(stderr, _("%s: only one of --locale and --lc-ctype can be specified\n"),
00147                     progname);
00148             exit(1);
00149         }
00150         if (lc_collate)
00151         {
00152             fprintf(stderr, _("%s: only one of --locale and --lc-collate can be specified\n"),
00153                     progname);
00154             exit(1);
00155         }
00156         lc_ctype = locale;
00157         lc_collate = locale;
00158     }
00159 
00160     if (encoding)
00161     {
00162         if (pg_char_to_encoding(encoding) < 0)
00163         {
00164             fprintf(stderr, _("%s: \"%s\" is not a valid encoding name\n"),
00165                     progname, encoding);
00166             exit(1);
00167         }
00168     }
00169 
00170     if (dbname == NULL)
00171     {
00172         if (getenv("PGDATABASE"))
00173             dbname = getenv("PGDATABASE");
00174         else if (getenv("PGUSER"))
00175             dbname = getenv("PGUSER");
00176         else
00177             dbname = get_user_name(progname);
00178     }
00179 
00180     initPQExpBuffer(&sql);
00181 
00182     appendPQExpBuffer(&sql, "CREATE DATABASE %s",
00183                       fmtId(dbname));
00184 
00185     if (owner)
00186         appendPQExpBuffer(&sql, " OWNER %s", fmtId(owner));
00187     if (tablespace)
00188         appendPQExpBuffer(&sql, " TABLESPACE %s", fmtId(tablespace));
00189     if (encoding)
00190         appendPQExpBuffer(&sql, " ENCODING '%s'", encoding);
00191     if (template)
00192         appendPQExpBuffer(&sql, " TEMPLATE %s", fmtId(template));
00193     if (lc_collate)
00194         appendPQExpBuffer(&sql, " LC_COLLATE '%s'", lc_collate);
00195     if (lc_ctype)
00196         appendPQExpBuffer(&sql, " LC_CTYPE '%s'", lc_ctype);
00197 
00198     appendPQExpBuffer(&sql, ";\n");
00199 
00200     /* No point in trying to use postgres db when creating postgres db. */
00201     if (maintenance_db == NULL && strcmp(dbname, "postgres") == 0)
00202         maintenance_db = "template1";
00203 
00204     conn = connectMaintenanceDatabase(maintenance_db, host, port, username,
00205                                       prompt_password, progname);
00206 
00207     if (echo)
00208         printf("%s", sql.data);
00209     result = PQexec(conn, sql.data);
00210 
00211     if (PQresultStatus(result) != PGRES_COMMAND_OK)
00212     {
00213         fprintf(stderr, _("%s: database creation failed: %s"),
00214                 progname, PQerrorMessage(conn));
00215         PQfinish(conn);
00216         exit(1);
00217     }
00218 
00219     PQclear(result);
00220 
00221     if (comment)
00222     {
00223         printfPQExpBuffer(&sql, "COMMENT ON DATABASE %s IS ", fmtId(dbname));
00224         appendStringLiteralConn(&sql, comment, conn);
00225         appendPQExpBuffer(&sql, ";\n");
00226 
00227         if (echo)
00228             printf("%s", sql.data);
00229         result = PQexec(conn, sql.data);
00230 
00231         if (PQresultStatus(result) != PGRES_COMMAND_OK)
00232         {
00233             fprintf(stderr, _("%s: comment creation failed (database was created): %s"),
00234                     progname, PQerrorMessage(conn));
00235             PQfinish(conn);
00236             exit(1);
00237         }
00238 
00239         PQclear(result);
00240     }
00241 
00242     PQfinish(conn);
00243 
00244     exit(0);
00245 }
00246 
00247 
00248 static void
00249 help(const char *progname)
00250 {
00251     printf(_("%s creates a PostgreSQL database.\n\n"), progname);
00252     printf(_("Usage:\n"));
00253     printf(_("  %s [OPTION]... [DBNAME] [DESCRIPTION]\n"), progname);
00254     printf(_("\nOptions:\n"));
00255     printf(_("  -D, --tablespace=TABLESPACE  default tablespace for the database\n"));
00256     printf(_("  -e, --echo                   show the commands being sent to the server\n"));
00257     printf(_("  -E, --encoding=ENCODING      encoding for the database\n"));
00258     printf(_("  -l, --locale=LOCALE          locale settings for the database\n"));
00259     printf(_("      --lc-collate=LOCALE      LC_COLLATE setting for the database\n"));
00260     printf(_("      --lc-ctype=LOCALE        LC_CTYPE setting for the database\n"));
00261     printf(_("  -O, --owner=OWNER            database user to own the new database\n"));
00262     printf(_("  -T, --template=TEMPLATE      template database to copy\n"));
00263     printf(_("  -V, --version                output version information, then exit\n"));
00264     printf(_("  -?, --help                   show this help, then exit\n"));
00265     printf(_("\nConnection options:\n"));
00266     printf(_("  -h, --host=HOSTNAME          database server host or socket directory\n"));
00267     printf(_("  -p, --port=PORT              database server port\n"));
00268     printf(_("  -U, --username=USERNAME      user name to connect as\n"));
00269     printf(_("  -w, --no-password            never prompt for password\n"));
00270     printf(_("  -W, --password               force password prompt\n"));
00271     printf(_("  --maintenance-db=DBNAME      alternate maintenance database\n"));
00272     printf(_("\nBy default, a database with the same name as the current user is created.\n"));
00273     printf(_("\nReport bugs to <[email protected]>.\n"));
00274 }