LCOV - code coverage report
Current view: top level - src/bin/scripts - createdb.c (source / functions) Hit Total Coverage
Test: PostgreSQL 14devel Lines: 117 175 66.9 %
Date: 2021-01-26 02:06:48 Functions: 2 2 100.0 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.13