LCOV - code coverage report
Current view: top level - src/bin/scripts - createdb.c (source / functions) Hit Total Coverage
Test: PostgreSQL 13beta1 Lines: 108 169 63.9 %
Date: 2020-06-03 10:06:28 Functions: 2 2 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*-------------------------------------------------------------------------
       2             :  *
       3             :  * createdb
       4             :  *
       5             :  * Portions Copyright (c) 1996-2020, 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          44 : 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          44 :     const char *dbname = NULL;
      48          44 :     const char *maintenance_db = NULL;
      49          44 :     char       *comment = NULL;
      50          44 :     char       *host = NULL;
      51          44 :     char       *port = NULL;
      52          44 :     char       *username = NULL;
      53          44 :     enum trivalue prompt_password = TRI_DEFAULT;
      54          44 :     bool        echo = false;
      55          44 :     char       *owner = NULL;
      56          44 :     char       *tablespace = NULL;
      57          44 :     char       *template = NULL;
      58          44 :     char       *encoding = NULL;
      59          44 :     char       *lc_collate = NULL;
      60          44 :     char       *lc_ctype = NULL;
      61          44 :     char       *locale = NULL;
      62             : 
      63             :     PQExpBufferData sql;
      64             : 
      65             :     PGconn     *conn;
      66             :     PGresult   *result;
      67             : 
      68          44 :     pg_logging_init(argv[0]);
      69          44 :     progname = get_progname(argv[0]);
      70          44 :     set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pgscripts"));
      71             : 
      72          44 :     handle_help_version_opts(argc, argv, "createdb", help);
      73             : 
      74          62 :     while ((c = getopt_long(argc, argv, "h:p:U:wWeO:D:T:E:l:", long_options, &optindex)) != -1)
      75             :     {
      76          24 :         switch (c)
      77             :         {
      78           0 :             case 'h':
      79           0 :                 host = pg_strdup(optarg);
      80           0 :                 break;
      81           0 :             case 'p':
      82           0 :                 port = pg_strdup(optarg);
      83           0 :                 break;
      84          12 :             case 'U':
      85          12 :                 username = pg_strdup(optarg);
      86          12 :                 break;
      87           0 :             case 'w':
      88           0 :                 prompt_password = TRI_NO;
      89           0 :                 break;
      90           0 :             case 'W':
      91           0 :                 prompt_password = TRI_YES;
      92           0 :                 break;
      93           0 :             case 'e':
      94           0 :                 echo = true;
      95           0 :                 break;
      96           0 :             case 'O':
      97           0 :                 owner = pg_strdup(optarg);
      98           0 :                 break;
      99           0 :             case 'D':
     100           0 :                 tablespace = pg_strdup(optarg);
     101           0 :                 break;
     102           2 :             case 'T':
     103           2 :                 template = pg_strdup(optarg);
     104           2 :                 break;
     105           4 :             case 'E':
     106           4 :                 encoding = pg_strdup(optarg);
     107           4 :                 break;
     108           2 :             case 1:
     109           2 :                 lc_collate = pg_strdup(optarg);
     110           2 :                 break;
     111           0 :             case 2:
     112           0 :                 lc_ctype = pg_strdup(optarg);
     113           0 :                 break;
     114           2 :             case 'l':
     115           2 :                 locale = pg_strdup(optarg);
     116           2 :                 break;
     117           0 :             case 3:
     118           0 :                 maintenance_db = pg_strdup(optarg);
     119           0 :                 break;
     120           2 :             default:
     121           2 :                 fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
     122           2 :                 exit(1);
     123             :         }
     124             :     }
     125             : 
     126          38 :     switch (argc - optind)
     127             :     {
     128           0 :         case 0:
     129           0 :             break;
     130          38 :         case 1:
     131          38 :             dbname = argv[optind];
     132          38 :             break;
     133           0 :         case 2:
     134           0 :             dbname = argv[optind];
     135           0 :             comment = argv[optind + 1];
     136           0 :             break;
     137           0 :         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          38 :     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          38 :     if (encoding)
     161             :     {
     162           4 :         if (pg_char_to_encoding(encoding) < 0)
     163             :         {
     164           2 :             pg_log_error("\"%s\" is not a valid encoding name", encoding);
     165           2 :             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             :     /* No point in trying to use postgres db when creating postgres db. */
     180          36 :     if (maintenance_db == NULL && strcmp(dbname, "postgres") == 0)
     181           0 :         maintenance_db = "template1";
     182             : 
     183          36 :     conn = connectMaintenanceDatabase(maintenance_db, host, port, username,
     184             :                                       prompt_password, progname, echo);
     185             : 
     186          36 :     initPQExpBuffer(&sql);
     187             : 
     188          36 :     appendPQExpBuffer(&sql, "CREATE DATABASE %s",
     189             :                       fmtId(dbname));
     190             : 
     191          36 :     if (owner)
     192           0 :         appendPQExpBuffer(&sql, " OWNER %s", fmtId(owner));
     193          36 :     if (tablespace)
     194           0 :         appendPQExpBuffer(&sql, " TABLESPACE %s", fmtId(tablespace));
     195          36 :     if (encoding)
     196             :     {
     197           2 :         appendPQExpBufferStr(&sql, " ENCODING ");
     198           2 :         appendStringLiteralConn(&sql, encoding, conn);
     199             :     }
     200          36 :     if (template)
     201           2 :         appendPQExpBuffer(&sql, " TEMPLATE %s", fmtId(template));
     202          36 :     if (lc_collate)
     203             :     {
     204           4 :         appendPQExpBufferStr(&sql, " LC_COLLATE ");
     205           4 :         appendStringLiteralConn(&sql, lc_collate, conn);
     206             :     }
     207          36 :     if (lc_ctype)
     208             :     {
     209           2 :         appendPQExpBufferStr(&sql, " LC_CTYPE ");
     210           2 :         appendStringLiteralConn(&sql, lc_ctype, conn);
     211             :     }
     212             : 
     213          36 :     appendPQExpBufferChar(&sql, ';');
     214             : 
     215          36 :     if (echo)
     216           0 :         printf("%s\n", sql.data);
     217          36 :     result = PQexec(conn, sql.data);
     218             : 
     219          36 :     if (PQresultStatus(result) != PGRES_COMMAND_OK)
     220             :     {
     221           4 :         pg_log_error("database creation failed: %s", PQerrorMessage(conn));
     222           4 :         PQfinish(conn);
     223           4 :         exit(1);
     224             :     }
     225             : 
     226          32 :     PQclear(result);
     227             : 
     228          32 :     if (comment)
     229             :     {
     230           0 :         printfPQExpBuffer(&sql, "COMMENT ON DATABASE %s IS ", fmtId(dbname));
     231           0 :         appendStringLiteralConn(&sql, comment, conn);
     232           0 :         appendPQExpBufferChar(&sql, ';');
     233             : 
     234           0 :         if (echo)
     235           0 :             printf("%s\n", sql.data);
     236           0 :         result = PQexec(conn, sql.data);
     237             : 
     238           0 :         if (PQresultStatus(result) != PGRES_COMMAND_OK)
     239             :         {
     240           0 :             pg_log_error("comment creation failed (database was created): %s",
     241             :                          PQerrorMessage(conn));
     242           0 :             PQfinish(conn);
     243           0 :             exit(1);
     244             :         }
     245             : 
     246           0 :         PQclear(result);
     247             :     }
     248             : 
     249          32 :     PQfinish(conn);
     250             : 
     251          32 :     exit(0);
     252             : }
     253             : 
     254             : 
     255             : static void
     256           2 : help(const char *progname)
     257             : {
     258           2 :     printf(_("%s creates a PostgreSQL database.\n\n"), progname);
     259           2 :     printf(_("Usage:\n"));
     260           2 :     printf(_("  %s [OPTION]... [DBNAME] [DESCRIPTION]\n"), progname);
     261           2 :     printf(_("\nOptions:\n"));
     262           2 :     printf(_("  -D, --tablespace=TABLESPACE  default tablespace for the database\n"));
     263           2 :     printf(_("  -e, --echo                   show the commands being sent to the server\n"));
     264           2 :     printf(_("  -E, --encoding=ENCODING      encoding for the database\n"));
     265           2 :     printf(_("  -l, --locale=LOCALE          locale settings for the database\n"));
     266           2 :     printf(_("      --lc-collate=LOCALE      LC_COLLATE setting for the database\n"));
     267           2 :     printf(_("      --lc-ctype=LOCALE        LC_CTYPE setting for the database\n"));
     268           2 :     printf(_("  -O, --owner=OWNER            database user to own the new database\n"));
     269           2 :     printf(_("  -T, --template=TEMPLATE      template database to copy\n"));
     270           2 :     printf(_("  -V, --version                output version information, then exit\n"));
     271           2 :     printf(_("  -?, --help                   show this help, then exit\n"));
     272           2 :     printf(_("\nConnection options:\n"));
     273           2 :     printf(_("  -h, --host=HOSTNAME          database server host or socket directory\n"));
     274           2 :     printf(_("  -p, --port=PORT              database server port\n"));
     275           2 :     printf(_("  -U, --username=USERNAME      user name to connect as\n"));
     276           2 :     printf(_("  -w, --no-password            never prompt for password\n"));
     277           2 :     printf(_("  -W, --password               force password prompt\n"));
     278           2 :     printf(_("  --maintenance-db=DBNAME      alternate maintenance database\n"));
     279           2 :     printf(_("\nBy default, a database with the same name as the current user is created.\n"));
     280           2 :     printf(_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
     281           2 :     printf(_("%s home page: <%s>\n"), PACKAGE_NAME, PACKAGE_URL);
     282           2 : }

Generated by: LCOV version 1.13