psql.1

来自「PostgreSQL 8.2中增加了很多企业用户所需要的功能和性能上的提高,其开」· 1 代码 · 共 1,822 行 · 第 1/4 页

1
1,822
字号
.\\" auto-generated by docbook2man-spec $Revision: 1.1.1.1 $.TH "PSQL" "1" "2008-01-03" "Application" "PostgreSQL Client Applications".SH NAMEpsql \- PostgreSQL interactive terminal.SH SYNOPSIS.sp\fBpsql\fR [ \fB\fIoption\fB\fR\fI...\fR ]  [ \fB\fIdbname\fB [ \fIusername\fB ] \fR ] .SH "DESCRIPTION".PP\fBpsql\fR is a terminal-based front-end toPostgreSQL. It enables you to type inqueries interactively, issue them toPostgreSQL, and see the query results.Alternatively, input can be from a file. In addition, it provides anumber of meta-commands and various shell-like features tofacilitate writing scripts and automating a wide variety of tasks..SH "OPTIONS".TP\fB-a\fR.TP\fB--echo-all\fRPrint all input lines to standard output as they are read. This is moreuseful for script processing rather than interactive mode. This isequivalent to setting the variable ECHO toall..TP\fB-A\fR.TP\fB--no-align\fRSwitches to unaligned output mode. (The default output mode isotherwise aligned.).TP\fB-c \fIcommand\fB\fR.TP\fB--command \fIcommand\fB\fRSpecifies that \fBpsql\fR is to execute onecommand string, \fIcommand\fR,and then exit. This is useful in shell scripts.\fIcommand\fR must be eithera command string that is completely parsable by the server (i.e.,it contains no \fBpsql\fR specific features),or a single backslash command. Thus you cannot mixSQL and \fBpsql\fRmeta-commands with this option. To achieve that, you couldpipe the string into \fBpsql\fR, likethis: echo '\\x \\\\ SELECT * FROM foo;' | psql.(\\\\ is the separator meta-command.)If the command string contains multiple SQL commands, they areprocessed in a single transaction, unless there are explicit\fBBEGIN\fR/\fBCOMMIT\fR commands included in thestring to divide it into multiple transactions. This isdifferent from the behavior when the same string is fed to\fBpsql\fR's standard input..TP\fB-d \fIdbname\fB\fR.TP\fB--dbname \fIdbname\fB\fRSpecifies the name of the database to connect to. This isequivalent to specifying \fIdbname\fR as the first non-optionargument on the command line..TP\fB-e\fR.TP\fB--echo-queries\fRCopy all SQL commands sent to the server to standard output as well.This is equivalentto setting the variable ECHO toqueries..TP\fB-E\fR.TP\fB--echo-hidden\fREcho the actual queries generated by \fB\\d\fR and other backslashcommands. You can use this to study \fBpsql\fR'sinternal operations. This is equivalent tosetting the variable ECHO_HIDDEN from within\fBpsql\fR..TP\fB-f \fIfilename\fB\fR.TP\fB--file \fIfilename\fB\fRUse the file \fIfilename\fRas the source of commands instead of reading commands interactively.After the file is processed, \fBpsql\fRterminates. This is in many ways equivalent to the internalcommand \fB\\i\fR.If \fIfilename\fR is -(hyphen), then standard input is read.Using this option is subtly different from writing psql< \fIfilename\fR. In general,both will do what you expect, but using -fenables some nice features such as error messages with linenumbers. There is also a slight chance that using this option willreduce the start-up overhead. On the other hand, the variant usingthe shell's input redirection is (in theory) guaranteed to yieldexactly the same output that you would have gotten had you enteredeverything by hand..TP\fB-F \fIseparator\fB\fR.TP\fB--field-separator \fIseparator\fB\fRUse \fIseparator\fR as thefield separator for unaligned output. This is equivalent to\fB\\pset fieldsep\fR or \fB\\f\fR..TP\fB-h \fIhostname\fB\fR.TP\fB--host \fIhostname\fB\fRSpecifies the host name of the machine on which theserver is running. If the value beginswith a slash, it is used as the directory for the Unix-domainsocket..TP\fB-H\fR.TP\fB--html\fRTurn on HTML tabular output. This isequivalent to \\pset format html or the\fB\\H\fR command..TP\fB-l\fR.TP\fB--list\fRList all available databases, then exit. Other non-connectionoptions are ignored. This is similar to the internal command\fB\\list\fR..TP\fB-L \fIfilename\fB\fR.TP\fB--log-file \fIfilename\fB\fRWrite all query output into file \fIfilename\fR, in addition to thenormal output destination..TP\fB-o \fIfilename\fB\fR.TP\fB--output \fIfilename\fB\fRPut all query output into file \fIfilename\fR. This is equivalent tothe command \fB\\o\fR..TP\fB-p \fIport\fB\fR.TP\fB--port \fIport\fB\fRSpecifies the TCP port or the local Unix-domainsocket file extension on which the server is listening forconnections. Defaults to the value of the \fBPGPORT\fRenvironment variable or, if not set, to the port specified atcompile time, usually 5432..TP\fB-P \fIassignment\fB\fR.TP\fB--pset \fIassignment\fB\fRAllows you to specify printing options in the style of\fB\\pset\fR on the command line. Note that here youhave to separate name and value with an equal sign instead of aspace. Thus to set the output format to LaTeX, you could write-P format=latex..TP\fB-q\fR.TP\fB--quiet\fRSpecifies that \fBpsql\fR should do its workquietly. By default, it prints welcome messages and variousinformational output. If this option is used, none of thishappens. This is useful with the \fB-c\fR option.Within \fBpsql\fR you can also set theQUIET variable to achieve the same effect..TP\fB-R \fIseparator\fB\fR.TP\fB--record-separator \fIseparator\fB\fRUse \fIseparator\fR as therecord separator for unaligned output. This is equivalent to the\fB\\pset recordsep\fR command..TP\fB-s\fR.TP\fB--single-step\fRRun in single-step mode. That means the user is prompted beforeeach command is sent to the server, with the option to cancelexecution as well. Use this to debug scripts..TP\fB-S\fR.TP\fB--single-line\fRRuns in single-line mode where a newline terminates an SQL command, as asemicolon does..sp.RS.B "Note:"This mode is provided for those who insist on it, but you are notnecessarily encouraged to use it. In particular, if you mixSQL and meta-commands on a line the order ofexecution might not always be clear to the inexperienced user..RE.sp.TP\fB-t\fR.TP\fB--tuples-only\fRTurn off printing of column names and result row count footers,etc. This is equivalent to the \fB\\t\fR command..TP\fB-T \fItable_options\fB\fR.TP\fB--table-attr \fItable_options\fB\fRAllows you to specify options to be placed within theHTML table tag. See\fB\\pset\fR for details..TP\fB-u\fRForces \fBpsql\fR to prompt for the user name andpassword before connecting to the database.This option is deprecated, as it is conceptually flawed.(Prompting for a non-default user name and prompting for apassword because the server requires it are really two differentthings.) You are encouraged to look at the \fB-U\fR and\fB-W\fR options instead..TP\fB-U \fIusername\fB\fR.TP\fB--username \fIusername\fB\fRConnect to the database as the user \fIusername\fR instead of the default.(You must have permission to do so, of course.).TP\fB-v \fIassignment\fB\fR.TP\fB--set \fIassignment\fB\fR.TP\fB--variable \fIassignment\fB\fRPerform a variable assignment, like the \fB\\set\fRinternal command. Note that you must separate name and value, ifany, by an equal sign on the command line. To unset a variable,leave off the equal sign. To just set a variable without a value,use the equal sign but leave off the value. These assignments aredone during a very early stage of start-up, so variables reservedfor internal purposes might get overwritten later..TP\fB-V\fR.TP\fB--version\fRPrint the \fBpsql\fR version and exit..TP\fB-W\fR.TP\fB--password\fRForces \fBpsql\fR to prompt for apassword before connecting to a database. \fBpsql\fR should automatically prompt for apassword whenever the server requests password authentication.However, currently password request detection is not totallyreliable, hence this option to force a prompt. If no passwordprompt is issued and the server requires password authentication,the connection attempt will fail.This option will remain set for the entire session, even if youchange the database connection with the meta-command\fB\\connect\fR..TP\fB-x\fR.TP\fB--expanded\fRTurn on the expanded table formatting mode. This is equivalent to the\fB\\x\fR command..TP\fB-X,\fR.TP\fB--no-psqlrc\fRDo not read the start-up file (neither the system-wide\fIpsqlrc\fR file nor the user's\fI~/.psqlrc\fR file)..TP\fB-1\fR.TP\fB--single-transaction\fRWhen \fBpsql\fR executes a script with the\fB-f\fR option, adding this option wraps\fBBEGIN\fR/\fBCOMMIT\fR around the script to execute itas a single transaction. This ensures that either all the commandscomplete successfully, or no changes are applied. If the script itself uses \fBBEGIN\fR, \fBCOMMIT\fR,or \fBROLLBACK\fR, this option will not have the desiredeffects.Also, if the script contains any command that cannot be executedinside a transaction block, specifying this option will cause thatcommand (and hence the whole transaction) to fail..TP\fB-?\fR.TP\fB--help\fRShow help about \fBpsql\fR command linearguments, and exit..SH "EXIT STATUS".PP\fBpsql\fR returns 0 to the shell if itfinished normally, 1 if a fatal error of its own (out of memory,file not found) occurs, 2 if the connection to the server went badand the session was not interactive, and 3 if an error occurred in ascript and the variable ON_ERROR_STOP was set..SH "USAGE".SS "CONNECTING TO A DATABASE".PP\fBpsql\fR is a regularPostgreSQL client application. In orderto connect to a database you need to know the name of your targetdatabase, the host name and port number of the server and what username you want to connect as. \fBpsql\fR can betold about those parameters via command line options, namely\fB-d\fR, \fB-h\fR, \fB-p\fR, and\fB-U\fR respectively. If an argument is found that doesnot belong to any option it will be interpreted as the database name(or the user name, if the database name is already given). Not allthese options are required; there are useful defaults. If you omit the hostname, \fBpsql\fR will connect via a Unix-domain socketto a server on the local host, or via TCP/IP to localhost onmachines that don't have Unix-domain sockets. The default port number isdetermined at compile time.Since the database server uses the same default, you will not haveto specify the port in most cases. The default user name is yourUnix user name, as is the default database name. Note that you can'tjust connect to any database under any user name. Your databaseadministrator should have informed you about your access rights..PPWhen the defaults aren't quite right, you can save yourselfsome typing by setting the environment variables\fBPGDATABASE\fR, \fBPGHOST\fR,\fBPGPORT\fR and/or \fBPGUSER\fR to appropriatevalues. (For additional environment variables, see in the documentation.) It is also convenient to have a\fI~/.pgpass\fR file to avoid regularly having to type inpasswords. See in the documentation for more information..PPIf the connection could not be made for any reason (e.g., insufficientprivileges, server is not running on the targeted host, etc.),\fBpsql\fR will return an error and terminate..SS "ENTERING SQL COMMANDS".PPIn normal operation, \fBpsql\fR provides aprompt with the name of the database to which\fBpsql\fR is currently connected, followed bythe string =>. For example,.sp.nf$ \fBpsql testdb\fRWelcome to psql 8.2.6, the PostgreSQL interactive terminal.Type:  \\copyright for distribution terms       \\h for help with SQL commands       \\? for help with psql commands       \\g or terminate with semicolon to execute query       \\q to quittestdb=>.sp.fi.PPAt the prompt, the user may type in SQL commands.Ordinarily, input lines are sent to the server when acommand-terminating semicolon is reached. An end of line does notterminate a command. Thus commands can be spread over several lines forclarity. If the command was sent and executed without error, the resultsof the command are displayed on the screen..PPWhenever a command is executed, \fBpsql\fR also pollsfor asynchronous notification events generated byLISTEN [\fBlisten\fR(7)] andNOTIFY [\fBnotify\fR(7)]..SS "META-COMMANDS".PPAnything you enter in \fBpsql\fR that beginswith an unquoted backslash is a \fBpsql\fRmeta-command that is processed by \fBpsql\fRitself. These commands help make\fBpsql\fR more useful for administration orscripting. Meta-commands are more commonly called slash or backslashcommands..PPThe format of a \fBpsql\fR command is the backslash,followed immediately by a command verb, then any arguments. The argumentsare separated from the command verb and each other by any number ofwhitespace characters..PPTo include whitespace into an argument you may quote it with asingle quote. To include a single quote into such an argument,use two single quotes. Anything contained in single quotes isfurthermore subject to C-like substitutions for\\n (new line), \\t (tab),\\\fIdigits\fR (octal), and\\x\fIdigits\fR (hexadecimal)..PPIf an unquoted argument begins with a colon (:),it is taken as a \fBpsql\fR variable and the value of thevariable is used as the argument instead..PPArguments that are enclosed in backquotes (`)are taken as a command line that is passed to the shell. Theoutput of the command (with any trailing newline removed) is takenas the argument value. The above escape sequences also apply inbackquotes..PPSome commands take an SQL identifier (such as atable name) as argument. These arguments follow the syntax rulesof SQL: Unquoted letters are forced tolowercase, while double quotes (") protect lettersfrom case conversion and allow incorporation of whitespace intothe identifier. Within double quotes, paired double quotes reduceto a single double quote in the resulting name. For example,FOO"BAR"BAZ is interpreted as fooBARbaz,and "A weird"" name" becomes A weird"name..PPParsing for arguments stops when another unquoted backslash occurs.This is taken as the beginning of a new meta-command. The specialsequence \\\\ (two backslashes) marks the end ofarguments and continues parsing SQL commands, ifany. That way SQL and\fBpsql\fR commands can be freely mixed on aline. But in any case, the arguments of a meta-command cannotcontinue beyond the end of the line..PPThe following meta-commands are defined:.TP\fB\\a\fRIf the current table output format is unaligned, it is switched to aligned.If it is not unaligned, it is set to unaligned. This command iskept for backwards compatibility. See \fB\\pset\fR for amore general solution..TP\fB\\cd [ \fIdirectory\fB ]\fRChanges the current working directory to\fIdirectory\fR. Without argument, changesto the current user's home directory..sp.RS.B "Tip:"To print your current working directory, use \\!pwd..RE.sp.TP\fB\\C [ \fItitle\fB ]\fRSets the title of any tables being printed as the result of aquery or unset any such title. This command is equivalent to\\pset title \fItitle\fR. (The name ofthis command derives from ``caption'', as it waspreviously only used to set the caption in anHTML table.).TP\fB\\connect (or \\c) [ \fIdbname\fB [ \fIusername\fB ] [ \fIhost\fB ] [ \fIport\fB ] ]\fREstablishes a new connection to a PostgreSQL

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?