LCOV - code coverage report
Current view: top level - src/bin/scripts - createdb.c (source / functions) Hit Total Coverage
Test: PostgreSQL 12beta2 Lines: 91 146 62.3 %
Date: 2019-06-18 07:06:57 Functions: 2 2 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*-------------------------------------------------------------------------
       2             :  *
       3             :  * createdb
       4             :  *
       5             :  * Portions Copyright (c) 1996-2019, 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 "common/logging.h"
      16             : #include "fe_utils/string_utils.h"
      17             : 
      18             : 
      19             : static void help(const char *progname);
      20             : 
      21             : 
      22             : int
      23          42 : main(int argc, char *argv[])
      24             : {
      25             :     static struct option long_options[] = {
      26             :         {"host", required_argument, NULL, 'h'},
      27             :         {"port", required_argument, NULL, 'p'},
      28             :         {"username", required_argument, NULL, 'U'},
      29             :         {"no-password", no_argument, NULL, 'w'},
      30             :         {"password", no_argument, NULL, 'W'},
      31             :         {"echo", no_argument, NULL, 'e'},
      32             :         {"owner", required_argument, NULL, 'O'},
      33             :         {"tablespace", required_argument, NULL, 'D'},
      34             :         {"template", required_argument, NULL, 'T'},
      35             :         {"encoding", required_argument, NULL, 'E'},
      36             :         {"lc-collate", required_argument, NULL, 1},
      37             :         {"lc-ctype", required_argument, NULL, 2},
      38             :         {"locale", required_argument, NULL, 'l'},
      39             :         {"maintenance-db", required_argument, NULL, 3},
      40             :         {NULL, 0, NULL, 0}
      41             :     };
      42             : 
      43             :     const char *progname;
      44             :     int         optindex;
      45             :     int         c;
      46             : 
      47          42 :     const char *dbname = NULL;
      48          42 :     const char *maintenance_db = NULL;
      49          42 :     char       *comment = NULL;
      50          42 :     char       *host = NULL;
      51          42 :     char       *port = NULL;
      52          42 :     char       *username = NULL;
      53          42 :     enum trivalue prompt_password = TRI_DEFAULT;
      54          42 :     bool        echo = false;
      55          42 :     char       *owner = NULL;
      56          42 :     char       *tablespace = NULL;
      57          42 :     char       *template = NULL;
      58          42 :     char       *encoding = NULL;
      59          42 :     char       *lc_collate = NULL;
      60          42 :     char       *lc_ctype = NULL;
      61          42 :     char       *locale = NULL;
      62             : 
      63             :     PQExpBufferData sql;
      64             : 
      65             :     PGconn     *conn;
      66             :     PGresult   *result;
      67             : 
      68          42 :     pg_logging_init(argv[0]);
      69          42 :     progname = get_progname(argv[0]);
      70          42 :     set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pgscripts"));
      71             : 
      72          42 :     handle_help_version_opts(argc, argv, "createdb", help);
      73             : 
      74          38 :     while ((c = getopt_long(argc, argv, "h:p:U:wWeO:D:T:E:l:", long_options, &optindex)) != -1)
      75             :     {
      76           8 :         switch (c)
      77             :         {
      78             :             case 'h':
      79           0 :                 host = pg_strdup(optarg);
      80           0 :                 break;
      81             :             case 'p':
      82           0 :                 port = pg_strdup(optarg);
      83           0 :                 break;
      84             :             case 'U':
      85           0 :                 username = pg_strdup(optarg);
      86           0 :                 break;
      87             :             case 'w':
      88           0 :                 prompt_password = TRI_NO;
      89           0 :                 break;
      90             :             case 'W':
      91           0 :                 prompt_password = TRI_YES;
      92           0 :                 break;
      93             :             case 'e':
      94           0 :                 echo = true;
      95           0 :                 break;
      96             :             case 'O':
      97           0 :                 owner = pg_strdup(optarg);
      98           0 :                 break;
      99             :             case 'D':
     100           0 :                 tablespace = pg_strdup(optarg);
     101           0 :                 break;
     102             :             case 'T':
     103           2 :                 template = pg_strdup(optarg);
     104           2 :                 break;
     105             :             case 'E':
     106           2 :                 encoding = pg_strdup(optarg);
     107           2 :                 break;
     108             :             case 1:
     109           0 :                 lc_collate = pg_strdup(optarg);
     110           0 :                 break;
     111             :             case 2:
     112           0 :                 lc_ctype = pg_strdup(optarg);
     113           0 :                 break;
     114             :             case 'l':
     115           2 :                 locale = pg_strdup(optarg);
     116           2 :                 break;
     117             :             case 3:
     118           0 :                 maintenance_db = pg_strdup(optarg);
     119           0 :                 break;
     120             :             default:
     121           2 :                 fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
     122           2 :                 exit(1);
     123             :         }
     124             :     }
     125             : 
     126          36 :     switch (argc - optind)
     127             :     {
     128             :         case 0:
     129           0 :             break;
     130             :         case 1:
     131          36 :             dbname = argv[optind];
     132          36 :             break;
     133             :         case 2:
     134           0 :             dbname = argv[optind];
     135           0 :             comment = argv[optind + 1];
     136           0 :             break;
     137             :         default:
     138           0 :             pg_log_error("too many command-line arguments (first is \"%s\")",
     139             :                          argv[optind + 2]);
     140           0 :             fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
     141           0 :             exit(1);
     142             :     }
     143             : 
     144          36 :     if (locale)
     145             :     {
     146           2 :         if (lc_ctype)
     147             :         {
     148           0 :             pg_log_error("only one of --locale and --lc-ctype can be specified");
     149           0 :             exit(1);
     150             :         }
     151           2 :         if (lc_collate)
     152             :         {
     153           0 :             pg_log_error("only one of --locale and --lc-collate can be specified");
     154           0 :             exit(1);
     155             :         }
     156           2 :         lc_ctype = locale;
     157           2 :         lc_collate = locale;
     158             :     }
     159             : 
     160          36 :     if (encoding)
     161             :     {
     162           2 :         if (pg_char_to_encoding(encoding) < 0)
     163             :         {
     164           0 :             pg_log_error("\"%s\" is not a valid encoding name", encoding);
     165           0 :             exit(1);
     166             :         }
     167             :     }
     168             : 
     169          36 :     if (dbname == NULL)
     170             :     {
     171           0 :         if (getenv("PGDATABASE"))
     172           0 :             dbname = getenv("PGDATABASE");
     173           0 :         else if (getenv("PGUSER"))
     174           0 :             dbname = getenv("PGUSER");
     175             :         else
     176           0 :             dbname = get_user_name_or_exit(progname);
     177             :     }
     178             : 
     179          36 :     initPQExpBuffer(&sql);
     180             : 
     181          36 :     appendPQExpBuffer(&sql, "CREATE DATABASE %s",
     182             :                       fmtId(dbname));
     183             : 
     184          36 :     if (owner)
     185           0 :         appendPQExpBuffer(&sql, " OWNER %s", fmtId(owner));
     186          36 :     if (tablespace)
     187           0 :         appendPQExpBuffer(&sql, " TABLESPACE %s", fmtId(tablespace));
     188          36 :     if (encoding)
     189           2 :         appendPQExpBuffer(&sql, " ENCODING '%s'", encoding);
     190          36 :     if (template)
     191           2 :         appendPQExpBuffer(&sql, " TEMPLATE %s", fmtId(template));
     192          36 :     if (lc_collate)
     193           2 :         appendPQExpBuffer(&sql, " LC_COLLATE '%s'", lc_collate);
     194          36 :     if (lc_ctype)
     195           2 :         appendPQExpBuffer(&sql, " LC_CTYPE '%s'", lc_ctype);
     196             : 
     197          36 :     appendPQExpBufferChar(&sql, ';');
     198             : 
     199             :     /* No point in trying to use postgres db when creating postgres db. */
     200          36 :     if (maintenance_db == NULL && strcmp(dbname, "postgres") == 0)
     201           0 :         maintenance_db = "template1";
     202             : 
     203          36 :     conn = connectMaintenanceDatabase(maintenance_db, host, port, username,
     204             :                                       prompt_password, progname, echo);
     205             : 
     206          36 :     if (echo)
     207           0 :         printf("%s\n", sql.data);
     208          36 :     result = PQexec(conn, sql.data);
     209             : 
     210          36 :     if (PQresultStatus(result) != PGRES_COMMAND_OK)
     211             :     {
     212           2 :         pg_log_error("database creation failed: %s", PQerrorMessage(conn));
     213           2 :         PQfinish(conn);
     214           2 :         exit(1);
     215             :     }
     216             : 
     217          34 :     PQclear(result);
     218             : 
     219          34 :     if (comment)
     220             :     {
     221           0 :         printfPQExpBuffer(&sql, "COMMENT ON DATABASE %s IS ", fmtId(dbname));
     222           0 :         appendStringLiteralConn(&sql, comment, conn);
     223           0 :         appendPQExpBufferChar(&sql, ';');
     224             : 
     225           0 :         if (echo)
     226           0 :             printf("%s\n", sql.data);
     227           0 :         result = PQexec(conn, sql.data);
     228             : 
     229           0 :         if (PQresultStatus(result) != PGRES_COMMAND_OK)
     230             :         {
     231           0 :             pg_log_error("comment creation failed (database was created): %s",
     232             :                          PQerrorMessage(conn));
     233           0 :             PQfinish(conn);
     234           0 :             exit(1);
     235             :         }
     236             : 
     237           0 :         PQclear(result);
     238             :     }
     239             : 
     240          34 :     PQfinish(conn);
     241             : 
     242          34 :     exit(0);
     243             : }
     244             : 
     245             : 
     246             : static void
     247           2 : help(const char *progname)
     248             : {
     249           2 :     printf(_("%s creates a PostgreSQL database.\n\n"), progname);
     250           2 :     printf(_("Usage:\n"));
     251           2 :     printf(_("  %s [OPTION]... [DBNAME] [DESCRIPTION]\n"), progname);
     252           2 :     printf(_("\nOptions:\n"));
     253           2 :     printf(_("  -D, --tablespace=TABLESPACE  default tablespace for the database\n"));
     254           2 :     printf(_("  -e, --echo                   show the commands being sent to the server\n"));
     255           2 :     printf(_("  -E, --encoding=ENCODING      encoding for the database\n"));
     256           2 :     printf(_("  -l, --locale=LOCALE          locale settings for the database\n"));
     257           2 :     printf(_("      --lc-collate=LOCALE      LC_COLLATE setting for the database\n"));
     258           2 :     printf(_("      --lc-ctype=LOCALE        LC_CTYPE setting for the database\n"));
     259           2 :     printf(_("  -O, --owner=OWNER            database user to own the new database\n"));
     260           2 :     printf(_("  -T, --template=TEMPLATE      template database to copy\n"));
     261           2 :     printf(_("  -V, --version                output version information, then exit\n"));
     262           2 :     printf(_("  -?, --help                   show this help, then exit\n"));
     263           2 :     printf(_("\nConnection options:\n"));
     264           2 :     printf(_("  -h, --host=HOSTNAME          database server host or socket directory\n"));
     265           2 :     printf(_("  -p, --port=PORT              database server port\n"));
     266           2 :     printf(_("  -U, --username=USERNAME      user name to connect as\n"));
     267           2 :     printf(_("  -w, --no-password            never prompt for password\n"));
     268           2 :     printf(_("  -W, --password               force password prompt\n"));
     269           2 :     printf(_("  --maintenance-db=DBNAME      alternate maintenance database\n"));
     270           2 :     printf(_("\nBy default, a database with the same name as the current user is created.\n"));
     271           2 :     printf(_("\nReport bugs to <pgsql-bugs@lists.postgresql.org>.\n"));
     272           2 : }

Generated by: LCOV version 1.13