LCOV - code coverage report
Current view: top level - src/bin/scripts - createdb.c (source / functions) Hit Total Coverage
Test: PostgreSQL 17devel Lines: 156 206 75.7 %
Date: 2024-05-19 07:11:14 Functions: 2 2 100.0 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.14