2 * psql - the PostgreSQL interactive terminal
4 * Copyright (c) 2000-2010, 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"));
127 printf(_("\nConnection options:\n"));
128 /* Display default host */
129 env = getenv("PGHOST");
130 printf(_(" -h, --host=HOSTNAME database server host or socket directory (default: \"%s\")\n"),
131 env ? env : _("local socket"));
132 /* Display default port */
133 env = getenv("PGPORT");
134 printf(_(" -p, --port=PORT database server port (default: \"%s\")\n"),
135 env ? env : DEF_PGPORT_STR);
136 /* Display default user */
137 env = getenv("PGUSER");
140 printf(_(" -U, --username=USERNAME database user name (default: \"%s\")\n"), env);
141 printf(_(" -w, --no-password never prompt for password\n"));
142 printf(_(" -W, --password force password prompt (should happen automatically)\n"));
144 printf(_("\nFor more information, type \"\\?\" (for internal commands) or \"\\help\" (for SQL\n"
145 "commands) from within psql, or consult the psql section in the PostgreSQL\n"
146 "documentation.\n\n"));
147 printf(_("Report bugs to <pgsql-bugs@postgresql.org>.\n"));
154 * print out help for the backslash commands
157 slashUsage(unsigned short int pager)
161 output = PageOutput(90, pager);
163 /* if you add/remove a line here, change the row count above */
165 fprintf(output, _("General\n"));
166 fprintf(output, _(" \\copyright show PostgreSQL usage and distribution terms\n"));
167 fprintf(output, _(" \\g [FILE] or ; execute query (and send results to file or |pipe)\n"));
168 fprintf(output, _(" \\h [NAME] help on syntax of SQL commands, * for all commands\n"));
169 fprintf(output, _(" \\q quit psql\n"));
170 fprintf(output, "\n");
172 fprintf(output, _("Query Buffer\n"));
173 fprintf(output, _(" \\e [FILE] [LINE] edit the query buffer (or file) with external editor\n"));
174 fprintf(output, _(" \\ef [FUNCNAME [LINE]] edit function definition with external editor\n"));
175 fprintf(output, _(" \\p show the contents of the query buffer\n"));
176 fprintf(output, _(" \\r reset (clear) the query buffer\n"));
178 fprintf(output, _(" \\s [FILE] display history or save it to file\n"));
180 fprintf(output, _(" \\w FILE write query buffer to file\n"));
181 fprintf(output, "\n");
183 fprintf(output, _("Input/Output\n"));
184 fprintf(output, _(" \\copy ... perform SQL COPY with data stream to the client host\n"));
185 fprintf(output, _(" \\echo [STRING] write string to standard output\n"));
186 fprintf(output, _(" \\i FILE execute commands from file\n"));
187 fprintf(output, _(" \\o [FILE] send all query results to file or |pipe\n"));
188 fprintf(output, _(" \\qecho [STRING] write string to query output stream (see \\o)\n"));
189 fprintf(output, "\n");
191 fprintf(output, _("Informational\n"));
192 fprintf(output, _(" (options: S = show system objects, + = additional detail)\n"));
193 fprintf(output, _(" \\d[S+] list tables, views, and sequences\n"));
194 fprintf(output, _(" \\d[S+] NAME describe table, view, sequence, or index\n"));
195 fprintf(output, _(" \\da[S] [PATTERN] list aggregates\n"));
196 fprintf(output, _(" \\db[+] [PATTERN] list tablespaces\n"));
197 fprintf(output, _(" \\dc[S] [PATTERN] list conversions\n"));
198 fprintf(output, _(" \\dC [PATTERN] list casts\n"));
199 fprintf(output, _(" \\dd[S] [PATTERN] show comments on objects\n"));
200 fprintf(output, _(" \\ddp [PATTERN] list default privileges\n"));
201 fprintf(output, _(" \\dD[S] [PATTERN] list domains\n"));
202 fprintf(output, _(" \\des[+] [PATTERN] list foreign servers\n"));
203 fprintf(output, _(" \\deu[+] [PATTERN] list user mappings\n"));
204 fprintf(output, _(" \\dew[+] [PATTERN] list foreign-data wrappers\n"));
205 fprintf(output, _(" \\df[antw][S+] [PATRN] list [only agg/normal/trigger/window] functions\n"));
206 fprintf(output, _(" \\dF[+] [PATTERN] list text search configurations\n"));
207 fprintf(output, _(" \\dFd[+] [PATTERN] list text search dictionaries\n"));
208 fprintf(output, _(" \\dFp[+] [PATTERN] list text search parsers\n"));
209 fprintf(output, _(" \\dFt[+] [PATTERN] list text search templates\n"));
210 fprintf(output, _(" \\dg[+] [PATTERN] list roles\n"));
211 fprintf(output, _(" \\di[S+] [PATTERN] list indexes\n"));
212 fprintf(output, _(" \\dl list large objects, same as \\lo_list\n"));
213 fprintf(output, _(" \\dn[+] [PATTERN] list schemas\n"));
214 fprintf(output, _(" \\do[S] [PATTERN] list operators\n"));
215 fprintf(output, _(" \\dp [PATTERN] list table, view, and sequence access privileges\n"));
216 fprintf(output, _(" \\drds [PATRN1 [PATRN2]] list per-database role settings\n"));
217 fprintf(output, _(" \\ds[S+] [PATTERN] list sequences\n"));
218 fprintf(output, _(" \\dt[S+] [PATTERN] list tables\n"));
219 fprintf(output, _(" \\dT[S+] [PATTERN] list data types\n"));
220 fprintf(output, _(" \\du[+] [PATTERN] list roles\n"));
221 fprintf(output, _(" \\dv[S+] [PATTERN] list views\n"));
222 fprintf(output, _(" \\l[+] list all databases\n"));
223 fprintf(output, _(" \\sf[+] FUNCNAME show a function's definition\n"));
224 fprintf(output, _(" \\z [PATTERN] same as \\dp\n"));
225 fprintf(output, "\n");
227 fprintf(output, _("Formatting\n"));
228 fprintf(output, _(" \\a toggle between unaligned and aligned output mode\n"));
229 fprintf(output, _(" \\C [STRING] set table title, or unset if none\n"));
230 fprintf(output, _(" \\f [STRING] show or set field separator for unaligned query output\n"));
231 fprintf(output, _(" \\H toggle HTML output mode (currently %s)\n"),
232 ON(pset.popt.topt.format == PRINT_HTML));
233 fprintf(output, _(" \\pset NAME [VALUE] set table output option\n"
234 " (NAME := {format|border|expanded|fieldsep|footer|null|\n"
235 " numericlocale|recordsep|tuples_only|title|tableattr|pager})\n"));
236 fprintf(output, _(" \\t [on|off] show only rows (currently %s)\n"),
237 ON(pset.popt.topt.tuples_only));
238 fprintf(output, _(" \\T [STRING] set HTML <table> tag attributes, or unset if none\n"));
239 fprintf(output, _(" \\x [on|off] toggle expanded output (currently %s)\n"),
240 ON(pset.popt.topt.expanded));
241 fprintf(output, "\n");
243 fprintf(output, _("Connection\n"));
244 fprintf(output, _(" \\c[onnect] [DBNAME|- USER|- HOST|- PORT|-]\n"
245 " connect to new database (currently \"%s\")\n"),
247 fprintf(output, _(" \\encoding [ENCODING] show or set client encoding\n"));
248 fprintf(output, _(" \\password [USERNAME] securely change the password for a user\n"));
249 fprintf(output, _(" \\conninfo display information about current connection\n"));
250 fprintf(output, "\n");
252 fprintf(output, _("Operating System\n"));
253 fprintf(output, _(" \\cd [DIR] change the current working directory\n"));
254 fprintf(output, _(" \\timing [on|off] toggle timing of commands (currently %s)\n"),
256 fprintf(output, _(" \\! [COMMAND] execute command in shell or start interactive shell\n"));
257 fprintf(output, "\n");
259 fprintf(output, _("Variables\n"));
260 fprintf(output, _(" \\prompt [TEXT] NAME prompt user to set internal variable\n"));
261 fprintf(output, _(" \\set [NAME [VALUE]] set internal variable, or list all if no parameters\n"));
262 fprintf(output, _(" \\unset NAME unset (delete) internal variable\n"));
263 fprintf(output, "\n");
265 fprintf(output, _("Large Objects\n"));
266 fprintf(output, _(" \\lo_export LOBOID FILE\n"
267 " \\lo_import FILE [COMMENT]\n"
269 " \\lo_unlink LOBOID large object operations\n"));
277 * helpSQL -- help with SQL commands
279 * Note: we assume caller removed any trailing spaces in "topic".
282 helpSQL(const char *topic, unsigned short int pager)
284 #define VALUE_OR_NULL(a) ((a) ? (a) : "")
286 if (!topic || strlen(topic) == 0)
288 /* Print all the available command names */
297 struct winsize screen_size;
299 if (ioctl(fileno(stdout), TIOCGWINSZ, &screen_size) == -1)
300 screen_width = 80; /* ioctl failed, assume 80 */
302 screen_width = screen_size.ws_col;
304 screen_width = 80; /* default assumption */
307 ncolumns = (screen_width - 3) / (QL_MAX_CMD_LEN + 1);
308 ncolumns = Max(ncolumns, 1);
309 nrows = (QL_HELP_COUNT + (ncolumns - 1)) / ncolumns;
311 output = PageOutput(nrows + 1, pager);
313 fputs(_("Available help:\n"), output);
315 for (i = 0; i < nrows; i++)
317 fprintf(output, " ");
318 for (j = 0; j < ncolumns - 1; j++)
319 fprintf(output, "%-*s",
321 VALUE_OR_NULL(QL_HELP[i + j * nrows].cmd));
322 if (i + j * nrows < QL_HELP_COUNT)
323 fprintf(output, "%s",
324 VALUE_OR_NULL(QL_HELP[i + j * nrows].cmd));
335 bool help_found = false;
342 * We first try exact match, then first + second words, then first
347 for (x = 1; x <= 3; x++)
349 if (x > 1) /* Nothing on first pass - try the opening
353 while (topic[j] != ' ' && j++ < len)
358 while (topic[j] != ' ' && j++ <= len)
361 if (wordlen >= len) /* Don't try again if the same word */
364 output = PageOutput(nl_count, pager);
370 /* Count newlines for pager */
371 for (i = 0; QL_HELP[i].cmd; i++)
373 if (pg_strncasecmp(topic, QL_HELP[i].cmd, len) == 0 ||
374 strcmp(topic, "*") == 0)
376 nl_count += 5 + QL_HELP[i].nl_count;
378 /* If we have an exact match, exit. Fixes \h SELECT */
379 if (pg_strcasecmp(topic, QL_HELP[i].cmd) == 0)
385 output = PageOutput(nl_count, pager);
387 for (i = 0; QL_HELP[i].cmd; i++)
389 if (pg_strncasecmp(topic, QL_HELP[i].cmd, len) == 0 ||
390 strcmp(topic, "*") == 0)
392 PQExpBufferData buffer;
394 initPQExpBuffer(&buffer);
395 QL_HELP[i].syntaxfunc(&buffer);
397 fprintf(output, _("Command: %s\n"
403 /* If we have an exact match, exit. Fixes \h SELECT */
404 if (pg_strcasecmp(topic, QL_HELP[i].cmd) == 0)
408 if (help_found) /* Don't keep trying if we got a match */
413 fprintf(output, _("No help available for \"%s\".\nTry \\h with no arguments to see available help.\n"), topic);
422 print_copyright(void)
425 "PostgreSQL Data Base Management System\n\n"
426 "Portions Copyright (c) 1996-2010, PostgreSQL Global Development Group\n\n"
427 "This software is based on Postgres95, formerly known as Postgres, which\n"
428 "contains the following notice:\n\n"
429 "Portions Copyright(c) 1994, Regents of the University of California\n\n"
430 "Permission to use, copy, modify, and distribute this software and its\n"
431 "documentation for any purpose, without fee, and without a written agreement\n"
432 "is hereby granted, provided that the above copyright notice and this paragraph\n"
433 "and the following two paragraphs appear in all copies.\n\n"
434 "IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR\n"
435 "DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST\n"
436 "PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF\n"
437 "THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH\n"
439 "THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,\n"
440 "BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A\n"
441 "PARTICULAR PURPOSE.THE SOFTWARE PROVIDED HEREUNDER IS ON AN \"AS IS\" BASIS,\n"
442 "AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATIONS TO PROVIDE MAINTENANCE,\n"
443 "SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS."