2 * psql - the PostgreSQL interactive terminal
4 * Copyright (c) 2000-2012, PostgreSQL Global Development Group
8 #include "postgres_fe.h"
12 #include <pwd.h> /* for getpwuid() */
14 #include <sys/types.h> /* (ditto) */
15 #include <unistd.h> /* for geteuid() */
21 #include <sys/ioctl.h> /* for ioctl() */
37 * If you change something in this file, also make the same changes
38 * in the DocBook documentation, file ref/psql-ref.sgml. If you don't
39 * know how to do it, please find someone who can help you.
46 * print out command line arguments
48 #define ON(var) (var ? _("on") : _("off"))
57 struct passwd *pw = NULL;
60 /* Find default user, in case we need it. */
61 user = getenv("PGUSER");
64 #if !defined(WIN32) && !defined(__OS2__)
65 pw = getpwuid(geteuid());
70 psql_error("could not get current user name: %s\n", strerror(errno));
75 DWORD bufsize = sizeof(buf) - 1;
77 if (GetUserName(buf, &bufsize))
82 printf(_("psql is the PostgreSQL interactive terminal.\n\n"));
83 printf(_("Usage:\n"));
84 printf(_(" psql [OPTION]... [DBNAME [USERNAME]]\n\n"));
86 printf(_("General options:\n"));
87 /* Display default database */
88 env = getenv("PGDATABASE");
91 printf(_(" -c, --command=COMMAND run only single command (SQL or internal) and exit\n"));
92 printf(_(" -d, --dbname=DBNAME database name to connect to (default: \"%s\")\n"), env);
93 printf(_(" -f, --file=FILENAME execute commands from file, then exit\n"));
94 printf(_(" -l, --list list available databases, then exit\n"));
95 printf(_(" -v, --set=, --variable=NAME=VALUE\n"
96 " set psql variable NAME to VALUE\n"));
97 printf(_(" -X, --no-psqlrc do not read startup file (~/.psqlrc)\n"));
98 printf(_(" -1 (\"one\"), --single-transaction\n"
99 " execute command file as a single transaction\n"));
100 printf(_(" --help show this help, then exit\n"));
101 printf(_(" --version output version information, then exit\n"));
103 printf(_("\nInput and output options:\n"));
104 printf(_(" -a, --echo-all echo all input from script\n"));
105 printf(_(" -e, --echo-queries echo commands sent to server\n"));
106 printf(_(" -E, --echo-hidden display queries that internal commands generate\n"));
107 printf(_(" -L, --log-file=FILENAME send session log to file\n"));
108 printf(_(" -n, --no-readline disable enhanced command line editing (readline)\n"));
109 printf(_(" -o, --output=FILENAME send query results to file (or |pipe)\n"));
110 printf(_(" -q, --quiet run quietly (no messages, only query output)\n"));
111 printf(_(" -s, --single-step single-step mode (confirm each query)\n"));
112 printf(_(" -S, --single-line single-line mode (end of line terminates SQL command)\n"));
114 printf(_("\nOutput format options:\n"));
115 printf(_(" -A, --no-align unaligned table output mode\n"));
116 printf(_(" -F, --field-separator=STRING\n"
117 " set field separator (default: \"%s\")\n"),
119 printf(_(" -H, --html HTML table output mode\n"));
120 printf(_(" -P, --pset=VAR[=ARG] set printing option VAR to ARG (see \\pset command)\n"));
121 printf(_(" -R, --record-separator=STRING\n"
122 " set record separator (default: newline)\n"));
123 printf(_(" -t, --tuples-only print rows only\n"));
124 printf(_(" -T, --table-attr=TEXT set HTML table tag attributes (e.g., width, border)\n"));
125 printf(_(" -x, --expanded turn on expanded table output\n"));
126 printf(_(" -z, --field-separator-zero\n"
127 " set field separator to zero byte\n"));
128 printf(_(" -0, --record-separator-zero\n"
129 " set record separator to zero byte\n"));
131 printf(_("\nConnection options:\n"));
132 /* Display default host */
133 env = getenv("PGHOST");
134 printf(_(" -h, --host=HOSTNAME database server host or socket directory (default: \"%s\")\n"),
135 env ? env : _("local socket"));
136 /* Display default port */
137 env = getenv("PGPORT");
138 printf(_(" -p, --port=PORT database server port (default: \"%s\")\n"),
139 env ? env : DEF_PGPORT_STR);
140 /* Display default user */
141 env = getenv("PGUSER");
144 printf(_(" -U, --username=USERNAME database user name (default: \"%s\")\n"), env);
145 printf(_(" -w, --no-password never prompt for password\n"));
146 printf(_(" -W, --password force password prompt (should happen automatically)\n"));
148 printf(_("\nFor more information, type \"\\?\" (for internal commands) or \"\\help\" (for SQL\n"
149 "commands) from within psql, or consult the psql section in the PostgreSQL\n"
150 "documentation.\n\n"));
151 printf(_("Report bugs to <pgsql-bugs@postgresql.org>.\n"));
158 * print out help for the backslash commands
161 slashUsage(unsigned short int pager)
165 output = PageOutput(94, pager);
167 /* if you add/remove a line here, change the row count above */
169 fprintf(output, _("General\n"));
170 fprintf(output, _(" \\copyright show PostgreSQL usage and distribution terms\n"));
171 fprintf(output, _(" \\g [FILE] or ; execute query (and send results to file or |pipe)\n"));
172 fprintf(output, _(" \\h [NAME] help on syntax of SQL commands, * for all commands\n"));
173 fprintf(output, _(" \\q quit psql\n"));
174 fprintf(output, "\n");
176 fprintf(output, _("Query Buffer\n"));
177 fprintf(output, _(" \\e [FILE] [LINE] edit the query buffer (or file) with external editor\n"));
178 fprintf(output, _(" \\ef [FUNCNAME [LINE]] edit function definition with external editor\n"));
179 fprintf(output, _(" \\p show the contents of the query buffer\n"));
180 fprintf(output, _(" \\r reset (clear) the query buffer\n"));
182 fprintf(output, _(" \\s [FILE] display history or save it to file\n"));
184 fprintf(output, _(" \\w FILE write query buffer to file\n"));
185 fprintf(output, "\n");
187 fprintf(output, _("Input/Output\n"));
188 fprintf(output, _(" \\copy ... perform SQL COPY with data stream to the client host\n"));
189 fprintf(output, _(" \\echo [STRING] write string to standard output\n"));
190 fprintf(output, _(" \\i FILE execute commands from file\n"));
191 fprintf(output, _(" \\ir FILE as \\i, but relative to location of current script\n"));
192 fprintf(output, _(" \\o [FILE] send all query results to file or |pipe\n"));
193 fprintf(output, _(" \\qecho [STRING] write string to query output stream (see \\o)\n"));
194 fprintf(output, "\n");
196 fprintf(output, _("Informational\n"));
197 fprintf(output, _(" (options: S = show system objects, + = additional detail)\n"));
198 fprintf(output, _(" \\d[S+] list tables, views, and sequences\n"));
199 fprintf(output, _(" \\d[S+] NAME describe table, view, sequence, or index\n"));
200 fprintf(output, _(" \\da[S] [PATTERN] list aggregates\n"));
201 fprintf(output, _(" \\db[+] [PATTERN] list tablespaces\n"));
202 fprintf(output, _(" \\dc[S+] [PATTERN] list conversions\n"));
203 fprintf(output, _(" \\dC[+] [PATTERN] list casts\n"));
204 fprintf(output, _(" \\dd[S] [PATTERN] show object descriptions not displayed elsewhere\n"));
205 fprintf(output, _(" \\ddp [PATTERN] list default privileges\n"));
206 fprintf(output, _(" \\dD[S+] [PATTERN] list domains\n"));
207 fprintf(output, _(" \\det[+] [PATTERN] list foreign tables\n"));
208 fprintf(output, _(" \\des[+] [PATTERN] list foreign servers\n"));
209 fprintf(output, _(" \\deu[+] [PATTERN] list user mappings\n"));
210 fprintf(output, _(" \\dew[+] [PATTERN] list foreign-data wrappers\n"));
211 fprintf(output, _(" \\df[antw][S+] [PATRN] list [only agg/normal/trigger/window] functions\n"));
212 fprintf(output, _(" \\dF[+] [PATTERN] list text search configurations\n"));
213 fprintf(output, _(" \\dFd[+] [PATTERN] list text search dictionaries\n"));
214 fprintf(output, _(" \\dFp[+] [PATTERN] list text search parsers\n"));
215 fprintf(output, _(" \\dFt[+] [PATTERN] list text search templates\n"));
216 fprintf(output, _(" \\dg[+] [PATTERN] list roles\n"));
217 fprintf(output, _(" \\di[S+] [PATTERN] list indexes\n"));
218 fprintf(output, _(" \\dl list large objects, same as \\lo_list\n"));
219 fprintf(output, _(" \\dL[S+] [PATTERN] list procedural languages\n"));
220 fprintf(output, _(" \\dn[S+] [PATTERN] list schemas\n"));
221 fprintf(output, _(" \\do[S] [PATTERN] list operators\n"));
222 fprintf(output, _(" \\dO[S+] [PATTERN] list collations\n"));
223 fprintf(output, _(" \\dp [PATTERN] list table, view, and sequence access privileges\n"));
224 fprintf(output, _(" \\drds [PATRN1 [PATRN2]] list per-database role settings\n"));
225 fprintf(output, _(" \\ds[S+] [PATTERN] list sequences\n"));
226 fprintf(output, _(" \\dt[S+] [PATTERN] list tables\n"));
227 fprintf(output, _(" \\dT[S+] [PATTERN] list data types\n"));
228 fprintf(output, _(" \\du[+] [PATTERN] list roles\n"));
229 fprintf(output, _(" \\dv[S+] [PATTERN] list views\n"));
230 fprintf(output, _(" \\dE[S+] [PATTERN] list foreign tables\n"));
231 fprintf(output, _(" \\dx[+] [PATTERN] list extensions\n"));
232 fprintf(output, _(" \\l[+] list all databases\n"));
233 fprintf(output, _(" \\sf[+] FUNCNAME show a function's definition\n"));
234 fprintf(output, _(" \\z [PATTERN] same as \\dp\n"));
235 fprintf(output, "\n");
237 fprintf(output, _("Formatting\n"));
238 fprintf(output, _(" \\a toggle between unaligned and aligned output mode\n"));
239 fprintf(output, _(" \\C [STRING] set table title, or unset if none\n"));
240 fprintf(output, _(" \\f [STRING] show or set field separator for unaligned query output\n"));
241 fprintf(output, _(" \\H toggle HTML output mode (currently %s)\n"),
242 ON(pset.popt.topt.format == PRINT_HTML));
243 fprintf(output, _(" \\pset NAME [VALUE] set table output option\n"
244 " (NAME := {format|border|expanded|fieldsep|fieldsep_zero|footer|null|\n"
245 " numericlocale|recordsep|recordsep_zero|tuples_only|title|tableattr|pager})\n"));
246 fprintf(output, _(" \\t [on|off] show only rows (currently %s)\n"),
247 ON(pset.popt.topt.tuples_only));
248 fprintf(output, _(" \\T [STRING] set HTML <table> tag attributes, or unset if none\n"));
249 fprintf(output, _(" \\x [on|off|auto] toggle expanded output (currently %s)\n"),
250 pset.popt.topt.expanded == 2 ? "auto" : ON(pset.popt.topt.expanded));
251 fprintf(output, "\n");
253 fprintf(output, _("Connection\n"));
254 fprintf(output, _(" \\c[onnect] [DBNAME|- USER|- HOST|- PORT|-]\n"
255 " connect to new database (currently \"%s\")\n"),
257 fprintf(output, _(" \\encoding [ENCODING] show or set client encoding\n"));
258 fprintf(output, _(" \\password [USERNAME] securely change the password for a user\n"));
259 fprintf(output, _(" \\conninfo display information about current connection\n"));
260 fprintf(output, "\n");
262 fprintf(output, _("Operating System\n"));
263 fprintf(output, _(" \\cd [DIR] change the current working directory\n"));
264 fprintf(output, _(" \\setenv NAME [VALUE] set or unset environment variable\n"));
265 fprintf(output, _(" \\timing [on|off] toggle timing of commands (currently %s)\n"),
267 fprintf(output, _(" \\! [COMMAND] execute command in shell or start interactive shell\n"));
268 fprintf(output, "\n");
270 fprintf(output, _("Variables\n"));
271 fprintf(output, _(" \\prompt [TEXT] NAME prompt user to set internal variable\n"));
272 fprintf(output, _(" \\set [NAME [VALUE]] set internal variable, or list all if no parameters\n"));
273 fprintf(output, _(" \\unset NAME unset (delete) internal variable\n"));
274 fprintf(output, "\n");
276 fprintf(output, _("Large Objects\n"));
277 fprintf(output, _(" \\lo_export LOBOID FILE\n"
278 " \\lo_import FILE [COMMENT]\n"
280 " \\lo_unlink LOBOID large object operations\n"));
288 * helpSQL -- help with SQL commands
290 * Note: we assume caller removed any trailing spaces in "topic".
293 helpSQL(const char *topic, unsigned short int pager)
295 #define VALUE_OR_NULL(a) ((a) ? (a) : "")
297 if (!topic || strlen(topic) == 0)
299 /* Print all the available command names */
308 struct winsize screen_size;
310 if (ioctl(fileno(stdout), TIOCGWINSZ, &screen_size) == -1)
311 screen_width = 80; /* ioctl failed, assume 80 */
313 screen_width = screen_size.ws_col;
315 screen_width = 80; /* default assumption */
318 ncolumns = (screen_width - 3) / (QL_MAX_CMD_LEN + 1);
319 ncolumns = Max(ncolumns, 1);
320 nrows = (QL_HELP_COUNT + (ncolumns - 1)) / ncolumns;
322 output = PageOutput(nrows + 1, pager);
324 fputs(_("Available help:\n"), output);
326 for (i = 0; i < nrows; i++)
328 fprintf(output, " ");
329 for (j = 0; j < ncolumns - 1; j++)
330 fprintf(output, "%-*s",
332 VALUE_OR_NULL(QL_HELP[i + j * nrows].cmd));
333 if (i + j * nrows < QL_HELP_COUNT)
334 fprintf(output, "%s",
335 VALUE_OR_NULL(QL_HELP[i + j * nrows].cmd));
346 bool help_found = false;
353 * We first try exact match, then first + second words, then first
358 for (x = 1; x <= 3; x++)
360 if (x > 1) /* Nothing on first pass - try the opening
364 while (topic[j] != ' ' && j++ < len)
369 while (topic[j] != ' ' && j++ <= len)
372 if (wordlen >= len) /* Don't try again if the same word */
375 output = PageOutput(nl_count, pager);
381 /* Count newlines for pager */
382 for (i = 0; QL_HELP[i].cmd; i++)
384 if (pg_strncasecmp(topic, QL_HELP[i].cmd, len) == 0 ||
385 strcmp(topic, "*") == 0)
387 nl_count += 5 + QL_HELP[i].nl_count;
389 /* If we have an exact match, exit. Fixes \h SELECT */
390 if (pg_strcasecmp(topic, QL_HELP[i].cmd) == 0)
396 output = PageOutput(nl_count, pager);
398 for (i = 0; QL_HELP[i].cmd; i++)
400 if (pg_strncasecmp(topic, QL_HELP[i].cmd, len) == 0 ||
401 strcmp(topic, "*") == 0)
403 PQExpBufferData buffer;
405 initPQExpBuffer(&buffer);
406 QL_HELP[i].syntaxfunc(&buffer);
408 fprintf(output, _("Command: %s\n"
414 /* If we have an exact match, exit. Fixes \h SELECT */
415 if (pg_strcasecmp(topic, QL_HELP[i].cmd) == 0)
419 if (help_found) /* Don't keep trying if we got a match */
424 fprintf(output, _("No help available for \"%s\".\nTry \\h with no arguments to see available help.\n"), topic);
433 print_copyright(void)
436 "PostgreSQL Database Management System\n"
437 "(formerly known as Postgres, then as Postgres95)\n\n"
438 "Portions Copyright (c) 1996-2011, PostgreSQL Global Development Group\n\n"
439 "Portions Copyright (c) 1994, The Regents of the University of California\n\n"
440 "Permission to use, copy, modify, and distribute this software and its\n"
441 "documentation for any purpose, without fee, and without a written agreement\n"
442 "is hereby granted, provided that the above copyright notice and this\n"
443 "paragraph and the following two paragraphs appear in all copies.\n\n"
444 "IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR\n"
445 "DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING\n"
446 "LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS\n"
447 "DOCUMENTATION, EVEN IF THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE\n"
448 "POSSIBILITY OF SUCH DAMAGE.\n\n"
449 "THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,\n"
450 "INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY\n"
451 "AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS\n"
452 "ON AN \"AS IS\" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATIONS TO\n"
453 "PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.\n"