PageRenderTime 55ms CodeModel.GetById 29ms RepoModel.GetById 1ms app.codeStats 0ms

/src/bin/scripts/createdb.c

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